在使用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 == "":
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()
##1、解决方案
您可以将阻塞代码放入它自己的线程中,以便它可以根据需要循环或阻塞。
下面的代码实例演示了如何使用Tkinter和多线程在不冻结窗口的情况下执行此操作:
import tkinter as tk
import threading
# Create the main window
root = tk.Tk()
# Create the input and output fields
input_field = tk.Entry(root)
output_field = tk.Text(root)
# Create the function that will run in the thread
def long_running_task():
# Simulate a long-running task by sleeping for 5 seconds
time.sleep(5)
# Update the output field with the result of the task
output_field.insert(tk.END, "Task completed\n")
# Create the thread and start it
thread = threading.Thread(target=long_running_task)
thread.start()
# Start the main event loop
root.mainloop()
此代码将创建一个GUI,其中包含一个输入字段、一个输出字段和一个“运行任务”按钮。当用户单击按钮时,该任务将启动,并将在单独的线程中运行,而GUI仍将对外界事件做出响应。
您还可以使用Tkinter的after()
方法在不冻结窗口的情况下执行计划任务。例如,以下代码将创建一个GUI,其中包含一个输入字段、一个输出字段和一个“运行任务”按钮。当用户单击按钮时,该任务将启动,并将每秒更新一次输出字段。
import tkinter as tk
# Create the main window
root = tk.Tk()
# Create the input and output fields
input_field = tk.Entry(root)
output_field = tk.Text(root)
# Create the function that will run the task
def long_running_task():
# Simulate a long-running task by sleeping for 1 second
time.sleep(1)
# Update the output field with the result of the task
output_field.insert(tk.END, "Task completed\n")
# Schedule the task to run again after 1 second
root.after(1