我需要在窗口中垂直居中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()
答案 0 :(得分:1)
我认为在这种情况下,您只需使用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()