guyd
guyd

Reputation: 733

Python : How to center Label in tkinter window

I'm trying to construct a popup window, containing varying text message for user to respond.

I seek a way to center the text (Label) and Button inside that window- with no success.

Popup window

Popup Window has a determind size. Centering the frame inside it should take into account width and height of textlabel ( defined in amount of letters ).

As you can see in code, w, h defines window's size, xbias,ybias have an expression to center testframe (both contains alpha1, alpha2 as a correction factor for text's size )

I'm seeking the mathematical expression for alpha1, alpha2 ( which equal for 1 for now )... or a better way to construct such a popup window.

root = Tk()
w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))
root.configure(bg='lightgreen')

txt = StringVar()
txt.set("This is an error message")

alpha1 = 1
alpha2 = 1
xbias = int(w) / 2 - (len(txt.get()) / 2) * alpha1
ybias = int(h) / 2 - alpha2

testframe = ttk.Frame(root)
testframe.grid(row=0, column=1, pady=ybias, padx=xbias)

label1 = ttk.Label(testframe, textvariable=txt)
label1.grid(row=0, column=0)

Upvotes: 2

Views: 21130

Answers (3)

Julian wandhoven
Julian wandhoven

Reputation: 288

A little late but I've found that using the anchor makes it easier to center a label in the window.

from tkinter import *

root = Tk()
w = 100
h = 100
root.geometry(f"{w}x{h}")

label = Label(root, text="text")
label.place(anchor=CENTER, relx=0.5, rely=0.5)

root.mainloop()

Upvotes: 0

guyd
guyd

Reputation: 733

A way to center Frame inside root window can be done in this manner:

Part A) Create A window with a specific size h, w (since it is a popup - in this example I disable resizing it ). Inside the frame- a Label and a Button:

root = Tk()
w = '200'
h = '80'
root.geometry('{}x{}'.format(w, h))
root.configure(bg='lightgreen')    ###To diff between root & Frame
root.resizable(False, False)

txt = StringVar()
txt.set("This is an error message")

testframe = ttk.Frame(root)
testframe.grid(row=0, column=1)

label1 = ttk.Label(testframe, textvariable=txt)
label1.grid(row=0, column=0, pady=10)

ok_button = ttk.Button(testframe, text="OK", command=root.destroy)
ok_button.grid()

Part B) In order to get frame's dimensions ( including Label and Button inside ) we use testframe.update() and then testframe.winfo_width() testframe.winfo_height() to obtain frame's updated values. xbias and ybias calculates the center to place the frame in the middle:

testframe.update()

xbias = int(w) / 2 - testframe.winfo_width() / 2
ybias = int(h) / 2- testframe.winfo_height() / 2
testframe.grid(row=0, column=1, pady=ybias, padx=xbias)

root.mainloop()

Upvotes: 0

Ethan Field
Ethan Field

Reputation: 4730

Have you considered using the .pack() method instead for this. You could achieve the desired effect far easier that way:

from tkinter import *

root = Tk()
top = Toplevel(root)

w = '400'
h = '100'
top.geometry('{}x{}'.format(w, h))

frame = Frame(top)

label = Label(frame, text="This is an error message")
button = Button(frame, text="Ok")

frame.pack(expand=True) #expand assigns additional space to the frame if the parent is expanded
label.pack()
button.pack()

root.mainloop()

After some research, doing this with grid is an awful lot easier than expected, see below:

from tkinter import *

root = Tk()

w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))

label = Label(root, text="text")
label.grid(column=0, row=0)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

root.mainloop()

If we assign .rowconfigure() and .columnconfigure() a weight which is not 0 then the specified row and column will expand to fill the space given to it in the window.

Upvotes: 7

Related Questions