You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次使用Tkinter遇TypeError错误求助:repip()需传入1个参数却传入0个的问题排查与修复方案

Fixing TypeError: repip() takes exactly 1 argument (0 given) & Other Issues in Your Tkinter Script

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 colorama Import: You're using Fore.YELLOW and Fore.RED but haven't imported or initialized the colorama module.
  • Broken File Handling: In open_file, you're trying to re-open a file object returned by askopenfile, which will throw an error. Also, after converting the file content to a list of lines, you lose access to the file path via file.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 else clause of repip, you're printing str(url) but url isn't defined here (it should be file).

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 colorama import and initialization to fix the Fore undefined error.
  • Created selected_file_path to track the chosen file, fixing broken file path access in open_file.
  • Added start_processing function to handle reading the file and launching the thread pool—this ensures repip gets the required domain argument, fixing the original TypeError.
  • Implemented safe_update_listbox with main_window.after() to modify the listbox safely from background threads (Tkinter requires all widget changes to happen in the main thread).
  • Fixed the undefined url variable to use domain instead.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:27:34