473,473 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Tkinter button placement help

Thekid
145 New Member
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
5 17340
Thekid
145 New Member
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
dwblas
626 Recognized Expert Contributor
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
145 New Member
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
dwblas
626 Recognized Expert Contributor
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
145 New Member
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

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

Similar topics

2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
2
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make....
6
by: Elaine Jackson | last post by:
I've got a script where a button gets pushed over and over: to cut down on the carpal tunnel syndrome I'd like to have the button respond to presses of the Enter key as well as mouse clicks; can...
1
by: Elaine Jackson | last post by:
In the script I'm writing, there's a Tkinter Button that contains a square image at some times and is blank at other times. Is there a smart way to make its dimensions stay the same throughout all...
0
by: Casey Hawthorne | last post by:
Is there a better way to delete the image from a Tkinter button other than the following: - reconstructing the button - image=blank.gif -- Regards,
3
by: Shankar Iyer (siyer | last post by:
Hi, I have another Tkinter-related question. At the beginning of my program, a tkinter window is created with six buttons. Each of these buttons is assigned a function that should be executed...
2
by: Fuzzyman | last post by:
Hello all, I have some Tkinter buttons that display images. I would like to change these to 'active' images when the mouse is over the button. I see that the button widget can take an...
8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
5
by: vagrantbrad | last post by:
I've created a short test program that uses tkFileDialog.askdirectory to help the user input a path into a tk entry widget. The problem I'm having is that when I run the code as listed below, the...
4
by: Edward K. Ream | last post by:
Hello all, Creating a 'Help' menu 'by hand' on the Mac does not work, or rather, it creates a *second* Help menu. There are hints about how to do this at:
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.