Python Tkinter:固定尺寸框架中的中心标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32582751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:51:07  来源:igfitidea点击:

Tkinter: Center label in frame of fixed size?

pythontkinter

提问by John

I am trying to create a frame of fixed size and place a text label in the center. I am not sure why this isn't working. I want the frame in the top left of the master frame, so NW is specified and that works fine. But changing the sticky direction of the label doesn't do anything. Help is appreciated.

我正在尝试创建一个固定大小的框架并在中心放置一个文本标签。我不确定为什么这不起作用。我想要主框架左上角的框架,所以指定了 NW 并且工作正常。但是改变标签的粘性方向没有任何作用。帮助表示赞赏。

self.f = Frame(self.master,bg="yellow",width=50,height=50)
self.f.grid(row=0,column=0,sticky="NW")
self.f.grid_propagate(0)
self.f.update()
self.l = Label(self.f,text="123",anchor="center",bg="yellow")
self.l.grid(column=0,row=0,sticky="wens")

采纳答案by VRage

You can use .place()for your label since your frame and your label have different parents. In place()you can use anchor="center"specify the startingpoint of your "anchor" with: xand y. Here is a working example:

.place()由于您的框架和标签具有不同的父级,因此您可以将其用于标签。在place()你可以使用anchor="center"指定你的“锚点”的起点:xy。这是一个工作示例:

app = Tk()
f = Frame(app,bg="yellow",width=50,height=50)
f.grid(row=0,column=0,sticky="NW")
f.grid_propagate(0)
f.update()
l = Label(f,text="123",bg="yellow")
l.place(x=25, y=25, anchor="center")
app.mainloop()