简体   繁体   English

如何使用网格在 tkinter colspan 中居中标签?

[英]How to center a label in a tkinter colspan using grid?

I'm trying to center the "This should be in center" label on my python numberpad I made with tkinter and grid.我试图将“这应该在中心”标签放在我用 tkinter 和网格制作的 python 数字键盘上。

Currently, it looks like this: The label is not in center目前,它看起来像这样:标签不在中心

I've tried to add anchor=CENTER to the attributes of the label, but it made no change.我尝试将anchor=CENTER添加到标签的属性中,但没有任何改变。

Here is the relevant code for the numberpad:这是数字键盘的相关代码:

class App:
numdigs = 0
def __init__(self, root):
    frame = Frame(root)
    grid=Frame(frame)
    b = Label(root, text="This should be in center")
    b.grid(row=0, column=1, columnspan=2)
    b = Button(root, text="1", width=10, command= lambda *args: self.setVar(1))
    b.grid(row=1, column=0)
    b = Button(root, text="2", width=10,command= lambda *args: self.setVar(2))
    b.grid(row=1, column=1,)
    b = Button(root, text="3", width=10,command= lambda *args: self.setVar(3))
    b.grid(row=1, column=2,)
    b = Button(root, text="4", width=10,command= lambda *args: self.setVar(4))
    b.grid(row=2, column=0,)
    b = Button(root, text="5", width=10,command= lambda *args: self.setVar(5))
    b.grid(row=2, column=1,)
    b = Button(root, text="6", width=10,command= lambda *args: self.setVar(6))
    b.grid(row=2, column=2,)
    b = Button(root, text="7", width=10,command= lambda *args: self.setVar(7))
    b.grid(row=3, column=0,)
    b = Button(root, text="8", width=10,command= lambda *args: self.setVar(8))
    b.grid(row=3, column=1,)
    b = Button(root, text="9", width=10,command= lambda *args: self.setVar(9))
    b.grid(row=3, column=2,)
    b = Button(root, text="*", width=10,command= lambda *args: self.setVar("*"))
    b.grid(row=4, column=0,)
    b = Button(root, text="0", width=10,command= lambda *args: self.setVar(0))
    b.grid(row=4, column=1,)
    b = Button(root, text="#", width=10,command= lambda *args: self.setVar("#"))
    b.grid(row=4, column=2,)

Can you help me get back on track here?你能帮我回到正轨吗?

You put the label in column 1 and told it to span two columns.您将标签放在第 1 列并告诉它跨越两列。 Therefore it is in column 1 and column 2.因此它在第 1 列和第 2 列中。

I assume you want it to span all three columns, so the solution is to move the label to column zero and have it span three columns:我假设您希望它跨越所有三列,因此解决方案是将标签移动到第 0 列并使其跨越三列:

b.grid(row=0, column=0, columnspan=3)

You could easily solve with this你可以很容易地解决这个问题

lbl_address = ttk.Label(addmission_form,text="Address")
    lbl_address.grid(row=7,column=0,padx=10,pady=10)
    txtBox_address = ttk.Entry(addmission_form ,width=100)
    txtBox_address.grid(row=7,column=1,columnspan=3,padx=40,pady=10)

the out of this code will be this enter image description here此代码中的内容将在此处输入图像描述

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

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