简体   繁体   中英

python - change tkinter label

The following script creates a tkinter window with a text label, exit button and change-text button:

from tkinter import *
from tkinter import ttk

class Window(Frame):

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

    def init_window(self):
        test_label = Label(root, text="none").grid(row=0, column=0, sticky=W)

        change_text_btn = Button(root, text="change_text", command=self.set_label_text).grid(row=2, column=0, sticky=W)
        exit_btn = Button(root, text="Exit", command=self.client_exit).grid(row=2, column=1, sticky=W)

    def set_label_text(self):
        test_label.config(text='changed the value')

    def client_exit(self):
        exit()

if __name__ == '__main__':
    root = Tk()
    app = Window(root)
    root.mainloop()

After click on change_text_btn I get a NameError: name 'test_label' is not defined error. So the problem is that test_label created in init_window() is not avaliable from set_label_text() beacuse of the scope. How do I fix it?

To overcome the issue, you can make test_label an instance variable by prefixing it with self . Besides that, when you chain methods like that, what happens is you assign None to your variable, since grid() returns None - instead, place each method in a separate line (this stands for all of your buttons):

self.test_label = Label(root, text="none")
self.test_label.grid(row=0, column=0, sticky=W)

Of course, you'd need to refer to it with self.test_label later on in your set_label_text function.

Other than that, I suggest you get rid of from tkinter import * , since you don't know what names that imports. It can replace names you imported earlier, and it makes it very difficult to see where names in your program are supposed to come from. Use import tkinter as tk instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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