简体   繁体   English

Tkinter将窗口中的所有小部件居中

[英]Tkinter centre all widgets in window

I am building a GUI with tkinter using grid. 我正在使用tkinter使用网格构建GUI。 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. 所有问题和按钮的长度都是不同的,因此对于不占据整个窗口的较短问题和按钮,我希望它们位于x轴的中央。

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. 我研究了这个问题,并找到了一个使用place的解决方案,但是由于它当前正在使用网格,因此我不得不重写整个应用程序。

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. 我想将所有内容都置于createWidgets内部,以便它始终始于y轴的顶部,但始终位于x轴的中间。 I use root.geometry("250x160") to size the window right now and root.resizable(0, 0) to stop variables resizing it. 我现在使用root.geometry("250x160")调整窗口大小,并使用root.resizable(0, 0)停止调整窗口大小的变量。

I assume your class inherits from tk.Frame . 我假设您的类继承自tk.Frame You don't have to modify anything inside the class - rather, change how you pack your frame by passing fill and expand . 您无需在类中进行任何修改,而是通过传递fillexpand更改pack框架的方式。

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: 如果即使在根级别也确实要使用grid ,则可以使用以下命令:

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: pack几何图形管理器会将所有小部件集中在如下所示的列中:

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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