【发布时间】:2015-03-31 16:20:05
【问题描述】:
我已经为一个使用 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() 但这涉及调用更多函数并且似乎不太适用于我正在尝试做的事情。
【问题讨论】:
-
创建一个带有条目和按钮的对话框,然后锁定焦点直到按下按钮。
-
我刚刚发现 this 也可以。感谢您的帮助-老实说,我忘记了 while 循环可能会被破坏。