By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

Tkinter button placement help

Thekid
100+
P: 145
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:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('500x500')
  5. message = 'Button test'
  6. Label(root, text=message).pack()
  7. Button(root, text='Save',command=None).pack(side=LEFT)
  8. Button(root, text='Exit',command=root.destroy).pack(side=BOTTOM)
  9.  
  10. root.mainloop()
  11.  
Running this will have the 'Save' button mid-left of the window and 'Exit' centered-bottom. If I move them around to 'RIGHT','TOP',etc..they still won't be where I want them which is side by side, centered-bottom. How can I correct this? Thanks
Sep 30 '10 #1

✓ answered by dwblas

Put both buttons in a frame and then place the frame.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('500x500')
  5.  
  6. message = 'Button test'
  7. Label(root, text=message).pack()
  8.  
  9. but_frame = Frame(root)
  10. Button(but_frame, text='Save',command=None).pack(side=LEFT)
  11. Button(but_frame, text='Exit',command=root.quit, bg='red', fg='white').pack(side=LEFT)
  12.  
  13. but_frame.pack(side=BOTTOM)
  14.  
  15. root.mainloop() 

Share this Question
Share on Google+
5 Replies


Thekid
100+
P: 145
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.

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('500x500')
  5. message = 'Button test'
  6. Label(root, text=message).pack()
  7. B1=Button(root, text='Save',command=None)
  8. B1.pack()
  9. B1.place(relx=0.4, rely=0.95, anchor=CENTER)
  10. B2=Button(root, text='Exit',command=root.destroy)
  11. B2.pack()
  12. B2.place(relx=0.5, rely=0.95, anchor=CENTER)
  13.  
  14. root.mainloop()
  15.  
  16.  
Sep 30 '10 #2

Expert 100+
P: 621
Put both buttons in a frame and then place the frame.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2.  
  3. root = Tk()
  4. root.geometry('500x500')
  5.  
  6. message = 'Button test'
  7. Label(root, text=message).pack()
  8.  
  9. but_frame = Frame(root)
  10. Button(but_frame, text='Save',command=None).pack(side=LEFT)
  11. Button(but_frame, text='Exit',command=root.quit, bg='red', fg='white').pack(side=LEFT)
  12.  
  13. but_frame.pack(side=BOTTOM)
  14.  
  15. root.mainloop() 
Sep 30 '10 #3

Thekid
100+
P: 145
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
Sep 30 '10 #4

Expert 100+
P: 621
Depending on what you are doing, you may want to use .grid() instead of .pack()
http://effbot.org/tkinterbook/grid.htm
http://infohost.nmt.edu/tcc/help/pub...-mgt.html#grid
Oct 1 '10 #5

Thekid
100+
P: 145
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
Oct 2 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.