我試圖制作一個(gè)改進(jìn)的自動(dòng)答題器,具有更多功能,例如:按一個(gè)鍵保存光標(biāo)位置按鍵時(shí)執(zhí)行按一下鍵殺死程序這是代碼:import win32api, win32comimport pyautoguiimport tkinter as tkfrom time import sleepimport keyboardwin = tk.Tk()cursor = (0,0)di = 0e = tk.Entry()def geti(): di = float(e.get())l = tk.Label(text="Info:").pack()l1 = tk.Label(text="Press q to stop clicking").pack()l2 = tk.Label(text="Press c to copy mouse position to execute on").pack()l3 = tk.Label(text="Press e to start executing").pack()dl = tk.Label(text="Delay between eack click (s)").pack()e.pack()b = tk.Button(text="save delay time",command=geti).pack()def click(x, y): win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) sleep(di) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)while True: if(keyboard.is_pressed('c') == True): cursor = pyautogui.position() if(keyboard.is_pressed('q') == True): break if(keyboard.is_pressed('e') == True): click(cursor[0], cursor[1]) win.mainloop()
1 回答

精慕HU
TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊
你的 while 循環(huán)會(huì)阻塞 win.mainloop()。您可以使用帶有綁定的 tkinters 事件系統(tǒng),而不是檢查按鍵:
win.bind('key', callback)
# thanks to @acw1668's comment: added evt
win.bind('e', lambda evt: click(cursor[0], cursor[1]))
lambda 是必要的,因?yàn)槟M{(diào)用函數(shù)而不是結(jié)果。來(lái)自tkinter 文檔:
如果這樣做,Python 將在創(chuàng)建小部件之前調(diào)用回調(diào)函數(shù),并將函數(shù)的返回值傳遞給 Tkinter。然后,Tkinter 嘗試將返回值轉(zhuǎn)換為字符串,并告訴 Tk 在激活按鈕時(shí)調(diào)用具有該名稱的函數(shù)。這可能不是您想要的。
添加回答
舉報(bào)
0/150
提交
取消