How to delete a Tkinter widget placement?
Use place_forget()
instead of pack_forget()
. You may also remove the pack()
calls. In your example, calling place is negating the pack call. That's why pack_forget()
will not work. The button ceased to be packed the instant it was placed.
button1.pack(...) # do it this way
button1.place(...) # nevermind, do it this way
example:
from tkinter import *
def delete():
button1.place_forget()
button2.place_forget()
gui =Tk()
gui.geometry("250x500")
button1 = Button(gui, text="1", command=delete, pady= 15, padx= 20)
button1.place(x=85, y=60)
button2 = Button(gui, text="2", pady= 15, padx= 20)
button2.place(x=63, y=120)
gui.mainloop()
Tkinter has 3 geometry methods ~ place, pack, and grid. Only one geometry method is required, per widget, but combining the grid or pack method with place is allowed. However, that combination is intended to allow you to alternatively pack or place widgets simultaneously within one container. In other words, some of the widgets are packed, and some of them are placed, within one container. None of the widgets are both packed and placed.
Determining which to use is simple. If you want to "dock" a widget to one or more of it's parent's edges use pack. If you need a table layout use grid. If you need arbitrary positioning use place.
Tkinter in Python - Remove widget from active window
The specific answer to your question is that pack_forget
, grid_forget
or grid_remove
are what you want if you want to make a widget temporarily invisible. Which one you choose depends on if you're using grid or pack, and whether or not you want grid to remember where it was so you can later put it back in the same spot.
destroy
is what you want to call if you want to literally destroy the widget.
When used properly, none of those methods will cause your program to freeze. Without seeing your code, it's impossible to know what the root cause of the problem is.
Python Tkinter: How to delete all child widgets with a single deletion of a parent widget?
One implementation would be to have the top-level object track its widgets via a list, then have a .close()
method that looks something like:
def close(self):
for widget in self.widgets:
widget.destroy()
self.destroy()
Tkinter - Deleting widgets of a Canvas when calling Misc.lower() method
Not sure why you would just want to hide it though, then how would the users redirect to the previous page? Is this what you wanted?
from tkinter import *
class Application:
def __init__(self):
# self.backButton = None
self.window = Tk()
self.window.geometry("1280x720")
self.canvas1 = Canvas(self.window, highlightthickness=0, bg="#1b1b1b")
self.canvas2 = Canvas(self.window, highlightthickness=0, bg="red2")
self.initButtons()
self.window.mainloop()
def initButtons(self, *args):
buttons = {}
self.buttonList = []
buttons['backButtons'] = Button(self.window, bd=0, activebackground="#1b1b1b", bg="#1b1b1b", text='Back', fg="#ffffff")
self.buttonList.append(buttons['backButtons'])
self.initSecondWindow()
def initFirstWindow(self, *args):
Misc.lower(self.canvas2)
self.canvas2.delete(self.res)
self.canvas1.place(relwidth=1, relheight=1)
def initSecondWindow(self, *args):
backButton = self.buttonList[0]
backButton.config(command=self.initFirstWindow)
self.canvas2.place(relwidth=1, relheight=1)
self.res = self.canvas2.create_window(640, 360, window=backButton)
Application()
Related Topics
How to Change Python Version in Anaconda Spyder
How to Kill a While Loop With a Keystroke
Filtering the Dataframe Based on the Column Value of Another Dataframe
How to Get Maximum Length of Each Column in the Data Frame Using Pandas Python
Bold Formatting in Python Console
Finding the Max Value in a Two Dimensional Array
Replacing All Negative Values in Certain Columns by Another Value in Pandas
What Is the Simplest Way to Ssh Using Python
How to Sort a Single String Output in Ascii Descending Order Through a Function
How to Update a Pyspark Dataframe With New Values from Another Dataframe
How to Remove Any Url Within a String in Python
How to Specify File Path in Jupyter Notebook
Delete Rows Containing Numeric Values in Strings from Pandas Dataframe
Python Pandas .Isnull() Does Not Work on Nat in Object Dtype
How to Create Different Variable Names While in a Loop
How to Convert a 1 Channel Image into a 3 Channel With Opencv2