我不知道为什么但是在每个应该显示字符串的地方我都有PY_VAR#(#是一个数字)。 例如,它不显示“此区域显示来自从站的消息”,而是显示PY_VAR1。 我的代码是
#!/usr/bin/python
# -*- coding: utf-8 -*-
import tkinter
class Display(tkinter.Tk):
def __init__(self,parent):
tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
self.grid()
self.MessageString = tkinter.StringVar()
MessageDisplay = tkinter.Label(self, text = self.MessageString, anchor = 'nw' , justify = 'left')
MessageDisplay.grid(columnspan = 3)
self.ClockString = tkinter.StringVar()
ClockDisplay = tkinter.Label(self, text = self.ClockString)
ClockDisplay.grid(row=0)
self.Menu1Title = tkinter.StringVar()
Menu1Button = tkinter.Button(self, text = self.Menu1Title, command = self.SetMenu(MenuIndex = 1))
Menu1Button.grid(column = 0, columnspan = 2)
self.Menu2Title = tkinter.StringVar()
Menu2Button = tkinter.Button(self, text = self.Menu2Title, command = self.SetMenu(MenuIndex = 2))
Menu2Button.grid(column = 0, columnspan = 2)
self.Menu3Title = tkinter.StringVar()
Menu3Button = tkinter.Button(self, text = self.Menu3Title, command = self.SetMenu(MenuIndex = 3))
Menu3Button.grid(column = 0, columnspan = 2)
self.MenuDescriptionContent = tkinter.StringVar()
MenuDescription = tkinter.Label(self, text = self.MenuDescriptionContent, anchor = 'nw', justify = 'left')
MenuDescription.grid(column = 2, columnspan = 2, row = 1, rowspan = 3)
"""Temporary variables"""
self.MessageString = "This area shows messages coming from the slave"
self.ClockString = "00:00"
self.Menu1Title = "Menu 1"
self.Menu2Title = "Menu 2"
self.Menu3Title = "Menu 3"
self.MenuDescriptionContent = "This area shows the description of the selected menu"
def SetMenu(self, MenuIndex):
pass
if __name__ == "__main__":
app = Display(None)
app.title('Web Bell')
app.mainloop()
控制台中没有显示任何错误。 提前感谢您的回复
答案 0 :(得分:2)
如果您想要StringVar的值,则必须使用get
方法:
MessageDisplay = tkinter.Label(..., text = self.MessageString.get(), ...)
如果您使用StringVar作为textvariable
属性的值,那么您不需要这样做的唯一时间。
您的代码中还有其他问题。执行self.MessageString = "This area..."
时,您没有设置文本变量,实际上是将其丢弃并更改self.MessageString
以引用字符串。
相反,您需要调用变量的set方法,如下所示:
self.MessageString.set("This area...")