General Nuisance
General Nuisance

Reputation: 211

how can I use the Button widget in python to place buttons in specific places on the screen?

thanks for helping...
I would like to place a button using tkinter's Button function. I am not exactly sure how to place a button though. I've actually put a button on the screen, using the pack() method, but am unsure of how to have more control over were it goes. here is my code

main = Tk()
canvas = Canvas(main, width = 500, height = 500)
canvas.pack() 
btn1 = Button (tk, text = "speak your name and click here!", command = moo_man)

btn1.pack()

btn1.place(bordermode = OUTSIDE, width = 270, height = 25) 

say for example, I wanted to put the button in the top right corner.
Thanks, all help is appreciated!!

Upvotes: 1

Views: 1895

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385910

place has options for relative placement. You can combine absolute and relative positioning, so you want the relative X coordinate to be 1.0 (ie: all the way to the right) and the absolute Y coordinate to be zero. Also, you want the coordinate to represent the upper-right corner of the button so that it is in the upper right of the parent window:

bt1.place(relx=1.0, y=0, anchor="ne")

For more information see http://effbot.org/tkinterbook/place.htm

On a side note: you should consider using pack or place in almost all situations. place is useful for some edge cases, but for most situations it's the least useful geometry manager. pack and grid make your UI much easier to create and modify. They are worth taking time to learn.

Upvotes: 1

Related Questions