Python Forum
[split] Closing a window but not the whole program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] Closing a window but not the whole program
#2
here's an example of a main window with multiple toplevel windows the pink window is tied with the main and will not close until the main closes the red and blue are independent:
from tkinter import Frame,Canvas,Button,Tk,Toplevel,Label


class MainWindowExample(Frame):
    def __init__(self, parent= None):
        self.parent= parent
        Frame.__init__(self, self.parent)
        self.pack(expand='yes', fill='both')
        self.canvas= Canvas(self)
        self.canvas.config(width=500,height=300, bg='gray80')
        self.canvas.pack(expand='yes', fill='both')
        self.make_components()
    def make_components(self):
        self.label_1= Label(self.canvas, text='button One')
        self.label_1.place(x=10,y=10)
        self.button_1= Button(self.canvas, text='popup2', command=self.popup_2)
        self.button_1.place(x=100,y=10)
        self.myToplevel= Toplevel(width=200,height=250,bg='red')#seperate window
        self.myToplevel.title('example')
        self.myTopLevel_2= Toplevel(width=200,height=250,bg='pink') #closes w/main
        self.myTopLevel_2.protocol('WM_DELETE_WINDOW', lambda:None)
    def popup_2(self):
        self.toplevel= Toplevel(width=200,height=250,bg='blue')
        self.toplevel.title('example2')
if __name__ == '__main__':
    root= Tk()
    MainWindowExample(root)
    root.mainloop()
This can also be done other ways....
Reply


Messages In This Thread
RE: Closing a window but not the whole program - by joe_momma - Jun-25-2019, 03:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 1,621 Aug-12-2023, 03:25 PM
Last Post: bianca
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 727 Aug-06-2023, 01:54 AM
Last Post: deanhystad
  [Tkinter] [split] [split] How to make a window stay on top knoxvilles_joker 10 6,033 May-22-2021, 03:54 AM
Last Post: knoxvilles_joker
Question closing a "nested" window with a button in PySimpleGUI and repeating this process Robby_PY 9 13,416 Jan-18-2021, 10:21 PM
Last Post: Serafim
  [PyQt] Received RunTimeError after script concludes, closing Dialog Window (clicking x-out) skipper0802 0 2,526 Oct-09-2020, 09:23 PM
Last Post: skipper0802
  Closing window on button click not working kenwatts275 4 3,666 May-03-2020, 01:59 PM
Last Post: deanhystad
  Running cli program within a wx window? t4keheart 2 2,728 Jan-23-2020, 04:50 PM
Last Post: buran
  tkinter window and turtle window error 1885 3 6,624 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Closing a window but not the whole program Wiggy1 2 4,940 Jan-11-2019, 12:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020