Kivy垃圾邮件机器人运行报错AssertionError:线程对象不可调用问题排查
问题分析与修复
错误根源
你遇到的AssertionError: <Thread(Thread-1 (flood), initial)> is not callable错误,核心原因有两个:
- 按钮绑定错误:
start_button.bind(on_press=threading.Thread(target=self.flood))中,on_press需要接收一个可调用函数,但你直接传入了Thread实例。Kivy会在按钮点击时尝试调用这个实例,而Thread对象本身不可被直接调用,因此触发断言错误。 - 错误调用start方法:
start_button.start()完全错误——Button类没有start方法,start()是Thread对象的专属方法。
修复方案
需要创建一个中间函数,在按钮点击时创建并启动线程,而非直接把Thread实例绑定到点击事件。同时修正窗口配置拼写、标签位置(原位置超出窗口范围导致不可见)等小问题。
修正后的代码
import time import pyautogui import os import threading os.environ["KIVY_NO_CONSOLELOG"] = "1" from kivy.config import Config Config.set("graphics", "width", "400") Config.set("graphics", "height", "350") # 修正拼写错误hight→height,调高窗口高度以显示所有标签 Config.set("graphics", "resizable", "0") from kivy.app import App from kivy.uix.widget import Widget from kivy.uix.label import Label from kivy.uix.button import Button from kivy.uix.textinput import TextInput class asd(Widget): def __init__(self): super().__init__() start_button = Button(text="Start", size=(100, 50), pos=(150, 20)) # 绑定到中间函数,而非直接绑定Thread实例 start_button.bind(on_press=self.start_flood_thread) self.add_widget(start_button) text_label = Label(text="Text", size=(100, 50), pos=(100, 300)) self.add_widget(text_label) ammount_label = Label(text="Amount", size=(100, 50), pos=(100, 220)) self.add_widget(ammount_label) delay_label = Label(text="Delay", size=(100, 50), pos=(100, 140)) self.add_widget(delay_label) def start_flood_thread(self, instance): # 在中间函数内创建并启动线程 flood_thread = threading.Thread(target=self.flood) flood_thread.start() def flood(self): # 移除多余的instance参数,Thread调用target时不会自动传入按钮实例 time.sleep(5) for i in range(100): pyautogui.write("asd") pyautogui.press('enter') time.sleep(2) class Spambot(App): def build(self): return asd() Spambot().run()
关键修正点说明
- 中间函数
start_flood_thread:作为按钮点击的回调,内部负责创建Thread实例并启动线程,避免直接将Thread对象绑定到on_press。 - 移除
flood函数的instance参数:通过Thread(target=self.flood)调用时,不会自动传入按钮实例,保留该参数会导致参数不匹配错误。 - 窗口配置修正:修正
hight拼写错误为height,同时调高窗口高度确保所有标签可见。 - 按钮位置调整:将按钮移到窗口底部中间,符合常规UI布局逻辑。
内容的提问来源于stack exchange,提问作者Stewestrong
相关产品推荐
相关产品推荐

