I am building a GUI with Tkinter in which I would like to give users the option of changing an entry widget to a label widget (and vice-versa) by clicking a button.
I have tried a few different approaches but can't get it to work. Here is one of the ways I've tried to approach this problem:
import tkinter as tk
show_label = False
class App(tk.Tk):
def __init__(self):
super().__init__()
label = tk.Label(self, text="This is a label")
entry = tk.Entry(self)
button = tk.Button(self, text="Label/Entry",
command=self.change)
if show_label:
label.pack()
else:
entry.pack()
button.pack()
def change(self):
global show_label
show_label = not show_label
self.update()
if __name__ == '__main__':
app = App()
app.mainloop()
Aside from the above, I have also tried:
- Updating the instance of app inside the mainloop, i.e. after instantiating
app = App()
- Making show_label a class variable and the change() a class method
Any help on the matter is greatly appreciated!
Thanks