Python 如何使用Tkinter在窗口中垂直和水平居中放置小部件?
我需要在窗口内垂直居中放置3个标签。标签彼此居中,但固定在窗口顶部 我需要做什么才能让他们坐在窗户中间,(垂直和水平)? 这是我的密码: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
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()