Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Tkinter在窗口中垂直和水平居中放置小部件?_Python_Tkinter - Fatal编程技术网

Python 如何使用Tkinter在窗口中垂直和水平居中放置小部件?

Python 如何使用Tkinter在窗口中垂直和水平居中放置小部件?,python,tkinter,Python,Tkinter,我需要在窗口内垂直居中放置3个标签。标签彼此居中,但固定在窗口顶部 我需要做什么才能让他们坐在窗户中间,(垂直和水平)? 这是我的密码: from tkinter import * root = Tk() root.geometry("200x200") root.title("Question 2") root.configure(background="green") Label(root, text = "RED", fg="red", bg="black").pack() Label

我需要在窗口内垂直居中放置3个标签。标签彼此居中,但固定在窗口顶部

我需要做什么才能让他们坐在窗户中间,(垂直和水平)?

这是我的密码:

from tkinter import *

root = Tk()

root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")
Label(root, text = "RED", fg="red", bg="black").pack()
Label(root, text = "WHITE", fg="white", bg="black").pack()
Label(root, text = "BLUE", fg="blue", bg="black").pack()

root.mainloop()

我认为在这种情况下,您可以简单地使用
Frame
小部件作为标签的父项,然后通过将
expand
选项设置为
True
来打包框架:

from tkinter import *


root = Tk()

root.geometry("200x200")
root.title("Question 2")
root.configure(background="green")

parent = Frame(root)
Label(parent, text = "RED", fg="red", bg="black").pack(fill="x")
Label(parent, text = "WHITE", fg="white", bg="black").pack(fill="x")
Label(parent, text = "BLUE", fg="blue", bg="black").pack(fill="x")
parent.pack(expand=1)  # same as expand=True

root.mainloop()