How to center a tkinter widget in a sticky structure

I am writing a game in python3 using tkinter , and I am having a little trouble getting the grid to do what I want. I looked at least five pages of Google results, including stack overflow answers for each option on how to ask this question that I can think of. I finally just gave up and created this account to ask about it.

What I have: button (newGameButton) and a label (messageBox) centered in the frame (topBar), which in itself, but does not cover the entire window (contentFrame) horizontally.

The best I managed to get (adding sticky=W+Eto the topBar): Frame now covers the entire window, the button and label remain the same size (the sticker on the label did nothing and the sticky button just made it as wide as the label), and now they are attached to the left side of the top panel.

I want to do this: the frame covers the entire window, and the shortcut also covers the entire window, and the button in the center.

The reason for topBar columnspan=23is that the rest of the material in the content frame is 23 columns (including column 0). The reason I have a button and a label in the frame is because I would like this whole box around them to have a border effect.

the 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)

Does anyone have any ideas? I am pretty desperate at this moment.

+4
2

, . weight, , ( ) . , , , .

, . , 0 0, 1 , , :

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)
+2

" tkinter grid" google, .

"", , , > . , , > , .

, .

class Test():
    def __init__(self,root):
        self.root = root
        self.root.columnconfigure(0, weight=1)
        self.root.config(bg='green')
        self.message = 'test message'

        self.contentFrame = Frame(self.root)
        self.contentFrame.config(background='black',borderwidth=5,relief ='sunken')
        self.contentFrame.grid(row=0, column=0, sticky='news')
        self.contentFrame.columnconfigure(0, weight=1)

        self.topBar = Frame(self.contentFrame, border=2, relief=RAISED)
        self.topBar.grid(row=0, column=0, columnspan=23,sticky=W+E)
        self.topBar.config(background='blue')
        self.topBar.columnconfigure(0, weight=1)

        self.newGameButton = Button(self.topBar, text="New Game")
        self.newGameButton.grid(row=0, column=0)
        self.newGameButton.config(background='red')

        self.messageBox = Label(self.topBar, text=self.message, height=2)
        self.messageBox.grid(row=1, column=0, columnspan=1,sticky=W+E)
        self.messageBox.config(background='yellow')

Test(root)
0

All Articles