How to Hide Tkinter Python Gui

How To Hide Tkinter python Gui

To hide the default root window you can use

root.withdraw()

and to make it visible again you can use

root.deiconify()

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 do I get rid of Python Tkinter root window?

Probably the vast majority of of tk-based applications place all the components in the default root window. This is the most convenient way to do it since it already exists. Choosing to hide the default window and create your own is a perfectly fine thing to do, though it requires just a tiny bit of extra work.

To answer your specific question about how to hide it, use the withdraw method of the root window:

import Tkinter as tk
root = tk.Tk()
root.withdraw()

If you want to make the window visible again, call the deiconify (or wm_deiconify) method.

root.deiconify()

Once you are done with the dialog, you can destroy the root window along with all other tkinter widgets with the destroy method:

root.destroy()

python Tkinter() how to hide the UI

Here is an alternative: Define each "page" in a Frame. Then use calls to lift to raise the appropriate Frame above the others. (The following code borrows heavily from Bryan Oakley's code here.)

import tkinter as tk
class Page(tk.Frame):
def __init__(self, master, text, height, width, *args, **kwargs):
tk.Frame.__init__(self, *args, borderwidth=20, **kwargs)
self.height = height
self.width = width
button = tk.Button(self, text=text, font=('Comic Sans MS', 20),
command=lambda: self.callback())
button.pack(side="top", fill="both", expand=True)
def onlift(self):
root.geometry('{}x{}'.format(self.width, self.height))
self.lift()

class App(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)

p1 = Page(self, 'Login 1', height=200, width=300)
p2 = Page(self, 'Next page is 2', height=400, width=300)
p3 = Page(self, 'We love number 3', height=400, width=600)
p1.callback = p2.onlift
p2.callback = p3.onlift
p3.callback = p1.onlift

p1.place(x=0, y=0, relwidth=1, relheight=1)
p2.place(x=0, y=0, relwidth=1, relheight=1)
p3.place(x=0, y=0, relwidth=1, relheight=1)

p1.onlift()

root = tk.Tk()
app = App(root)
root.mainloop()

Above I've only defined one kind of Frame, called Page. For your situation, you'd want a Login Frame, and then a subsequent Frame:

    p1 = Login(self, ...)
p2 = Registered(self, ...)

Tkinter-Hide or quit and get back a window

You forgot to add global opened in both quit_window and return_main functions. Because of that, when you click on the return button, your global variable opened is not changed, only the local one inside the return_main function.

In addition, I don't see the need to destroy main each time you display window since you go back and forth between the two. So my suggestion is to keep main and simply withdraw it when not needed. In this case, window will be a Toplevel of main and the code is:

from tkinter import * 

def quit_window():
global opened
window.destroy()
opened = False
main.deiconify()

def return_main():
global opened
window.withdraw()
opened = True
print(opened)
main.deiconify()

def launch():
global opened, window
print(opened)
main.withdraw() # withdraw main instead of destroying it
if opened == True:
window.deiconify()
else:
window = Toplevel(main)
breturn = Button(window, text="Return", command=return_main).pack()
bquit = Button(window, text="Quit", command=quit_window).pack()

main = Tk()
bopen = Button(main, text="Open", command=launch).pack()
opened = False
main.mainloop() # call mainloop only for main

How do you hide a gui from the statusbar?

There are several ways to tell the desktop manager not to display the app in the taskbar:

  • Change the window type from 'normal' to a type which is not displayed in the taskbar by default (depending on the type it can also remove window decorations and change the window position, e.g. always on top). For example, the 'toolbar' type will do exactly what you want.

    import tkinter as tk

    root = tk.Tk()
    root.attributes('-type', 'toolbar')
    root.mainloop()

    You can find a list of types here.

  • You can also change yourself the state of your window with the ewmh module:

    import tkinter as tk
    from ewmh import EWMH

    ewmh = EWMH()

    root = tk.Tk()
    root.update_idletasks() # to make sure the window is displayed
    w = ewmh.getActiveWindow() # get the window
    ewmh.setWmState(w, 1, '_NET_WM_STATE_SKIP_TASKBAR') # remove window from taskbar
    ewmh.display.flush()
    root.mainloop()

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()

Hide a Button in Tkinter

As pointed out in the comments you should use place_forget() for widgets that were set on the screen using place().

Same goes for pack() and grid(). You would use pack_forget() and grid_forget() respectively.

Here is a modified example of your code.

import tkinter as tk

class Example(tk.Tk):
def __init__(self):
super().__init__()
canvas = tk.Canvas(self)
canvas.pack()
self.startGame = tk.Button(canvas, text="Start", background='white', font=("Helvetica"))
self.startGame.place(x=150, y=100)
self.startGame.bind('<Button-1>', self.hide_me)

def hide_me(self, event):
print('hide me')
event.widget.place_forget()

if __name__ == "__main__":
Example().mainloop()

That said you do not need a bind here. Simply use a lambda statement in your command like this:

import tkinter as tk

class Example(tk.Tk):
def __init__(self):
super().__init__()
canvas = tk.Canvas(self)
canvas.pack()
self.startGame = tk.Button(canvas, text="Start", background='white', font=("Helvetica"),
command=lambda: self.hide_me(self.startGame))
self.startGame.place(x=150, y=100)

def hide_me(self, event):
print('hide me')
event.place_forget()

if __name__ == "__main__":
Example().mainloop()


Related Topics



Leave a reply



Submit