Python - Tkinter 标签输出? [英] Python - Tkinter Label Output?

查看:38
本文介绍了Python - Tkinter 标签输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何从 Tkinter 获取我的条目,将它们连接起来,并将它们显示在下面的标签中(在输入例外:"旁边)?我只能在运行在 GUI 后面的 python 控制台中显示它们的输入.有没有办法在标签小部件中显示我的 InputExcept 变量?

from Tkinter import *主 = Tk()master.geometry('200x90')master.title('输入测试')定义用户名():usrE1 = usrE.get()usrN2 = usrN.get()InputExcept = usrE1 + " " + usrN2打印输入除外usrE = 条目(大师,救济=沉没)usrE.pack()usrN = 条目(大师,救济=沉没)usrN.pack()Btn1 = Button(文本=输入",命令=用户名)Btn1.pack()lbl = Label(text='输入除外:')lbl.pack()master.mainloop()

解决方案

要做的两个主要步骤:

  • 您需要将 usrEusrElbl 声明为回调方法中的全局变量.
  • 您需要使用

    注意:

    不要忘记指定在其上绘制标签和按钮的父小部件 (master).

    How would I take my entries from Tkinter, concatenate them, and display them in the Label below (next to 'Input Excepted: ')? I have only been able to display them input in the python console running behind the GUI. Is there a way my InputExcept variable can be shown in the Label widget?

    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.
    • You need to use config() method to update the text of 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.

    这篇关于Python - Tkinter 标签输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆