1 回答
TA贡献1868条经验 获得超4个赞
首先:gui 不会崩溃 - 它工作正常并且在主循环中它执行您正在编写的命令。它不会刷新,因为它打印输出而不是刷新 UI。首先请注意,在创建 UI 时需要记住的是,您应该省略主线程中不必要的工作。可能的解决方案:将工作移至单独的线程并在那里运行,例如:
import time
import tkinter as tk
from pynput.keyboard import Controller as KeyboardController
import threading
keyboard = KeyboardController()
def copy(a):
def print_test(a):
pause_input=2
pause_input2=2
pause_input3=0.5
for _ in range(a):
time.sleep(pause_input)
keyboard.press('t')
keyboard.release('t')
time.sleep(pause_input3)
keyboard.press('e')
keyboard.release('e')
time.sleep(pause_input3)
keyboard.press('s')
keyboard.release('s')
keyboard.press('t')
keyboard.release('t')
print("Wrote test")
t = threading.Thread(target=print_test, args=(a,))
t.start()
root = tk.Tk()
root.title("GUI Button")
btn1 = tk.Button(root, text="Button 1", command=lambda : copy(360))
btn1.pack()
root.mainloop()
添加回答
举报