我如何从Tkinter获取我的条目,连接它们,并在下面的标签中显示它们(在'输入例外:'旁边)?我只能在GUI后面运行的python控制台中显示它们的输入。我的InputExcept变量是否可以在Label小部件中显示?
from Tkinter import *
master = Tk()
master.geometry('200x90')
master.title('Input Test')
def UserName():
usrE1 = usrE.get()
usrN2 = usrN.get()
InputExcept = usrE1 + " " + usrN2
print InputExcept
usrE = Entry(master, relief=SUNKEN)
usrE.pack()
usrN = Entry(master, relief=SUNKEN)
usrN.pack()
Btn1 = Button(text="Input", command=UserName)
Btn1.pack()
lbl = Label(text='Input Excepted: ')
lbl.pack()
master.mainloop()
答案 0 :(得分:3)
要做的两个主要步骤:
usrE
,usrE
和lbl
作为全局变量。config()
方法更新lbl
。<强>程序:强>
以下是解决方案:
from Tkinter import *
master = Tk()
master.geometry('200x90')
master.title('Input Test')
def UserName():
global usrE
global usrN
global lbl
usrE1 = usrE.get()
usrN2 = usrN.get()
InputExcept = usrE1 + " " + usrN2
print InputExcept
lbl.config(text='User expected:'+InputExcept)
usrE = Entry(master, relief=SUNKEN)
usrE.pack()
usrN = Entry(master, relief=SUNKEN)
usrN.pack()
Btn1 = Button(master, text="Input", command=UserName)
Btn1.pack()
lbl = Label(master)
lbl.pack()
master.mainloop()
<强>演示:强>
运行上面的程序会引导您达到预期的结果:
注意:强>
不要忘记指定绘制标签和按钮的父窗口小部件(master
)。