简体   繁体   中英

Python/Tkinter: How to change Label widget with using Entry widget?

What i wanted to do is, while typing some words in Entry widget, at the same time changing the characters that are displayed in label widget. Here are the codes:

import tkinter as tk


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

        self.entry = tk.Entry(master=self)
        self.entry.pack(side="left")

        self.var = tk.StringVar()
        self.var.set(self.entry.get)

        self.label = tk.Label(master=self)
        self.label.pack(side="left")

        self.configure_widgets()
        self.pack()

    def configure_widgets(self):
        self.label.configure(textvariable=self.var)


if __name__ == "__main__":
    root = tk.Tk()
    example = App(master=root)
    example.mainloop()

Which parts i should change of the codes? Thank you in advance.

Both Entry and Label accept a variable as a parameter. The entry will set the variable value and the Label will get it.

import tkinter as tk


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

        self.var = tk.StringVar()

        self.entry = tk.Entry(master=self, textvariable=self.var)
        self.entry.pack(side="left")

        self.label = tk.Label(master=self, textvariable=self.var)
        self.label.pack(side="left")

        self.configure_widgets()
        self.pack()

    def configure_widgets(self):
        self.label.configure(textvariable=self.var)


if __name__ == "__main__":
    root = tk.Tk()
    example = App(master=root)
    example.mainloop()

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