我正在尝试在Python中为我的编码程序创建一个GUI。问题是它似乎并不能同时发挥作用。我是使用Python的初学者,所以我不确定一切是如何运作的。我已经在互联网上寻找解决方案,但我一无所获。
我到目前为止的程序是
def program (str,my_fn):
global i
i=0
while i<len(str):
my_fn()
i += 1
def encrypt(my_result,str,number=0):
my_result.append(ord(str[i])-number)
def decrypt(my_result,str,number=0):
my_result.append(chr(str[i]+number))
def password_generator():
password = input("What would the password be:\n")
numerical_password=[]
program(password,partial(encrypt,numerical_password,password))
global code
code = sum(numerical_password)
while choice != "Exit":
choice = input("Do you want to Encrypt, Decrypt, or Exit?\n")
if choice == "Encrypt": #Encrypt
option = input("Do you want a password?(Yes or No)\n")
if option == "Yes":
password_generator()
else:
code = 0
answer = input("What would you like to encrypt:\n")
message = []
program(answer,partial(encrypt,message,answer,code))
print (message)
if choice == "Decrypt": #Decrypt
option = input("Do you have a password?(Yes or No)\n")
code= 0
if option == "Yes":
password_generator()
else:
code = 0
answer = [int(x) for x in input("(Please insert ONLY intergers seperated with commas)\nWhat would you like to decrypt:\n").split(",")]
text =[]
program(answer, partial(decrypt,text,answer,code))
print("".join(text))
print("ThAnK yOu foR uSiNg eNcodER. HAve a nICe dAY!")
到目前为止,我已经想出了如何向用户界面添加一些按钮。我计划稍后添加一个显示和一个文本输入框,但现在我只想专注于链接GUI和程序。
root = Tk()
root.title("Note Taker")
root.geometry("250x250")
app = Frame(root)
app.grid()
def Button1():
choice =="Encrypted"
button1 = Button(root, text="button1", command=Button1)
button1.grid()
root.mainloop()
我的目标是按下Button1时,它将允许用户加密他们想要的字符串。
非常感谢!
答案 0 :(得分:0)
从技术上讲,对于同时执行,应该使用threads,但这不适合您的代码。我建议将代码的主体转换为函数。这应该很简单。而不是:
while choice != "Exit":
choice = input("Do you want to Encrypt, Decrypt, or Exit?\n")
...
写
def make_choice(choice = None):
if choice is None:
choice = input("Do you want to Encrypt, Decrypt, or Exit?\n")
...
这应该可以解决你的问题;只需将choice == "Encrypted"
替换为make_choice("Encrypted")
或您需要的任何选项。希望这有帮助!