Help with Tkinter Windows

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Billy Blue
    New Member
    • Mar 2011
    • 2

    Help with Tkinter Windows

    Hello

    I am currently designing my first GUI and am having difficulties opening and closing new windows. I have been looking for ideas for sometime now, however when ever I fit them into my code it does not seem to work.

    What I am trying to do is open up a fresh window when a button is clicked and closing or hiding the master window.

    I've attached my code and any assistance would be appriciated.

    B
    Attached Files
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    You can use withdraw and deiconify on a frame. A simple example that will close and open a Frame or a top level/root ("window" is a generic term and not specific enough for your question). You can also create an entirely new frame within Toplevel with a button click's callback.
    Code:
    import Tkinter
    
    class TestClass():
    
       def __init__(self):
          self.top = Tkinter.Tk()
          self.top.title("Test")
          self.top.geometry("200x150+500+5")
    
          frame_1 = Tkinter.Toplevel()
          frame_1.title("frame 1")
    
          but_1 = Tkinter.Button(self.top, text='DeIconify',                        
                  command=frame_1.deiconify)                                        
          but_1.pack(side="bottom", fill=Tkinter.X, expand=1) 
    
          but_2 = Tkinter.Button(self.top, text='Quit',
                  command=self.top.quit, bg='blue', fg='yellow')
          but_2.pack(side="bottom", fill=Tkinter.X, expand=1)
    
          can_1 = Tkinter.Canvas(frame_1)
          can_1.pack()
          can_1.create_rectangle(50,50,100,100, outline='white', fill='black')
    
          cont = Tkinter.Button(frame_1, text='Withdraw',
                  command=frame_1.withdraw, bg='red', fg='white' )
          cont.pack(side="bottom", fill=Tkinter.X, expand=1)
    
    
          self.top.mainloop()
             
    ##====================================================================
    if __name__ == '__main__':
       CT=TestClass()

    Comment

    • Billy Blue
      New Member
      • Mar 2011
      • 2

      #3
      Hi dwblas

      Many thanks for taking the time to reply. I'm not sure if the code you have sent will do what I intend it to.

      Effectivley, upon the click of a button I would like one frame to close and also another frame to open a bit like a 'next' button.

      Kind regards

      B

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Use deiconify (or destroy) to close one frame, and add a new Frame class for the new window.

        Comment

        Working...