Reputation: 1
So I am making a tkinter GUI application with python and how do you make this forget the label and not the button when the button is pressed?
import tkinter as tk
window = tk.Tk()
def func():
mainframe = tk.Frame()
label = tk.Label(text = "Label", master = mainframe)
label.pack()
mainframe.pack()
def function():
mainframe = tk.Frame()
button = tk.Button(text = "Button", master = mainframe, command = lambda: remove(mainframe))
button.pack()
mainframe.pack()
func()
def remove(mainframe):
mainframe.forget()
function()
window.mainloop()
I'm pretty bad at coding, it's probably a really dumb question.
Upvotes: 0
Views: 61
Reputation: 1
I got it, I just have to define a global variable and change it into mainframe like this. Thanks for the help though:) I really appreciate it.
import tkinter as tk
window = tk.Tk()
def func():
mainframe = tk.Frame()
label = tk.Label(text = "Label", master = mainframe)
label.pack()
mainframe.pack()
global x
x = mainframe
def function():
mainframe = tk.Frame()
button = tk.Button(text = "Button", master = mainframe, command = lambda:
remove(mainframe))
button.pack()
mainframe.pack()
func()
def remove(mainframe):
x.forget()
function()
window.mainloop()
Upvotes: 0
Reputation: 15
import tkinter as tk
window = tk.Tk()
labelframe = None
def func():
global labelframe
labelframe = tk.Frame()
label = tk.Label(text="Label", master=labelframe)
label.pack()
labelframe.pack()
def function():
global label
buttonframe = tk.Frame()
button = tk.Button(text="Button", master=buttonframe, command=lambda: remove(labelframe))
button.pack()
buttonframe.pack()
func()
def remove(to_forget):
to_forget.forget()
function()
window.mainloop()
Upvotes: 1
Reputation: 365
Just add a label.forget()
like I did below:
import tkinter as tk
window = tk.Tk()
def func():
mainframe = tk.Frame()
label = tk.Label(text = "Label", master = mainframe)
label.pack()
label.forget()
mainframe.pack()
def function():
mainframe = tk.Frame()
button = tk.Button(text = "Button", master = mainframe, command = lambda: remove(mainframe))
button.pack()
mainframe.pack()
func()
def remove(mainframe):
mainframe.forget()
function()
window.mainloop()
Upvotes: 0