请看下面的代码。 我是 Python 的新手(1 周并且还在增加),所以仍在学习曲线上。 使用按钮单击 - 驱动定义 - 尝试更改标签文本。 GUI 由三个按钮组成: Variable1(按下时将变量更改为 Variable1) Variable2(按下时将变量更改为 variable2) 打印变量(按下时打印变量)
我尝试了各种方法来更新标签文本 - 但无济于事。 我确实尝试了无法正常工作的全局变量。
如有任何帮助,我们将不胜感激。
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()
最佳答案
使用 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()
关于python - 更改标签文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9869935/