Python- 如何在没有 window 冻结的情况下让函数等待或在 tkinter 中使用 while 循环
Python- How to make a function wait or use a while loop in tkinter without the window freezing
我已经使用 Tkinter 为一个简单的加密程序设置了一个 GUI,它使用 Entry() 框作为密钥和输入。我正在尝试的是,如果他们将密钥框留空并点击加密,则强制用户重新输入密钥。以前,该程序只会为他们生成一个随机密钥,并使用 messagebox.showinfo 显示它,并使用 Key_Box.insert(0, Key)
将其放入密钥框中
我现在使用的是重定向,Key_Box.focus() 和一个 messagebox.showinfo 告诉用户他们还没有输入密钥。
问题是在这一点之后我无法阻止该功能继续运行,它只是继续运行。当整个程序都是基于文本的时候,我可以只输入
while Key == "":
Key = input("Input a new key:")
但是 Tkinter 使用 while 循环或 time.sleep(n) 会阻止程序响应(可以理解,因为它都在循环中运行)。
那么,我怎样才能让程序做到这一点-
psuedo- if Key == "":
cursor at Key_Box
display ok button
until OK is pressed:
repeat
else:
key = Keybox.get()
我的其余代码如下所示-
注意 - 它需要清理,或者 Tkinter 总是看起来有点乱
import time, random
from tkinter import *
root = Tk()
##Encrypt and Decrypt
Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?@[\]^_`{|}~r\x0b\x0c"
def Encrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) + Master_Key.index(Key[i])
if Ref_For_Output >= len(Master_Key):
Ref_For_Output -= len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
def Decrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) - Master_Key.index(Key[i])
if Ref_For_Output < 0:
Ref_For_Output += len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
def Compatibility(User_Input, Key):
if Key == "":
## while len(Key) < 5:
## Key += Master_Key[random.randint(0, (len(Master_Key)-1))]
## Key_Box.insert(0, Key)
## messagebox.showinfo(title="Error!", message="Your key cannot be blank, your new randomly generated key is: \n" + Key)
Key_Box.focus()
Temp = 0
while len(Key) < len(User_Input):
Key += (Key[Temp])
Temp += 1
return Key
##Layout
root.title("A451 CAM2")
#root.geometry("300x100")- Window will resize as I add to the Output_Box
##Input label
Label1 = Label(root, text="Input: ")
Label1.grid(row=0, column=0, padx=10)
##Key label
Label2 = Label(root, text="Key: ")
Label2.grid(row=1, column=0, padx=10)
##Output label
Label3 = Label(root, text="Output: ")
Label3.grid(row=2, column=0, padx=10)
##Input entry box
Input_Box = Entry(root, bg="grey60")
Input_Box.grid(row=0, column=1)
#Key entry box
Key_Box = Entry(root, bg="grey60")
Key_Box.grid(row=1, column=1)
##The Output box
Output_Box = Text(root, height=1, width=15)
Output_Box.grid(row=2, column=1, rowspan=2)
##Encrypt button action- Manages setting input, checking the key, changing the Encrypt button, showing a message, changing output box, and adding to clipboard
def Encrypt_Button_Press():
User_Input = Input_Box.get()
Key = Compatibility(User_Input, Key_Box.get())
root.clipboard_append(Encrypt(User_Input, Key))
Encrypt_Button.configure(text="Encrypting")
Encrypt_Button.configure(text="Encrypt")
Output_Box.insert(INSERT, Encrypt(User_Input, Key) + "\n")
New_Height = Output_Box.cget("height") + 1
Output_Box.configure(bg="green4", height=New_Height)
messagebox.showinfo("Complete", "Your encrypted text is: \n" + Encrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
##Decrypt button action- Manages setting input, checking the key, changing the Decrypt button, showing a message, changing output box, and adding to clipboard
def Decrypt_Button_Press():
User_Input = Input_Box.get()
Key = Key = Compatibility(User_Input, Key_Box.get())
root.clipboard_append(Decrypt(User_Input, Key))
Decrypt_Button.configure(text="Decrypting")
Decrypt_Button.configure(text="Decrypt")
Output_Box.insert(INSERT, Encrypt(User_Input, Key) + "\n")
New_Height = Output_Box.cget("height") + 1
Output_Box.configure(bg="green4", height=New_Height)
messagebox.showinfo("Complete", "Your Decrypted text is: \n" + Decrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
##The Clear button action
def Clear_All():
Input_Box.delete(0,END)
Key_Box.delete(0, END)
Output_Box.delete(1.0, END)
Output_Box.configure(bg="grey60", height=1)
##The Encrypt button
Encrypt_Button = Button(text="Encrypt", command=Encrypt_Button_Press, width=10, bg="green")
Encrypt_Button.grid(row=0, column=3, padx=10)
##The Decrypt button
Decrypt_Button = Button(text="Decrypt", command=Decrypt_Button_Press, width=10, bg="orange")
Decrypt_Button.grid(row=1, column = 3, padx=10)
##The clear button
Clear_Button = Button(text="Clear", command=Clear_All, bg="red", width=10)
Clear_Button.grid(row=2, column=3)
root.mainloop()
预先感谢您的帮助,我查看了 after() 但这涉及调用更多函数并且似乎不太适用于我正在尝试做的事情。
您可以将阻塞代码放在它自己的线程中,允许它按照编写的方式循环或阻塞。
我已经使用 Tkinter 为一个简单的加密程序设置了一个 GUI,它使用 Entry() 框作为密钥和输入。我正在尝试的是,如果他们将密钥框留空并点击加密,则强制用户重新输入密钥。以前,该程序只会为他们生成一个随机密钥,并使用 messagebox.showinfo 显示它,并使用 Key_Box.insert(0, Key)
将其放入密钥框中我现在使用的是重定向,Key_Box.focus() 和一个 messagebox.showinfo 告诉用户他们还没有输入密钥。 问题是在这一点之后我无法阻止该功能继续运行,它只是继续运行。当整个程序都是基于文本的时候,我可以只输入
while Key == "":
Key = input("Input a new key:")
但是 Tkinter 使用 while 循环或 time.sleep(n) 会阻止程序响应(可以理解,因为它都在循环中运行)。
那么,我怎样才能让程序做到这一点-
psuedo- if Key == "":
cursor at Key_Box
display ok button
until OK is pressed:
repeat
else:
key = Keybox.get()
我的其余代码如下所示- 注意 - 它需要清理,或者 Tkinter 总是看起来有点乱
import time, random
from tkinter import *
root = Tk()
##Encrypt and Decrypt
Master_Key = "0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#£$%&'()*+,-./:;?@[\]^_`{|}~r\x0b\x0c"
def Encrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) + Master_Key.index(Key[i])
if Ref_For_Output >= len(Master_Key):
Ref_For_Output -= len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
def Decrypt(User_Input, Key):
Output = ""
for i in range(len(User_Input)):
Ref_For_Output = Master_Key.index(User_Input[i]) - Master_Key.index(Key[i])
if Ref_For_Output < 0:
Ref_For_Output += len(Master_Key)
Output += Master_Key[Ref_For_Output]
return Output
def Compatibility(User_Input, Key):
if Key == "":
## while len(Key) < 5:
## Key += Master_Key[random.randint(0, (len(Master_Key)-1))]
## Key_Box.insert(0, Key)
## messagebox.showinfo(title="Error!", message="Your key cannot be blank, your new randomly generated key is: \n" + Key)
Key_Box.focus()
Temp = 0
while len(Key) < len(User_Input):
Key += (Key[Temp])
Temp += 1
return Key
##Layout
root.title("A451 CAM2")
#root.geometry("300x100")- Window will resize as I add to the Output_Box
##Input label
Label1 = Label(root, text="Input: ")
Label1.grid(row=0, column=0, padx=10)
##Key label
Label2 = Label(root, text="Key: ")
Label2.grid(row=1, column=0, padx=10)
##Output label
Label3 = Label(root, text="Output: ")
Label3.grid(row=2, column=0, padx=10)
##Input entry box
Input_Box = Entry(root, bg="grey60")
Input_Box.grid(row=0, column=1)
#Key entry box
Key_Box = Entry(root, bg="grey60")
Key_Box.grid(row=1, column=1)
##The Output box
Output_Box = Text(root, height=1, width=15)
Output_Box.grid(row=2, column=1, rowspan=2)
##Encrypt button action- Manages setting input, checking the key, changing the Encrypt button, showing a message, changing output box, and adding to clipboard
def Encrypt_Button_Press():
User_Input = Input_Box.get()
Key = Compatibility(User_Input, Key_Box.get())
root.clipboard_append(Encrypt(User_Input, Key))
Encrypt_Button.configure(text="Encrypting")
Encrypt_Button.configure(text="Encrypt")
Output_Box.insert(INSERT, Encrypt(User_Input, Key) + "\n")
New_Height = Output_Box.cget("height") + 1
Output_Box.configure(bg="green4", height=New_Height)
messagebox.showinfo("Complete", "Your encrypted text is: \n" + Encrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
##Decrypt button action- Manages setting input, checking the key, changing the Decrypt button, showing a message, changing output box, and adding to clipboard
def Decrypt_Button_Press():
User_Input = Input_Box.get()
Key = Key = Compatibility(User_Input, Key_Box.get())
root.clipboard_append(Decrypt(User_Input, Key))
Decrypt_Button.configure(text="Decrypting")
Decrypt_Button.configure(text="Decrypt")
Output_Box.insert(INSERT, Encrypt(User_Input, Key) + "\n")
New_Height = Output_Box.cget("height") + 1
Output_Box.configure(bg="green4", height=New_Height)
messagebox.showinfo("Complete", "Your Decrypted text is: \n" + Decrypt(User_Input, Key) + "\n The text has been added to your clipboard.")
##The Clear button action
def Clear_All():
Input_Box.delete(0,END)
Key_Box.delete(0, END)
Output_Box.delete(1.0, END)
Output_Box.configure(bg="grey60", height=1)
##The Encrypt button
Encrypt_Button = Button(text="Encrypt", command=Encrypt_Button_Press, width=10, bg="green")
Encrypt_Button.grid(row=0, column=3, padx=10)
##The Decrypt button
Decrypt_Button = Button(text="Decrypt", command=Decrypt_Button_Press, width=10, bg="orange")
Decrypt_Button.grid(row=1, column = 3, padx=10)
##The clear button
Clear_Button = Button(text="Clear", command=Clear_All, bg="red", width=10)
Clear_Button.grid(row=2, column=3)
root.mainloop()
预先感谢您的帮助,我查看了 after() 但这涉及调用更多函数并且似乎不太适用于我正在尝试做的事情。
您可以将阻塞代码放在它自己的线程中,允许它按照编写的方式循环或阻塞。