i am making a GUI and in it I am keeping the root hidden. So when i close the window which is showing using the x arrow, i wont the whole program to end not just the window that can be seen. Because other wise the user will have problems when closing the program.my root is hidden,
login can be seen,
when login is closed using the red x at the top i want to close the root as well how to do that?
You can bind to the <Destroy> event on your window. Within the bound function, you can do whatever you want including destroying the root window.
In the following example, killing the Toplevel windows will destroy the root window.
import tkinter as tk
class Window(tk.Toplevel):
def __init__(self, root, label):
super().__init__(root)
self.root = root
label = tk.Label(self, text=label)
label.pack(padx=20, pady=20)
self.bind("<Destroy>", self.kill_root)
def kill_root(self, event):
if event.widget == self and self.root.winfo_exists():
self.root.destroy()
root = tk.Tk()
label = tk.Label(root, text="Root window")
label.pack(padx=20, pady=20)
w1 = Window(root, "This is a toplevel window")
root.mainloop()
The reason for checking that event.widget is self is due to the fact that functions bound to the root or toplevel window are automatically inherited by all children within that window You only want to destroy the root window when the actual toplevel window is destroyed.
Related
How do I open a new window using a link in tkinter .
(For eg : in a login window i want to add a link that says "New user ? click here" and when I click on "click here" it takes me to the register window .
Please help me
enter image description here
[1]: https://i.stack.imgur.com/K5GV0.png
Please click the above link to see the image
Creating new toplevel windows works almost exactly the same as creating new widgets.
Toplevel windows are created using the Toplevel function:
t = Toplevel(parent)
Unlike regular widgets, you don't have to "Grid" a toplevel fo it to appear on screen. Once you've created a toplevel you can add children widgets within and grid them like in the main window. In other words toplevel behaves exactly like the automatic created root window.
To destroy a window use the method:
window.destroy()
You can open new windows in tkinter with the tkinter.Toplevel() command.
import tkinter as tk
class Gui:
"""Gui class"""
def __init__(self):
self.root = tk.Tk()
self.new_window = tk.Button(master=self.root, text="Open new window", width=20, pady=4, command=self.new_window)
self.new_window.pack()
self.root.mainloop()
def new_window(self):
"""Create a new top level window"""
new_window = tk.Toplevel()
tk.Label(master=new_window, text="This is a new window").pack()
if __name__ == '__main__':
Gui()
You can create a function to open a new window and then bind it to that Label, for example:
import tkinter as tk
def newWindow():
# Window object (top level)
newWindow = Toplevel(master)
# Title
newWindow.title("New Window 1")
# Geometry
newWindow.geometry("300x300")
root = tk.Tk()
label = tk.Label(text="Hello!", width=50, height=10, master=root)
label.pack()
label.bind("<Button-1>", newWindow)
How do you make a button open a duplicate window of the main page in a new window?
What would the python code be to do this with Tkinter?
For clarification, in notepad++ when you right click on the tab that you are in, it shows you a whole menu.
Then, there is a dropdown menu with an option that says, open in a new instance or move to a new instance.
This would then open the tab you selected into a new tab/window.
I haven't tried anything yet but I did look up about how to do this type of thing and there were no results on what exactly I wanted to do.
Can you try to show me an example of how what I need to do can be done?
Code:
Found Here
The simplest solution is to make your window be a subclass of Toplevel. Then, each time you need a new window, create a new instance of the class.
You can then leave the root window blank and hide it so that you only see your custom windows. It's important to make sure that you destroy the root window after the user has deleted the last application window, or else you'll end up with an invisible window that will be hard to kill.
Here is a bit of a contrived example:
import tkinter as tk
class AppWindow(tk.Toplevel):
def __init__(self, root):
tk.Toplevel.__init__(self, root)
self.root = root
menubar = tk.Menu(self)
windowMenu = tk.Menu(menubar)
windowMenu.add_command(label="New Window", command=self.new_window)
windowMenu.add_command(label="Quit", command=root.destroy)
menubar.add_cascade(label="Window", menu=windowMenu)
self.configure(menu=menubar)
self.text = tk.Text(self)
self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
# call a function to destroy the root window when the last
# instance of AppWindow is destroyed
self.wm_protocol("WM_DELETE_WINDOW", self.exit_on_last_window)
def new_window(self):
AppWindow(self.root)
def exit_on_last_window(self):
"""Destroy the root window when no other windows exist"""
self.destroy()
if not any([window.winfo_exists() for window in self.root.winfo_children()]):
self.root.destroy()
def quit(self):
self.root.destroy()
# create the root, then hide it. The app will create its
# own windows as Toplevels
root = tk.Tk()
root.withdraw()
# create the first window, then start the event loop
AppWindow(root)
tk.mainloop()
Preface:
I have a Python ControlGPIO code with a working GUI (let us call it MainGUI).
I wish to have a dialog pop up window, prior to running MainGUI, in a way that the user can enable/ disable features in MainGUI. BUT MainGUI should start running only after dialog pop up window is closed.
My question is: How can I make a popup window that will postpone MainGUI untill it is closed?
Code below- boot_windows is my dialog pop up window (where all the enable/disable checkboxes will be ), BUT obviously does not postpone App as I need
root = Tk()
#output_gpioPins = [4,22,6,26]
#input_gpioPins = [3,21,5,27]
#ip = '192.168.2.112'
boot_windows = Toplevel(root)
text1 = ttk.Label(boot_windows, text="Hello World !!!")
text1.grid()
App = ContorlGPIOWindow(root, ip = '192.168.2.113', with_sf_bt=1, with_hw_bt=1, switch_names=['Light Kitchen','Light Room1', 'Window1', 'Window2'])
root.mainloop()
You can't do precisely what you want. Widgets exist in a tree-like structure. All windows except the root require a root window. The root window must be created first (which is why it's called the root window).
If you don't want the user to see it, you can hide it until it is ready to be displayed.
import tkinter as tk
root = tk.Tk()
root.withdraw()
boot_window = tk.Toplevel(...)
...
You can then call root.deiconify() when you are ready for it to be visible.
Another common solution is to use the root window for your dialog or splash screen or whatever, and then simply replace its contents with the real contents when you're ready.
As for how to wait for the popup... the root window has a method named wait_window which will enter the event loop and not return until the given window has been destroyed.
Here's an example of it's use:
import Tkinter as tk
class MainGUI(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Hello, world!")
label.pack(fill="both", expand=True, padx=20, pady=20)
class Popup(tk.Toplevel):
def __init__(self, root):
tk.Toplevel.__init__(self, root)
label = tk.Label(self, text="Click to continue...")
label.pack(fill="both", expand=True, padx=20, pady=20)
button = tk.Button(self, text="OK", command=self.destroy)
button.pack(side="bottom")
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
popup = Popup(root)
root.wait_window(popup)
main = MainGUI(root)
main.pack(fill="both", expand=True)
root.deiconify()
root.mainloop()
I have a UI root window where two other Toplevel windows get created on separate button clicks. These Toplevel windows are anchored to the root window and drag along the screen with the root window.
My problem is if i have another window open and my UI is hiding behind it, if i click on my UI from the taskbar or the little i can see on the screen, only the root Tk window pops up and the other Toplevel windows are still hiding behind the other window.
I tried toplevel.lift() and toplevel.wm_attributes("-topmost", 1) but neither give me what i want.
How can I tie the Toplevel windows so that if they are open and I click on the root window the Toplevel window also pops to the top?
Here is a simple example that will open 2 windows and disable everything on the root window while also binding any interaction with that root window to lift all the top windows above it.
I have also bound the top level close event to first remove the root binding and then destroy the top levels then re-enable all the widgets in the root window. this should serve to be an antiquity example of what you are trying to do.
Let me know if you have any questions.
import tkinter as tk
class ExampleApp(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
self.master.geometry("400x150")
self.main_frame = tk.Frame(self.master)
self.main_frame.pack(expand=tk.YES, fill=tk.BOTH)
self.master.protocol('<WM_LBUTTONDBLCLK>', self.motion)
tk.Label(self.main_frame, text = "This is the main window").pack()
tk.Button(self.main_frame, text = "Open 2 top level windows!", command = self.open_windows).pack()
def motion(self, event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
def open_windows(self):
self.top1 = tk.Toplevel(self.master)
self.top2 = tk.Toplevel(self.master)
self.top1.geometry("100x100")
self.top2.geometry("100x100")
# ties the window close event to our customer close method for toplevel
self.top1.protocol("WM_DELETE_WINDOW", self.close_toplevels)
self.top2.protocol("WM_DELETE_WINDOW", self.close_toplevels)
self.master.bind("<Unmap>", self.icon_all)
self.top1.bind("<Unmap>", self.icon_all)
self.top2.bind("<Unmap>", self.icon_all)
self.master.bind("<Map>", self.de_icon_all)
self.top1.bind("<Map>", self.de_icon_all)
self.top2.bind("<Map>", self.de_icon_all)
for child in self.main_frame.winfo_children():
child.configure(state='disable')
tk.Label(self.top1, text ="Topwindow 1").pack()
tk.Label(self.top2, text ="Topwindow 2").pack()
# sets the top windows to their initial locations
self.lock_top_to_root()
#keeps the top windows in the specified locations compared to root window
self.master.bind("<Configure>", self.lock_top_to_root)
def withdraw_tops(self, event=None):
self.top1.withdraw()
self.top2.withdraw()
def de_icon_tops(self, event=None):
self.top1.deiconify()
self.top2.deiconify()
def icon_all(self, event=None):
self.withdraw_tops()
self.master.iconify()
def de_icon_all(self, event=None):
self.de_icon_tops()
self.master.deiconify()
self.lock_top_to_root()
def lock_top_to_root(self, event=None):
self.top1.lift() # lift both toplevel windows about root
self.top2.lift()
# places each top level at each side
# this is not set up to compensate for the root being resized but can be if you need it to.
self.top1.geometry('+{}+{}'.format(self.master.winfo_x()+10, self.master.winfo_y()+30))
self.top2.geometry('+{}+{}'.format(self.master.winfo_x()+275, self.master.winfo_y()+30))
def close_toplevels(self):
# customer close method to reset everything
self.master.unbind('<Configure>')
self.master.unbind("<Unmap>")
self.master.unbind("<Map>")
self.top1.destroy()
self.top2.destroy()
for child in self.main_frame.winfo_children():
child.configure(state='active')
root = tk.Tk()
my_example = ExampleApp(root)
root.mainloop()
I'm looking for the most efficient way to create a child window and also forbid all access to the main window, in order that user will have to click on "OK" button inside the child window in order to recover the access to the main window.
Here is my code, using Toplevel class. It works, but is there a more efficient way to do ?
from tkinter import *
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
def SetEnableStatus(self, status):
for w in self.winfo_children():
if status == False:
w.grab_set()
else:
w.grab_release()
def CreateChildWindow(self):
subWindow = Toplevel(self)
def quit_subwindow():
subWindow.destroy()
self.SetEnableStatus(True) # Enable all widgets of main window
Button(subWindow, text='Exit', command=quit_subwindow).pack()
self.SetEnableStatus(False) # Disable all widgets of main window
It should be enough to call grab_set on the Toplevel object, and when you are done with it, you can simply destroy it, and call grab_set on self (but I am not 100% sure, even if the resulting program below confirms it).
In fact, if you create a button on your Tk root window and if you associate with this button for example a lambda function that prints something, then nothing will be printed, after "setting the grab" on the child window.
See the following example where basically all events are redirected to the Toplevel window, instead of to the Tk root window:
from tkinter import *
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
Entry(self).pack(side="left")
Button(self, text="whoami", command=lambda : print("A Tk root window.")).pack(side="left")
def CreateChildWindow(self):
subWindow = Toplevel(self)
def quit_subwindow():
subWindow.destroy()
self.grab_set()
Button(subWindow, text="whoami", command=lambda : print("A Toplevel child window.")).pack()
Button(subWindow, text='Exit', command=quit_subwindow).pack()
subWindow.grab_set()
win = MainWindow()
win.CreateChildWindow()
win.mainloop()
Check out the following article at effbot.org to know more about how to create dialogs:
http://effbot.org/tkinterbook/tkinter-dialog-windows.htm