In Tkinter How to Make a Widget Invisible

In Tkinter is there any way to make a widget invisible?

You may be interested by the pack_forget and grid_forget methods of a widget. In the following example, the button disappear when clicked

from Tkinter import *

def hide_me(event):
event.widget.pack_forget()

root = Tk()
btn=Button(root, text="Click")
btn.bind('<Button-1>', hide_me)
btn.pack()
btn2=Button(root, text="Click too")
btn2.bind('<Button-1>', hide_me)
btn2.pack()
root.mainloop()

how to show/hide widget in tkinter without moving other widget

The problem is grid() does not take up empty space by default, it gives the last empty row/col to the widget(if previous rows before it are empty).

So what you can do is, set minimum space for your column and row so that those space will remain empty, so change your function to:

def d_check_vat_1():
E_partnum.focus()
if I_check_vat.get():
L_quan.grid(row=2, column=2)
E_quan.grid(row=3, column=2)
GUI.update() # To update the values

width = E_quan.winfo_width() # Get widget width
height = L_quan.winfo_height() # Get widget height

GUI.rowconfigure(2,minsize=height) # Now apply the values
GUI.rowconfigure(3,minsize=height)
GUI.columnconfigure(2,minsize=width)

else:
L_quan.grid_remove()
E_quan.grid_remove()

Now its dynamic as well, it takes the width of widget and applies that as the minsize of that row so that row will have that empty space.

How to show/hide widgets in Tkinter?

I think you want grid_remove().

From http://www.tkdocs.com/tutorial/grid.html:

The "forget" method of grid, taking as arguments a list of one or more
slave widgets, can be used to remove slaves from the grid they're
currently part of. This does not destroy the widget altogether, but
takes it off the screen, as if it had not been gridded in the first
place. You can grid it again later, though any grid options you'd
originally assigned will have been lost.

The "remove" method of grid works the same, except that the grid
options will be remembered.

Ugly example follows. Play with the grid options and the entry text to see how they are preserved.

def toggle_entry():
global hidden
if hidden:
e.grid()
else:
e.grid_remove()
hidden = not hidden

hidden = False
root = tk.Tk()
e = tk.Entry(root)
e.grid(row=0, column=1)
tk.Button(root, text='Toggle entry', command=toggle_entry).grid(row=0, column=0)
root.mainloop()

Showing and Hiding widgets

This has been answered before on stackoverflow. The short answer is, you can use grid_remove which will cause the widget to be removed if it was previously added via grid. grid_remove remembers where the widget was, so a simple grid() will put it back without having to re-specify all of the options.

You can also use pack_forget (if using pack) and grid_forget (if using grid). These work similarly to grid_remove except they cause all of the settings to be forgotten, which means you have to explicitly place it back into the proper place on the screen.

Another option is to take advantage of the stacking order of widgets. You can use the lower method to hide the widget behind its sibling, and lift to raise it above. See this answer for an example.



Related Topics



Leave a reply



Submit