Reputation: 1070
I need to center 3 labels vertically within the window. The labels are centered on-top of each other, but they are fixed at the top of the window.
What do I need to do to have them sit right in the middle of the window, (vertically and horizontally)?
Here is my code:
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()
Upvotes: 3
Views: 7169
Reputation: 15868
I think that in this case you can simply use a Frame
widget as the parent of the labels and then pack the frame by setting the expand
option to 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()
Upvotes: 3