Tkinter button placement help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    Tkinter button placement help

    I'm working on a Tkinter project that has several toplevel windows and I'm having a problem placing the buttons on the windows where I want them to be. For now I'm working on getting the templates the way I want them and once that's done I plan on going back and adding code for MySQL to store the info. Anyway, here's an example of a simple root window with 2 buttons:

    Code:
    from Tkinter import *
    
    root = Tk()
    root.geometry('500x500')
    message = 'Button test'
    Label(root, text=message).pack()
    Button(root, text='Save',command=None).pack(side=LEFT)
    Button(root, text='Exit',command=root.destroy).pack(side=BOTTOM)
    
    root.mainloop()
    Running this will have the 'Save' button mid-left of the window and 'Exit' centered-bottom. If I move them around to 'RIGHT','TOP',e tc..they still won't be where I want them which is side by side, centered-bottom. How can I correct this? Thanks
  • Thekid
    New Member
    • Feb 2007
    • 145

    #2
    I've found the '.place()' command which seems to somewhat be what I'm looking for so I guess I'll just play around with the positioning.

    Code:
    from Tkinter import *
     
    root = Tk()
    root.geometry('500x500')
    message = 'Button test'
    Label(root, text=message).pack()
    B1=Button(root, text='Save',command=None)
    B1.pack()
    B1.place(relx=0.4, rely=0.95, anchor=CENTER)
    B2=Button(root, text='Exit',command=root.destroy)
    B2.pack()
    B2.place(relx=0.5, rely=0.95, anchor=CENTER)
     
    root.mainloop()

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Put both buttons in a frame and then place the frame.
      Code:
      from Tkinter import *
       
      root = Tk()
      root.geometry('500x500')
      
      message = 'Button test'
      Label(root, text=message).pack()
      
      but_frame = Frame(root)
      Button(but_frame, text='Save',command=None).pack(side=LEFT)
      Button(but_frame, text='Exit',command=root.quit, bg='red', fg='white').pack(side=LEFT)
      
      but_frame.pack(side=BOTTOM)
       
      root.mainloop()

      Comment

      • Thekid
        New Member
        • Feb 2007
        • 145

        #4
        Ah...very interesting! I suppose I could do that and if need be, also use the 'place' function if I want to move the frame slightly! Thanks

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          Depending on what you are doing, you may want to use .grid() instead of .pack()

          Comment

          • Thekid
            New Member
            • Feb 2007
            • 145

            #6
            The .grid() method is better in this situation than the .place(), especially since I have many entry fields so I'll change the .pack() to it. Thanks

            Comment

            Working...