简体   繁体   English

Python:如何在 tkinter 窗口中居中标签

[英]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 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 )正如您在代码中看到的, wh定义了窗口的大小, xbiasybias有一个表达式来居中testframe (都包含alpha1alpha2作为文本大小的校正因子)

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.我正在寻找alpha1alpha2 (现在等于 1 )的数学表达式......或者构建这样一个弹出窗口的更好方法。

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)

Have you considered using the .pack() method instead for this. 您是否考虑过.pack()使用.pack()方法。 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. 如果我们为.rowconfigure().columnconfigure()分配的weight不为0则指定的行和列将扩展以填充窗口中为其指定的空间。

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 ). A部分)创建一个具有特定大小hw窗口(因为它是一个弹出窗口-在此示例中,我禁用了它的大小调整)。 Inside the frame- a Label and a Button : 在框架内部- LabelButton

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. B部分)为了获得框架的尺寸(包括内部的LabelButton ),我们使用testframe.update()然后使用testframe.winfo_width() testframe.winfo_height()获得框架的更新值。 xbias and ybias calculates the center to place the frame in the middle: xbiasybias将框架放置在中间:

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()

A little late but I've found that using the anchor makes it easier to center a label in the window.有点晚了,但我发现使用anchor可以更轻松地将标签在窗口中居中。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM