繁体   English   中英

Python Tkinter更改变量标签状态和状态栏

[英]Python tkinter change variable label state and statusbar

我在这里确实有一点问题。 这是关于TKinter中标签变量的交换。 我的程序不会刷新该值。

class Application(Frame):
    def __init__(self,parent,**kw):
         Frame.__init__(self,parent,**kw)
         self.x = None
         self.directory = None
         self.autostate = None
         self.state = "closed"
         self.GUI2()

     def state(self):
         #change states
         self.stateVar="open"
         self.statusbar = "Status: Opening gate..."

         #update tkinter
         self.group.update_idletasks()
         self.w.update_idletasks()

     def GUI2(self):

         self.statusbar = "Status:..."

         # menu left
         self.menu_left = tk.Frame(root, width=150, bg="red", bd=1, relief=RIDGE)
         self.menu_left_upper = tk.Frame(self.menu_left, width=300, height=900, bg="#C0C0C0")
         self.menu_left_lower = tk.Frame(self.menu_left, width=300, bd=1, relief=GROOVE)

         self.label1 = tk.Label(self.menu_left_lower, relief=FLAT, bg="blue" )
         self.button1 = Button(self.menu_left_lower, text="RUN")
         self.test = tk.Label(self.menu_left_upper, text="info", bg="#C0C0C0")


         self.menu_left_upper.pack(side="top", fill="both", expand=TRUE)
         self.menu_left_lower.pack(side="top", fill="both", expand=FALSE)

         # right area
         self.some_title_frame = tk.Frame(root, bg="#dfdfdf", bd=1, relief=RIDGE)
         self.some_title = tk.Label(self.some_title_frame, text="some title", bg="#dfdfdf")
         self.text_area = Listbox(root, width=50, height=10, background="#ffffff", relief=GROOVE)

         #Label and Button
         self.group = LabelFrame(self.menu_left_upper, text="info", height=70)
         self.group.pack(side="top", fill="both", expand=TRUE)
         Button(self.menu_left_lower, text='Press', command=self.state).pack(side="bottom")
         self.w = Label(self.group, text='State='+self.stateVar)    #text printed!
         self.w.pack(expand=TRUE)

         # status bar
         self.status_frame = tk.Frame(root)
         self.status = tk.Label(self.status_frame, text=self.statusbar, bd=1, relief=SUNKEN)    #statusbar printed here
         self.status.pack(fill="both", expand=True)
         self.menu_left.grid(row=0, column=0, rowspan=2, sticky="nsew")
         self.status_frame.grid(row=2, column=0, columnspan=2, sticky="ew")
         root.grid_rowconfigure(1, weight=1)
         root.grid_columnconfigure(1, weight=1)

 #Starts the main loop and causes the class to interact with the init function
 if __name__ == '__main__':
    root = Tk()
    root.title("simulation")
    app = Application(root)
    app.grid()
    root.mainloop()

在这里您可以看到整个代码。

重要的是检查#tab1中是否有按钮。 此按钮指的是def状态(自身):此按钮需要更改标签和状态栏。 在程序中我将它们添加到self.wself.status中,并添加了一个#text打印内容! 下线之后。

错误在于Label参数中:如果输入变量已更新,则不会更新tekst参数。 您应该将stateVar分配给Labeltextvariable关键字参数,并且不使用text参数。

下面是一个示例程序,可以帮助您弄清楚如何更改标签的文本。 他们的关键是创建一个StringVar并将标签指向此标签,以便在StringVar出现时更新标签。

from Tkinter import *

class Application(Frame):
    def __init__(self,parent,**kw):
        Frame.__init__(self,parent,**kw)
        # Create a String variable to store our status string
        self.stateVar = StringVar()
        self.stateVar.set("Initial Value")

        # Create a label to display the status
        self.label1 = Label(textvariable=self.stateVar)
        self.label1.grid()

        # Create a button which will change the status
        self.button = Button(text="Press me", command=self.change)
        self.button.grid()

    def change(self):
        """Called when the button is pressed"""
        self.stateVar.set('You pressed the button')


if __name__ == '__main__':
    root = Tk()
    root.title("simulation")
    app = Application(root)
    app.grid()
    root.mainloop()

暂无
暂无

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

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