我如何在tkinter Entry字段中获得输出

问题描述 投票:0回答:1

我正在进行自己的密码加密,我想将结果放入称为输出的输入字段中。现在我只是使用print(),所以我可以测试是否有结果。但这仅用于测试。这是我第一次使用Python,因此如果还有其他一些我可以做得更好的事情,请告诉我:)这是我到目前为止所拥有的。

from tkinter import *

#Make frame
root = Tk()
root.geometry("500x300")
root.title("Encryption Tool")

top_frame = Frame(root)
bottom_frame = Frame(root)

top_frame.pack()
bottom_frame.pack()

#Text
headline = Label(top_frame, text="Encryption Tool", fg='black')
headline.config(font=('Courier', 27))
headline.grid(padx=10, pady=10)

Key = Label(bottom_frame, text="Key:", fg='black')
Key.config(font=('Courier', 20))
Key.grid(row=1)

Text_entry = Label(bottom_frame, text="Text:", fg='black')
Text_entry.config(font=('Courier', 20))
Text_entry.grid(row=2)

Output_text = Label(bottom_frame, text="Output:", fg='black')
Output_text.config(font=('Courier', 20))
Output_text.grid(row=3)

Key_entry = Entry(bottom_frame)
Key_entry.grid(row=1, column=1)

Text_entry = Entry(bottom_frame)
Text_entry.grid(row=2, column=1)

Output_text = Entry(bottom_frame)
Output_text.grid(row=3, column=1)

#Encryption_button

def encrypt():
    result = ''
    text = ''

    key = Key_entry.get()
    text = Text_entry.get()
    formule = int(key)

    for i in range(0, len(text)):
            result = result + chr(ord(text[i]) + formule + i * i)


    result = ''                                     

Encryption_button = Button(bottom_frame, text="Encrypt", fg='black')
Encryption_button.config(height = 2, width = 15)
Encryption_button.grid(row = 4, column = 0, sticky = S)
Encryption_button['command'] = encrypt


#Decryption_button

def decrypt():
    result = ''
    text = ''

    key = Key_entry.get()
    text = Text_entry.get()
    formule = int(key)

    for i in range(0, len(text)):
            result = result + chr(ord(text[i]) - formule - i * i)

    print(result)
    result = ''

Decryption_button = Button(bottom_frame, text="Decrypt", fg="black")
Decryption_button.config(height = 2, width = 15)
Decryption_button.grid(row = 5, column = 0, sticky = S)
Decryption_button['command'] = decrypt

#Quit_button
def end():
    exit()

Quit_button = Button(bottom_frame, text="Quit", fg='black')
Quit_button.config(height = 2, width = 15)
Quit_button.grid(row = 6, column = 0, sticky = S)

Quit_button['command'] = end

root.mainloop()
python python-3.x encryption tkinter output
1个回答
0
投票

使用tkinter进行此操作的最常见方法是使用StringVar()对象,您可以将其连接到Entry对象(此处为documentation)。

output_entry_value = StringVar()

Output_text = Entry(bottom_frame, textvariable=output_entry_value)
Output_text.grid(row=3, column=1)

然后您可以在stringvar中对结果进行.set(),它将在您将其连接到的条目中更新:

output_entry_value.set(result)
© www.soinside.com 2019 - 2024. All rights reserved.