简体   繁体   中英

Tkinter centre all widgets in window

I am building a GUI with tkinter using grid. I need to centre all my objects in the centre of the window. All questions and buttons are different lengths so for the shorter ones that don't take up the whole window I want them to be central on the x axis.

I've looked into the issue and found a somewhat solution using place however I would have to rewrite the whole app as it is currently using grid.

def createWidgets(self):

        self.question = tk.Label(self, text="What is Prague the capital of?\n")
        self.question.grid(row=1, column=1, columnspan=2)

        self.option1 = tk.Button(self, width=12)
        self.option1["text"] = "Romania"
        self.option1["command"] = self.wrong1
        self.option1.grid(column=1, row=2, padx=1, pady=1)

        self.option2 = tk.Button(self, width=12)
        self.option2["text"] = "Slovakia"
        self.option2["command"] = self.wrong1
        self.option2.grid(column=2, row=2, padx=1, pady=1)

        self.option3 = tk.Button(self, width=12)
        self.option3["text"] = "Czech Republic"
        self.option3["command"] = self.correct1
        self.option3.grid(column=1, row=3, padx=1, pady=1)

        self.option4 = tk.Button(self, width=12)
        self.option4["text"] = "Ukraine"
        self.option4["command"] = self.wrong1
        self.option4.grid(column=2, row=3, padx=1, pady=1)

I want to centre everything inside of createWidgets so that it always starts at the top of the y axis but in the middle of the x axis. I use root.geometry("250x160") to size the window right now and root.resizable(0, 0) to stop variables resizing it.

I assume your class inherits from tk.Frame . You don't have to modify anything inside the class - rather, change how you pack your frame by passing fill and expand .

import tkinter as tk

class MainFrame(tk.Frame):
    def __init__(self,master=None,**kwargs):
        tk.Frame.__init__(self,master,**kwargs)
        self.question = tk.Label(self, text="What is Prague the capital of?\n")
        self.question.grid(row=1, column=1, columnspan=2)

        self.option1 = tk.Button(self, width=12)
        self.option1["text"] = "Romania"
        #self.option1["command"] = self.wrong1
        self.option1.grid(column=1, row=2, padx=1, pady=1)

        self.option2 = tk.Button(self, width=12)
        self.option2["text"] = "Slovakia"
        #self.option2["command"] = self.wrong1
        self.option2.grid(column=2, row=2, padx=1, pady=1)

        self.option3 = tk.Button(self, width=12)
        self.option3["text"] = "Czech Republic"
        #self.option3["command"] = self.correct1
        self.option3.grid(column=1, row=3, padx=1, pady=1)

        self.option4 = tk.Button(self, width=12)
        self.option4["text"] = "Ukraine"
        #self.option4["command"] = self.wrong1
        self.option4.grid(column=2, row=3, padx=1, pady=1)

root = tk.Tk()
frame = MainFrame(root)
frame.pack(fill=tk.Y,expand=True)
root.mainloop()

If you really want to use grid even at the root level, you can use the following:

root = tk.Tk()
frame = MainFrame(root)
frame.grid(row=0,column=0,sticky="ns")
root.grid_columnconfigure(0,weight=1)
root.mainloop()

the pack geometry manager will center all your widgets in a column that will look like this:

在此处输入图片说明

import tkinter as tk


class MVCE(tk.Tk):

    def __init__(self):
        super().__init__()
        self.create_widgets()
        self.mainloop()

    def create_widgets(self):

        self.question = tk.Label(self, text="What is Prague the capital of?\n")
        self.question.pack()

        self.option1 = tk.Button(self, width=12)
        self.option1["text"] = "Romania"
        self.option1["command"] = self.wrong1
        self.option1.pack()

        self.option2 = tk.Button(self, width=12)
        self.option2["text"] = "Slovakia"
        self.option2["command"] = self.wrong1
        self.option2.pack()

        self.option3 = tk.Button(self, width=12)
        self.option3["text"] = "Czech Republic"
        self.option3["command"] = self.correct1
        self.option3.pack()

        self.option4 = tk.Button(self, width=12)
        self.option4["text"] = "Ukraine"
        self.option4["command"] = self.wrong1
        self.option4.pack()

    def wrong1(self):
        print('wrong1')

    def correct1(self):
        print('correct1')

MVCE()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM