【问题标题】:Updating tkinter label更新 tkinter 标签
【发布时间】:2017-04-12 06:30:00
【问题描述】:

我正在尝试更新标签,但我编写的代码每次都会创建一个新标签。我对 tkinter 比较陌生,所以我不明白如何将其他答案应用于我的代码。

from tkinter import *
import random

class Window(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master) 
        self.master=master 
        self.init_window()

    def init_window(self): 
        self.pack(fill=BOTH, expand=1)
        testButton=Button(self, text="Press", command=calc)
        testButton.pack()
        l1=Label(text="")

    def testbutton(ans): #creates a new instance of l1 each time, I want to update existing l1
        var=StringVar()
        l1=Label(textvariable=var) #l1.configure() gives error l1 not defined
        var.set(ans)
        l1.pack()

def calc():
    list1=["a","b","c"]
    index=random.randint(0,2)
    answer=list1[index]
    Window.testbutton(answer)

root=Tk()
root.geometry("400x300")
app=Window(root)
root.mainloop()

每次按下按钮时,都会创建一个新标签,而不是更新现有标签上的文本。 这是我实际项目的简化版本,但突出了标签的问题。 我尝试在 testbutton 函数中使用l1.configure(...),但它运行一个错误,即 l1 未定义。

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    为避免每次都创建一个新的Label,您需要创建一个并将其保存为Window 实例的属性。要使其可供calc() 函数访问,您还需要将Window 实例作为参数传递给它(以避免使用全局变量)。使用tkinter 执行此操作的常见原因是使用lamba 函数作为Buttoncommand= 参数并将self 设置为其参数的默认值,如下所示。

    from tkinter import *
    import random
    
    class Window(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.master = master
            self.init_window()
    
        def init_window(self):
            self.pack(fill=BOTH, expand=1)
            testButton = Button(self, text="Press",
                                command=lambda win=self: calc(win))
            testButton.pack()
            self.l1 = Label(text="")
            self.l1.pack()
    
        def testbutton(self, ans):
            self.l1.configure(text=ans)
    
    def calc(window):  # note window argument added
        list1 = ["a","b","c"]
        index = random.randint(0,2)
        answer = list1[index]
        window.testbutton(answer)
    
    root = Tk()
    root.geometry("400x300")
    app = Window(root)
    root.mainloop()
    

    【讨论】:

    • 谢谢,这解决了我的问题。为什么init_window中的l1标签需要self.l1...?
    • Becky:self.l1 = Label(text="") 是创建的Label 小部件如何附加到Window 实例以供以后参考。在 Python 中,您可以随时通过为要使用的名称分配值来创建新的实例属性:即self.attribute_name = ...。您也可以使用del self.attribute_name 删除它们。
    【解决方案2】:

    您可以只使用类方法和属性。
    使用StringVar 更改标签文本:

    class Window(Frame):
    
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.master = master
            self.init_window()
    
        def init_window(self):
            self.pack(fill=BOTH, expand=1)
            testButton = Button(self, text="Press", command=self.calc)
            testButton.pack()
            self.ltext = StringVar()
            l1 = Label(textvariable=self.ltext)
            l1.pack()
    
        def testbutton(self, ans):
            self.ltext.set(ans)
    
        def calc(self):
            list1 = ["a", "b", "c"]
            index = random.randint(0, 2)
            answer = list1[index]
            self.testbutton(answer)
    
    root = Tk()
    root.geometry("400x300")
    app = Window(root)
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-01
      • 2017-11-18
      • 2015-12-08
      • 2015-05-24
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多