我使用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()
提前感谢您的帮助,我在()之后查了一下,但这涉及到调用更多功能,而且似乎并不适用于我想要做的事情。
答案 0 :(得分:1)
你可以将阻塞代码放在它自己的线程中,允许它以编写的方式循环或阻塞。