首次使用Tkinter遇TypeError错误求助:repip()需传入1个参数却传入0个的问题排查与修复方案
Hey there! Let's walk through the problems in your code and get it working properly. The TypeError you're seeing is just the tip of the iceberg—there are a few other issues we need to address too.
1. Root Cause of the TypeError
The error happens because you've bound the Start button directly to the repip function, which expects a single argument (a domain/IP string). When you click the button, Tkinter doesn't pass any arguments to repip, hence the "0 given" error.
Additionally, your Start button should trigger the processing of the selected file (like the flow you started in open_file), not call repip directly.
2. Other Critical Issues
- Missing
coloramaImport: You're usingFore.YELLOWandFore.REDbut haven't imported or initialized thecoloramamodule. - Broken File Handling: In
open_file, you're trying to re-open a file object returned byaskopenfile, which will throw an error. Also, after converting the file content to a list of lines, you lose access to the file path viafile.name. - Thread Safety Violation: Tkinter widgets (like your
listbox) can't be modified directly from background threads—this will cause crashes or unpredictable behavior. - Undefined Variable: In the
elseclause ofrepip, you're printingstr(url)buturlisn't defined here (it should befile).
3. Fixed Code
Here's the corrected version of your script with all issues resolved:
import Tkinter as tk from Tkinter import * import ttk import tkFileDialog as filedialog import os import requests from multiprocessing.dummy import Pool as ThreadPool from re import findall as reg from colorama import Fore, init # Initialize colorama for terminal color support init(autoreset=True) main_window = tk.Tk() main_window.title('sample 1') main_window.resizable(False, False) main_window.geometry("390x400") selected_file_path = "" # Track the chosen file path globally def open_file(): global selected_file_path file_obj = filedialog.askopenfile(mode='r', filetypes=[('text files', '*.txt')]) if file_obj: selected_file_path = os.path.abspath(file_obj.name) entry1.delete(0, END) entry1.insert(END, selected_file_path) file_obj.close() def safe_update_listbox(message): # Update Tkinter widget from main thread (required for thread safety) main_window.after(0, lambda: listbox.insert(END, message)) def repip(domain): try: grab = requests.get('https://tools.webservertalk.com/?tool=reverseip&host='+domain).content if 'Domain / IP:' in grab: regx = reg('<td class="text-left">(.*?)<', grab) for ckk in regx: mek = ckk.replace("Domain","").strip() if mek: safe_update_listbox("[Grabbed] {}".format(mek)) with open('grabbed.txt', 'a') as f: f.write('http://'+mek+'\n') else: print "{}[Operation timed out] {}".format(Fore.RED, str(domain)) except Exception as e: print "{}[Error] {}: {}".format(Fore.RED, domain, str(e)) def start_processing(): if not selected_file_path: safe_update_listbox("Please select a domain list file first!") return # Read domains from the selected file with open(selected_file_path, 'r') as f: domains = f.read().splitlines() # Launch thread pool to process domains thread_count = 50 pool = ThreadPool(thread_count) pool.map(repip, domains) pool.close() pool.join() safe_update_listbox("Processing finished!") # UI Setup label = Label(main_window, text="Select domain list") label.grid(row=0, sticky=W, pady=3, padx=3) entry1 = tk.Entry(main_window, width=50) entry1.grid(row=1, sticky=W, pady=3, padx=3) ttk.Button(main_window, text="Browse", command=open_file).grid(row=1, column=1, sticky=W, pady=2) ttk.Button(main_window, text="Start", command=start_processing).grid(row=2, column=1, sticky=W, pady=2) listbox = Listbox(main_window, width=50, height=20) listbox.place(x=2, y=60) scrollbar = Scrollbar(main_window, orient="vertical", command=listbox.yview) scrollbar.place(x=290, y=60, height=323) listbox.config(yscrollcommand=scrollbar.set) tk.mainloop()
Key Changes Explained
- Added
coloramaimport and initialization to fix theForeundefined error. - Created
selected_file_pathto track the chosen file, fixing broken file path access inopen_file. - Added
start_processingfunction to handle reading the file and launching the thread pool—this ensuresrepipgets the required domain argument, fixing the original TypeError. - Implemented
safe_update_listboxwithmain_window.after()to modify the listbox safely from background threads (Tkinter requires all widget changes to happen in the main thread). - Fixed the undefined
urlvariable to usedomaininstead. - Used
with open(...)for file operations to ensure proper file closure. - Added detailed error messages to help debug issues during processing.
内容的提问来源于stack exchange,提问作者Mustafa Ajmail

