一个非常简单的tkinter项目

时间:2017-05-30 19:08:18

标签: python python-3.x tkinter

我在论坛上发现了这段代码,我已经花了好几个小时才得到它 使其打印在另一个文本字段而不是控制台...我正在尝试创建一个加密器...提前感谢您的帮助......

from tkinter import *

def print_text():
global e
string = e.get() 
print(string)


root = Tk()

root.title('Name')

e = Entry(root)
e.pack()
e.focus_set()

b = Button(root, text='Okay', command=print_text)
b.pack(side='bottom')
root.mainloop()

2 个答案:

答案 0 :(得分:0)

您必须为该标签添加标签和文本变量:

from tkinter import *

def print_text():
    global e, l_var
    string = e.get() 
    print(string)
    l_var.set(string)


root = Tk()

root.title('Name')

e = Entry(root)
e.pack()
e.focus_set()

b = Button(root, text='Okay', command=print_text)
b.pack(side='bottom')

l_var = StringVar()
l = Label(root, textvariable=l_var)
l.pack()

root.mainloop()

答案 1 :(得分:0)

from tkinter import *

def print_text():
    # global e
    string = e.get() 
    # print(string)
    # updating the label
    l.configure(text=string)

root = Tk()
root.title('Name')

e = Entry(root)
e.pack()
e.focus_set()

b = Button(root, text='Okay', command=print_text)
b.pack(side='bottom')

# a label to display the text from Entry
l = Label(root)
l.pack(side='left')

root.mainloop()