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

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

    Open Another Pthon Window - How To


    I need help getting started on this topic. I have a Python form
    with listboxes and tkinter widgets. A list of listbox selectons is
    compiled when an 'Add' button is clicked.
    I want to feed that list to another form when the 'Edit'
    button is clicked, and this list of mp3 titles can be viewed, or
    reordered, randomized, items deleted, saved and/or played
    from Edit mode.

    What should I be studying to learn about multiple-form
    applications? Anyone have a simple example? Here is code
    you might use to edit to demonstrate the concept.

    My primary question is about the mechanics of opening another
    form, and are there any safe-guards against returning to
    form #1 while form #2 is open.

    Thanks.
    Code:
    from tkinter import *
    
    root = Tk()
    root.geometry('400x300+20+20')
    
    def open_next():
        """
        This should open another screen that will have
        a listbox and other widgets. Screen #2 will display
        in a listbox, items compiled from listbox selections
        in screen #1; it will be a list of mp3 titles that
        can be reordered, deleted, played, or saved to a
        permanent playlist.
        """
        
        pass
    
    btnNext = Button(root, text='Open Next Screen',
            font=('Helvetica', 9, 'normal'),
            width = 20,
            command=open_next)
    
    btnNext.pack(padx=4, pady=12)
    btnNext.place(x=120, y = 260)
    
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    look at the toplevel widget
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    Thankyou rrashkin for that tip about using the toplevel widget
    to open another window.
    I am posting experimental code to show how a window can be
    created and appear to be modal by hiding the parent window.
    Code:
    #open child window-simulate modal by hiding parent window 
    from tkinter import *
    
    root = Tk()
    
    x,y = 100,100
    w,h = 400,300
    
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    
    class new_window:
        """
        Opens a new window and temporarily hides the parent window
        """
    
        def __init__(self,root):
            
            self.top = Toplevel()
    
            #method to bypass the Windows close icon:
            try:
                self.top.protocol("WM_DELETE_WINDOW", self.destroy_window)
            except:
                pass  #possibly catch error if a non-Windows OS
            
            self.top.geometry('%dx%d+%d+%d' % (w, h, x, y))
            self.top.title("You've opened a new window")
    
            msg = Message(self.top, text="Window No 2", bg='red', width=80)
            msg.pack(expand=YES)
    
            button = Button(self.top, text="Close This Window", width=24, command=self.destroy_window)
            button.pack(side=BOTTOM, padx=4, pady=4)
     
            root.withdraw()    #hide window #1 until window #2 is closed
            
        def destroy_window(self):
            """
            make window #1 visible.
            destroy window #2.
            """
    
            root.update()
            root.deiconify()
        
            self.top.destroy()
    
        
    btnNext = Button(root, text='Open New Window',
        width = 20, command=lambda: new_window(root))
    btnNext.pack(side=TOP, padx=4, pady=4)
    
    btnExit = Button(root, text='Exit',
        width = 8, command=lambda: root.destroy())
    btnExit.pack(side=BOTTOM, padx=4, pady=4)
    
    msg = Message(root, text="Window No 1", width=80)
    msg.pack(expand=YES)    
    
    root.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo