Home > database >  tkinter label and button grid placing
tkinter label and button grid placing

Time:11-10

i have created window, in that window i put frame. Then i want to create two labels after them button widget. But, button widget appears upper than second label widget even though i put btn in row=2, and label2 in row=1. It 's hard for me to get why?

from  tkinter import *

window=Tk()
window.geometry('620x540 33 33')

var_pg=StringVar()
var_pg.set('Page 001 placeholder')

class lbl_custom(Label):
    def __init__(self,frame_window):
        super().__init__()
        self['width']=33
        self['font']='Segoe 12'

frame_submit=Frame(window)

lbl_page_num01=Label(frame_submit,width=33,textvariable=var_pg,font='Segoe 12')
lbl_page_num02=lbl_custom(frame_submit)
lbl_page_num02['text']='Page 002 placeholder'

btn_submit=Button(frame_submit,width=33,relief=RAISED,text='submit')

frame_submit.grid(row=0,column=0)
lbl_page_num01.grid(row=0,column=0)
lbl_page_num02.grid(row=1,column=0)

btn_submit.grid(row=2,column=0)

window.mainloop()

Now, it is for sure that. Every label widget which is created by class inheritance is going down than button nonetheless grid placeholder. But if I create it as Label() class it will be upper than button. Why it is so?

CodePudding user response:

You must pass the parent object when calling super, otherwise your custom object will always be a child of the root window.

class lbl_custom(Label):
    def __init__(self,frame_window):
        super().__init__(frame_window)
        #                ^^^^^^^^^^^^

You can pass other options as well, which will save you a couple lines of code:

class lbl_custom(Label):
    def __init__(self,frame_window):
        super().__init__(frame_window, width=33, font=("Segoe 12",))
  • Related