【问题标题】:How do I get a output into a tkinter Entry field如何将输出输入到 tkinter 输入字段
【发布时间】:2020-02-23 06:50:20
【问题描述】:

我正在制作自己的密码加密,我想将结果放入名为 output 的输入字段中。现在我只是使用 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()

【问题讨论】:

  • 为什么要使用Entry 小部件进行输出?似乎Text 小部件更合适。

标签: python python-3.x encryption tkinter output


【解决方案1】:

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

output_entry_value = StringVar()

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

然后你可以 .set() 在 stringvar 中的结果,它会在你连接到的条目中更新:

output_entry_value.set(result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2017-03-30
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多