如何在tkinter上更新标签

时间:2016-11-07 19:02:02

标签: python python-3.x tkinter label

按下其中一个按钮后,我想更新标签。 这是我的代码 - 我添加了一个标签(caled label1),现在我有两个问题:

      
  1. 提出一些乱码
  2.   
  3. 当用户按下“浏览”按钮时,如何使用文本更新标签?
  4. from tkinter import *
    import threading
    
    class Window(Frame):
        def __init__(self, master=None):
            Frame.__init__(self, master)
            self.master = master
            self.init_window()
    
        def init_window(self):
            self.var = IntVar()
            self.master.title("GUI")
            self.pack(fill=BOTH, expand=1)
            quitButton = Button(self, text="Exit", command=self.client_exit)
            startButton = Button(self, text="Browse", command=self.start_Button)
            label1 = Label(self, text=self.lable_1)
    
            quitButton.grid(row=0, column=0)
            startButton.grid(row=0, column=2)
            label1.grid(row=1, column=0)
    
        def client_exit(self):
            exit()
    
        def lable_1(self):
            print('starting')
    
        def start_Button(self):
            def f():
                print('Program is starting')
            t = threading.Thread(target=f)
            t.start()
    
    root = Tk()
    root.geometry("250x50")
    app = Window(root)
    root.title("My Program")
    root.mainloop()
    

1 个答案:

答案 0 :(得分:0)

使用self.label['text']更改文字

(最小?)工作示例:

import tkinter as tk

class Window(tk.Frame):

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

        # tk.Frame.__init__ creates self.master so you don't have to
        #self.master = master 

        self.init_window()

    def init_window(self):
        self.pack(fill=tk.BOTH, expand=1)

        quit_button = tk.Button(self, text="Exit", command=root.destroy)
        start_button = tk.Button(self, text="Browse", command=self.on_click)

        self.label = tk.Label(self, text="Hello")

        quit_button.grid(row=0, column=0)
        start_button.grid(row=0, column=1)

        self.label.grid(row=1, column=0, columnspan=2)

    def on_click(self):
        self.label['text'] = "Starting..."

root = tk.Tk()
app = Window(root)
root.mainloop()