#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2

    Split code into separate Modules-can Parent Window be hidden and restored?


    Question: can the window from main_mod.py be withdrawn until
    the window from child_mod.py is closed? I was unable to code
    child_mod to do anything with the window in main_mod, to make
    the main window visible again in the child window's
    close_window function.

    Code:
    #main_mod.py  - see also child_mod.py
    #goal: using separate modules to open each window, can methods root.withdraw() and
    #root.update() & root.deiconffy() hide the main window while child window is visible,
    #and make the main window visible after child window is closed?
    
    from tkinter import *
    root = Tk()
    
    class main_window():
    
        def __init__(self, root):
            self.main_label = Label(root, text='Main Window',
                bg='Azure', font=('Helvetica', 20), padx=8, pady = 4)
            self.main_label.pack(side=TOP, anchor=N)
    
            close_main_btn = Button(root, text='Close Main Window',
                font=('Helvetica', 16), command=self.close_window)
            close_main_btn.pack(side=BOTTOM, anchor=S)
    
            main_btn = Button(root, text='Open Child Window',
                font=('Helvetica', 16), command=self.open_child_window)
            main_btn.pack(side=BOTTOM, anchor=S, fill=X, expand=YES)
    
        def close_window(self):
            root.destroy()
            
        def open_child_window(self):
            import child_mod
            cw = child_mod.child_window()
            
            
    mw = main_window(root)
    
    root.mainloop()
    Code:
    #child_mod.py - called from main_mod.py
    
    from tkinter import *
    
    class child_window():
    
        def __init__(self):
            self.top = Toplevel()
            self.top.child_label = Label(self.top, text='Child Window',
                bg='Gold', font=('Helvetica', 20), padx=8, pady = 4)
            self.top.child_label.pack(side=TOP, anchor=N, fill=X, expand=YES)
    
            self.top.child_btn = Button(self.top, text='Close Child Window',
                font=('Helvetica', 16), command=self.close_window)
            self.top.child_btn.pack(side=BOTTOM, anchor=S)
    
        def close_window(self):
            self.top.destroy()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    Using slightly (painfully, however) modified from daniweb,
    Code:
    import tkinter as tk
    
    class Demo1:
        def __init__(self, master):
            self.master = master
            #print(dir(master))
            self.frame = tk.Frame(self.master)
            tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window).pack()
            tk.Button(self.frame, text = 'Exit', width = 25, command = master.destroy).pack()
            self.frame.pack()
        def new_window(self):
            master = self.master
            Demo2(tk.Toplevel(master))
    
    class Demo2:
        def __init__(self, master):
            self.parent = master.nametowidget(master.winfo_parent())
            self.master = master
            self.frame = tk.Frame(self.master)
            self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
            self.quitButton.pack()
            self.frame.pack()
            self.parent.withdraw()
        def close_windows(self):
            self.parent.deiconify()
            self.master.destroy()
    
    def main():
        root = tk.Tk()
        app = Demo1(root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    76
    Rep Power
    2
    be4...you did it. Your response helped produce this example
    of splitting the code into 2 modules and being able to show a
    window from module #1, and then hide that window when a
    window defined in module #2 is displayed.
    Code:
    #pywin1.py  -  also opens window #2 in pywin2.py via button click
    import tkinter as tk
    import pywin2
    
    class Demo1:
        def __init__(self, master):
            self.master = master
            self.frame = tk.Frame(self.master)
            tk.Button(self.frame, text = 'New Window',
                width = 25, command = self.new_window).pack()
            tk.Entry(self.frame, width=30).pack()
            tk.Button(self.frame, text = 'Exit',
                width = 25, command = master.destroy).pack()
            self.frame.pack()
        def new_window(self):
            master = self.master
            pywin2.Demo2(tk.Toplevel(master))
    
    def main():
        root = tk.Tk()
        app = Demo1(root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    Code:
    #pywin2.py  - imported by pywin1.py
    import tkinter as tk
    
    class Demo2:
        def __init__(self, master):
            self.parent = master.nametowidget(master.winfo_parent())
            self.master = master
            self.frame = tk.Frame(self.master)
            self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
            self.quitButton.pack()
            self.frame.pack()
            self.parent.withdraw()
        def close_windows(self):
            self.parent.deiconify()
            self.master.destroy()

IMN logo majestic logo threadwatch logo seochat tools logo