[英]TypeError : start() takes 1 positional argument but 2 were given
以下程序应该在按住“Ctrl”键时自动单击并在松开键时停止并完全停止是 tkinter window 关闭。 到目前为止,一切都很好。
import keyboard
import pyautogui
import threading
from tkinter import *
from tkinter import ttk
def convert_to_pause2(clicks_per_second2):
clicks_per_second2 = min(clicks_per_second2, 20)
return 1 / clicks_per_second2
def repeat_function2(kill_event2, int2):
while not kill_event2.is_set():
pyautogui.PAUSE = convert_to_pause2(int2.get())
pyautogui.click()
def hotkey_thread_loop2(int2):
while True:
kill_event2 = threading.Event()
new_thread2 = threading.Thread(target=lambda: repeat_function2(kill_event2, int2))
keyboard.on_press_key('ctrl',new_thread2.start)
keyboard.on_release_key('ctrl',kill_event2.set())
keyboard.wait('ctrl', keyboard.KEY_UP)
keyboard.unhook_all()
# Start the main program
if __name__ == "__main__":
root = Tk()
root.geometry('500x400')
bovar2 = IntVar()
scale2 = Scale(root,orient=HORIZONTAL, from_=0,to=20,variable=bovar2)
scale2.pack(side=RIGHT)
hotkey_thread2 = threading.Thread(target=lambda: hotkey_thread_loop2(bovar2),
daemon=True)
hotkey_thread2.start()
root.mainloop()
当我按住 ctrl 键启动程序时,它会显示以下错误:
Exception in thread Thread-8:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.8/dist-packages/keyboard/_generic.py", line 58, in process
if self.pre_process_event(event):
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 213, in pre_process_event
key_hook(event)
File "/usr/local/lib/python3.8/dist-packages/keyboard/__init__.py", line 510, in <lambda>
return hook_key(key, lambda e: e.event_type == KEY_UP or callback(e), suppress=suppress)
TypeError: start() takes 1 positional argument but 2 were given
但是我对鼠标模块做了同样的事情,没有问题,所以我知道出了什么问题
我这样做了: lambda *a:new_thread2.start()
并且它消除了错误,但是现在您遇到了一个新问题,您不能多次启动线程,并且您的循环将启动它远不止于此。
有什么原因导致这不正是您想要做的吗? 似乎您的所有线程都只是在尝试侦听并等待按键。 Tkinter 已经这样做了,而且它比您当前的方法更简洁。
import pyautogui, tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.geometry('500x400')
self.bovar = tk.IntVar()
self.bind('<Control_L>', self._handler)
#self.bind('<Control_R>', self._handler) #if you want both Control keys
#you can't start scale at 0 if you intend for it to be a divisor
scale = tk.Scale(self, orient=tk.HORIZONTAL, from_=1, to=20, variable=self.bovar)
scale.pack(side=tk.RIGHT)
def _handler(self, event):
#the scale only goes to 20 so your min was not necessary
pyautogui.PAUSE = 1/self.bovar.get()
pyautogui.click()
if __name__ == "__main__":
App().mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.