Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a tkinter widget in a sticky frame

I'm writing a game in python3 using tkinter, and I'm having a little trouble getting the grid to do what I want it to do. I've looked through at least five pages of google results including stack overflow answers for every variant of how to ask this question I can think of. I finally just gave in and created this account to ask about this.

What I've got: A button (newGameButton) and a label (messageBox) centered in a frame (topBar) which is itself centered but does not span the entire window (contentFrame) horizontally.

The best I've managed to get (by putting sticky=W+E on topBar): Frame now spans the whole window, button and label remain the same size (sticky on the label didn't do a thing, and sticky on the button only made it as wide as the label), and are now stuck against the left side of the topBar.

What I want it to do: have the frame span the whole window, with the label also spanning the whole window, and the button centered.

The reason topBar is columnspan=23 is that the rest of the stuff in the content frame is 23 columns wide (including the 0 column). The reason I have the button and label in a frame is that I'd like that entire box surrounding them to have a border effect.

Code:

self.contentFrame = Frame(self.root)
self.contentFrame.grid(row=0, column=0)
self.topBar = Frame(self.contentFrame, border=2, relief=RAISED)
self.topBar.grid(row=0, column=0, columnspan=23)
self.newGameButton = Button(self.topBar, text="New Game")
self.newGameButton.grid(row=0, column=0)
self.messageBox = Label(self.topBar, textvariable=self.message, height=2)
self.messageBox.grid(row=1, column=0, sticky=W+E)

Has anyone got any ideas? I'm pretty desperate at this point.

like image 277
littlefiredragon Avatar asked Nov 13 '14 05:11

littlefiredragon


People also ask

How do you center a frame in tkinter?

To position the Tkinters widgets, we can use place() geometry manager, where we will specify the anchor property. It can take (NW, N, NE, W, CENTER, E, SW, S, SE) as the position of the widget.

How do you center something in tkinter?

To configure and align the text at the CENTER of a Tkinter Text widget, we can use justify=CENTER property.

How do you center a GUI in Python?

Build A Paint Program With TKinter and Python In order to place a tkinter window at the center of the screen, we can use the PlaceWindow method in which we can pass the toplevel window as an argument and add it into the center. We can also set the window to its center programmatically by defining its geometry.

How do you center a grid label?

Just give the row and column in which the label appears a weight of 1 and be sure not to specify a sticky attribute. By default the row and column weight is set to zero, which means neither will grow to fill in any extra space in the widget.


1 Answers

The problem is that none of your columns have any weight. It is the weight attribute that decides what columns (and rows) get any extra space. Since none of your columns have a non-zero weight, none of the extra space is allocated to them, so they stay as small as they can be.

As a rule of thumb, you should always give at least one row and one column in a frame a non-zero weight. In your case, giving row 0 and column 0 a weight of 1 for all of the frames seems to work:

self.root.grid_columnconfigure(0, weight=1)
self.root.grid_rowconfigure(0, weight=1)
self.contentFrame.grid_columnconfigure(0, weight=1)
self.contentFrame.grid_rowconfigure(0, weight=1)
self.topBar.grid_columnconfigure(0, weight=1)
self.topBar.grid_rowconfigure(0, weight=1)
like image 125
Bryan Oakley Avatar answered Sep 22 '22 20:09

Bryan Oakley