如何在tkinter窗口中添加函数结果字符串?

2024-05-06 02:52:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,正在尝试编写一个将给定字符串转换为机密代码的程序。用户在文本框中输入的字符串作为输入,并转换为密码(使用加密模块)。如何在窗口中显示结果(我尝试使用标签,但显示错误。)

from tkinter import *
import encryption as En             # Loading Custom libraries
import decryption as De
out_text = None     # Out text is the output text of message or the encryption
root = None
font_L1 = ('Verdana', 18, 'bold')   # The font of the header label
button1_font = ("Ms sans serif", 8, 'bold')
button2_font = ("Ms sans serif", 8, 'bold')
font_inst = ("Aerial", 8)
my_text = None
input_text = None
text_box = None
resut_l = None
result_2 = None

def b1_action():                       # Encryption button
    input_text = text_box.get()
    if input_text == "":
        print("Text field empty")
    else:
        En.enc_text(input_text)         # Message is returned as 'code'

def b2_action():
    input_text = text_box.get()
    if input_text == "":
        print("Text field Empty")
    else:
        De.dec_text(input_text)        

def enc_button():           # Button for rendering encryption
    b1 = Button(root, text = "ENCRYPT", font = button1_font, command = b1_action)
    b1.configure(bg = 'palegreen3', width = '10', height = '3')
    b1.place(x = '120', y = '130')

def dec_button():           # Button for decryption
    b2 = Button(root, text = "DECRYPT", font = button2_font, command = b2_action)
    b2.configure(bg = 'palegreen3', width = '10', height = '3')
    b2.place(x = '340', y = '130')

def main():                         #This is the core of GUI
    global root
    global text_box
    root = Tk()
    root.geometry("550x350")
    root.configure(bg = "MediumPurple1")
    win_text = Label(root, text = 'Enter text below and Choose an action:', bg = 'MediumPurple1', font = font_L1)
    win_text.place(x = '10', y = '50')
    text_box = Entry(root, text = 'Enter the Text', width = 60, bg = 'light blue')
    text_box.place(x = '100', y = '100')
    inst_text = Label(root, text = instructions, bg = "MediumPurple1", font = font_inst)
    inst_text.pack(side = BOTTOM)
    enc_button()
    dec_button()
    root.title('Secret Message.')
    root.mainloop() 


main()

这是加密模块

^{pr2}$

你也可以建议一些即兴表演。


Tags: thetextboxnoneinputdefbuttonaction
1条回答
网友
1楼 · 发布于 2024-05-06 02:52:36

问题的一部分是Entry小部件没有text=配置选项,因此在这一行中它被完全忽略了:

text_box = Entry(root, text='Enter the Text', width=60, bg='light blue')

处理Entry字符内容的最佳方法是使用其textvariable=选项并将其值设置为^{}的实例,然后获取并设置该对象的值将自动更新屏幕上的Entry小部件。在

下面是对它所做的更改。注意:为了能够运行代码,我对一些不相关的东西进行了注释和更改,但尝试指出了最重要的内容。还请注意,我在自定义encryption模块中的enc_text()函数的末尾添加了return code语句。在

^{pr2}$

相关问题 更多 >