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

时间:2017-10-07 17:19:57

标签: python python-3.x tkinter

我是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()

这是加密模块

def enc_text(line):
    msg = str(line).replace(' ', '_').lower()
    msg_list = list(msg)
    all_char = list("abcdefghijklmnopqrstuvwxyzabc_!?@")

    for i in range(0, len(msg)):
        pos_replaced = all_char.index(str(msg_list[i])) #will give the positon of the word to be replaced in the main list of alphabets
        msg_list.insert(i, all_char[pos_replaced + 3])  #will replace the elements one by one
        msg_list.pop(i + 1)
        i += 1

    code = ''.join(msg_list).replace('@', ' ')
    print(code)

你也可以建议一些即兴创作。

1 个答案:

答案 0 :(得分:0)

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

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

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

以下是您的代码,并对其进行了更改。注意我评论并更改了一些不相关的东西以便能够运行代码,但试图指出最重要的代码。另请注意,我在自定义return code模块的enc_text()函数的最后添加了encryption语句。

from tkinter import *
import encryption as En             # Loading Custom libraries
#import decryption as De            # DON'T HAVE THIS.

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_var = None                     # ADDED.
text_box = None
resut_l = None
result_2 = None

# CHANGED TO USE NEW "text_var" variable.
def b1_action():                       # Encryption button
    input_text = text_var.get()
    if input_text == "":
        print("Text field empty")
    else:
        text_var.set(En.enc_text(input_text))

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
    global text_var                 # ADDED
    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_var = StringVar()          # ADDED
    text_var.set('Enter the Text')  # ADDED
    # CHANGED text='Enter the Text' to textvariable=text_var
    text_box = Entry(root, textvariable=text_var, 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()
相关问题