Как получить вывод в поле ввода tkinter - PullRequest
1 голос
/ 28 октября 2019

Я создаю свое собственное шифровальное шифрование, и я хочу поместить результат в поле ввода, называемое 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()

1 Ответ

1 голос
/ 28 октября 2019

Наиболее распространенный способ сделать это с помощью tkinter - это объект StringVar (), который вы можете подключить к объекту Entry (некоторые документация здесь).

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...