简体   繁体   中英

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.

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.

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. Therefore it is in column 1 and column 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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