为什么当我尝试使用键盘模块时,tkinter会崩溃?

huangapple go评论64阅读模式
英文:

Why does tkinter crash when i try using the keyboard module?

问题

Here is the translated code part:

# 导入模块
import tkinter as tk
import keyboard as kb

# 设置
root = tk.Tk()
root.title("Autospam by previous")
root.geometry("400x400")
root.resizable(False, False)
root.configure(bg="black")
stopspam = False

# 函数
def start():
    stopspam = False
    while True:
        kb.write(entry1.get(), 0.1)  # 0.1秒延迟可以防止严重卡顿
        if stopspam is True:
            break

def stop():
    stopspam = True

# 设计
label1 = tk.Label(root, text="Autospam", font=("Verdana", 30, "bold"), bg="black", fg="white")
label1.place(relx=0.2, rely=0)

label2 = tk.Label(root, text="Made by previous", font=("Verdana", 15, "italic"), bg="black", fg="white")
label2.place(relx=0.25, rely=0.15)

label3 = tk.Label(root, text="text:", font=("Verdana", 15), bg="black", fg="white")
label3.place(relx=0.41, rely=0.3)

entry1 = tk.Entry(root, font=("Verdana", 15, "bold"))
entry1.place(relx=0.125, rely=0.4)

label4 = tk.Label(root, text="tip: 不要使用大段的文本。", font=("Verdana", 15), bg="black", fg="white")
label4.place(relx=0.05, rely=0.5)

button1 = tk.Button(root, text="Spam", font=("Verdana", 15, "bold"), command=start)
button1.place(relx=0.125, rely=0.8)

button2 = tk.Button(root, text="Stop", font=("Verdana", 15, "bold"), command=stop)
button2.place(relx=0.525, rely=0.8)

# 主循环
root.mainloop()

Please note that I've translated the code part as requested, and you may need to review it for any potential errors or improvements.

英文:

Whenever i try to make the keyboard module type something when a tkinter button is clicked, it starts lagging a lot, resulting in the tkinter window getting the "application not responding" error.

What i tried to make is a program that spams anything you write in the textbox after you click the "spam" button.

Here is my python code:

# imports
import tkinter as tk
import keyboard as kb
# setup
root = tk.Tk()
root.title("Autospam by previous")
root.geometry("400x400")
root.resizable(False, False)
root.configure(bg="black")
stopspam = False
# functions
def start():
stopspam = False
while True:
kb.write(entry1.get(), 0.1)  # 0.1 delay prevents massive lag
if stopspam is True:
break
def stop():
stopspam = True
# design
label1 = tk.Label(root, text="Autospam", font=("Verdana", 30, "bold"), bg="black", fg="white")
label1.place(relx=0.2, rely=0)
label2 = tk.Label(root, text="Made by previous", font=("Verdana", 15, "italic"), bg="black", fg="white")
label2.place(relx=0.25, rely=0.15)
label3 = tk.Label(root, text="text:", font=("Verdana", 15), bg="black", fg="white")
label3.place(relx=0.41, rely=0.3)
entry1 = tk.Entry(root, font=("Verdana", 15, "bold"))
entry1.place(relx=0.125, rely=0.4)
label4 = tk.Label(root, text="tip: don't use large chunks of text.", font=("Verdana", 15), bg="black", fg="white")
label4.place(relx=0.05, rely=0.5)
button1 = tk.Button(root, text="Spam", font=("Verdana", 15, "bold"), command=start)
button1.place(relx=0.125, rely=0.8)
button2 = tk.Button(root, text="Stop", font=("Verdana", 15, "bold"), command=stop)
button2.place(relx=0.525, rely=0.8)
# mainloop
root.mainloop()

If there are any other mistakes or errors i've missed, feel free to say them!

Any help will be appreciated.

答案1

得分: 2

Your program is single-threaded, so the while True loop in start() causes the root.mainloop() to not run, thus the window does not respond. To fix this, we'll use a recursive function defined with tkinter's built-in function .after().

# 函数
def start():
    global stopspam
    stopspam = False
    toWrite = entry1.get()
    
    loop(toWrite)

def loop(text):
    if not stopspam:
        kb.write(text, 0.1)
        root.after(10, loop, text) # 10 毫秒后再次运行

def stop():
    global stopspam
    stopspam = True

The global declarations are required because you are modifying a variable inside of a method. Notice how it's not necessary inside of the loop method.

Your program was also going to be very slow because you were running entry1.get() in every kb.write instance. So it would exponentially get slower and slower, since the keyboard.write() is slow once strings get large. I modified the code to only write your initial string, which is probably what you were intending in the first place.

Hope this helps!

英文:

Your program is single-threaded, so the while True loop in start() causes the root.mainloop() to not run, thus the window does not respond. To fix this, we'll use a recursive function defined with tkinter's built-in function .after().

# functions
def start():
global stopspam
stopspam = False
toWrite = entry1.get()
loop(toWrite)
def loop(text):
if not stopspam:
kb.write(text, 0.1)
root.after(10, loop, text) #run again after 10 milliseconds
def stop():
global stopspam
stopspam = True

The global declarations are required because you are modifying a variable inside of a method. Notice how it's not necessary inside of the loop method.

Your program was also going to be very slow because you were running entry1.get() in every kb.write instance. So it would exponentially get slower and slower, since the keyboard.write() is slow once strings get large. I modified the code to only write your initial string, which is probably what you were intending in the first place.

Hope this helps!

huangapple
  • 本文由 发表于 2023年5月24日 19:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定