tkinter label and button grid placing

Question:

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?

Asked By: xlmaster

||

Answers:

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",))
Answered By: Bryan Oakley
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.