VirtualUser
VirtualUser

Reputation: 215

How to center the button before drawing it?

I am trying to set position of button in the center of my GUI window:

capture_button = Button(self, text="Capture", command=self.client_exit)
capture_button.place(x=100, y=100)

But there is a problem - button's centre is not at (100,100) but its left top corner is at (100,100) so there is no symmetry. So I decided to do something like that:

capture_button.place(x=100+buttonsize.x/2, y=100 - buttonsize.y/2)

I know that I can use

capture_button.winfo_width

but that works after first draw - how can I do this before drawing, just in ctor of python class? Or there are other ways like setOrigin(centre) and then setting position?

Upvotes: 0

Views: 28

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

If your real goal is simply to center the button using place, you can use relative coordinates instead of relying on computing the width of the button.

capture_button.place(anchor="c", relx=.5, rely=.5)

Upvotes: 1

Related Questions