简体   繁体   English

Python - Tkinter标签输出?

[英]Python - Tkinter Label Output?

How would I take my entries from Tkinter, concatenate them, and display them in the Label below (next to 'Input Excepted: ')? 我如何从Tkinter获取我的条目,连接它们,并在下面的标签中显示它们(在“输入异常:”旁边)? I have only been able to display them input in the python console running behind the GUI. 我只能在GUI后面运行的python控制台中显示它们的输入。 Is there a way my InputExcept variable can be shown in the Label widget? 我的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()

Two main steps to do: 要做的两个主要步骤:

  • You need to declare usrE , usrE and lbl as global variables inside your callback method. 您需要在回调方法usrEusrElbl声明为全局变量。
  • You need to use config() method to update the text of lbl . 您需要使用config()方法来更新lbl的文本。

Program: 程序:

Here is the solution: 这是解决方案:

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

Demo: 演示:

Running the program above will lead you to the expected result: 运行上面的程序将引导您达到预期的结果:

在此输入图像描述

Note: 注意:

Do not forget to specify the parent widget ( master ) on which you draw the label and the button. 不要忘记指定绘制标签和按钮的父窗口小部件( master窗口)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM