更改标签文字的问题

时间:2012-03-26 09:47:51

标签: python button tkinter label

请查看以下代码。 我是Python的新手(1周和数量)所以仍然在学习曲线上。 使用按钮单击 - 驱动定义 - 尝试更改标签文本。 GUI由三个按钮组成: Variable1(按下时将变量更改为Variable1) 变量2(按下时将变量更改为变量2) 打印变量(按下时打印变量)

我尝试过各种各样的东西让标签文字更新 - 无济于事。 我确实尝试了全局变量,但我无法工作。

非常感谢任何帮助。

from Tkinter import *
import time

class GridDemo( Frame ):
    def changevar1 (self): #change variable 
        global variable
        self.variable = "Variable1"
        print "You have changed the variable to:" , self.variable
        time.sleep(0.5)

    def changevar2 (self): #change variable
        global variable
        self.variable = "Variable2"
        print "You have changed the variable to:" , self.variable
        time.sleep(0.5)

    def printvar (self):  # print variable
        print "The variable is:" , self.variable
        print ""
        time.sleep(0.5)

    def __init__( self ):
        Frame.__init__( self )
        self.master.title( "Grid Demo" )
        global variable
        self.variable = "Start Variable"

        self.master.rowconfigure( 0, weight = 1 )
        self.master.columnconfigure( 0, weight = 1 )
        self.grid( sticky = W+E+N+S )

        self.button1 = Button( self, text = "Variable 1", command = self.changevar1 )
        self.button1.grid( row = 1, column = 1,  sticky = W+E+N+S )

        self.button2 = Button( self, text = "Variable 2", command = self.changevar2 )
        self.button2.grid( row = 1, column = 2, sticky = W+E+N+S )

        self.button3 = Button( self, text = "print variable" , command = self.printvar)
        self.button3.grid( row = 1, column = 3, sticky = W+E+N+S )

        self.label4 = Label(self, text = self.variable)
        self.label4.grid( row = 2, column = 1, columnspan = 2, sticky = W+E+N+S )

        self.rowconfigure( 1, weight = 1 )
        self.columnconfigure( 1, weight = 1 )

def main():
    GridDemo().mainloop() 

2 个答案:

答案 0 :(得分:2)

使用 StringVar textvariable 。这是工作代码,我评论了我改变的内容。

from Tkinter import *
import time

class GridDemo( Frame ):
    def changevar1 (self): #change variable 
        self.label4String.set("Variable1") # use set method to change
        print "You have changed the variable to:" , self.label4String.get()
        time.sleep(0.5)

    def changevar2 (self): #change variable
        self.label4String.set("Variable2") # use set method to change
        print "You have changed the variable to:" , self.label4String.get()
        time.sleep(0.5)

    def printvar (self):  # print variable
        print "The variable is:" ,  self.label4String.get()
        print # just this is enough, no need to add ""
        time.sleep(0.5)

    def __init__( self ):
        Frame.__init__( self )
        self.master.title( "Grid Demo" )
        self.variable = "Start Variable"

        self.master.rowconfigure( 0, weight = 1 )
        self.master.columnconfigure( 0, weight = 1 )
        self.grid( sticky = W+E+N+S )

        self.button1 = Button( self, text = "Variable 1", command = self.changevar1 )
        self.button1.grid( row = 1, column = 1,  sticky = W+E+N+S )

        self.button2 = Button( self, text = "Variable 2", command = self.changevar2 )
        self.button2.grid( row = 1, column = 2, sticky = W+E+N+S )

        self.button3 = Button( self, text = "print variable" , command = self.printvar)
        self.button3.grid( row = 1, column = 3, sticky = W+E+N+S )

        self.label4String = StringVar() # use Tk's StringVar
        self.label4 = Label(self, textvariable=self.label4String) # bind a StringVar to textvariable attr
        self.label4.grid( row = 2, column = 1, columnspan = 2, sticky = W+E+N+S )

        self.rowconfigure( 1, weight = 1 )
        self.columnconfigure( 1, weight = 1 )

def main():
    GridDemo().mainloop() 

if __name__ == '__main__':
    main()

答案 1 :(得分:2)

您可以将变量与窗口小部件相关联,但您也可以使用configure方法将窗口小部件的文本(或任何属性)关联起来。例如:

self.label4.configure(text="hello, world!")

您可以定义按钮,以便它们发送相应的字符串。例如:

定义几个按钮:

self.button1 = Button(frame, text="Variable 1", 
                      command=lambda message="Variable 1": self.changevar(message))
self.button2 = Button(frame, text="Variable 2", 
                      command=lambda message="Variable 2": self.changevar(message))

定义回调:

def changevar(self, message):
    self.label4.configure(text=message)

顺便说一句,我强烈建议使用from Tkinter import * not 。这是一个不好的做法。相反,导入Tk并完全限定您的功能。例如:

import Tkinter as tk
...
frame = tk.Frame(...)

从任何模块执行import *都很糟糕。从Tkinter做起,对于现代版本的python来说尤其糟糕。 Tkinter在模块ttk中有所谓的“主题”小部件。它们与普通小部件具有相同的类名 - 有一个Tkinter.Button和一个ttk.Button。

执行import *会阻止您在同一文件中轻松使用它们。对于任何复杂的应用程序,您将希望混合和匹配以获得您想要的外观。如果您使用首选导入样式,则可以执行tk.Label(...)ttk.Label(...),并且您的代码将变得清晰,您正在使用哪个窗口小部件集。