Home > front end >  TKinter weird behavior using grid location manager with arguments row=0, column=0
TKinter weird behavior using grid location manager with arguments row=0, column=0

Time:11-28

I'm using two frames to organize my main frame into two subframes. One subframe is one the left and it contains a few buttons and a label. The subframe on the right contains a treeview.

The items in the left_frame don't show up when I use the arguments row=0, column=0 as show in the example below.

class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        # containers
        self.left_frame = ttk.Frame() # contains all the buttons and icons on the left
        self.left_frame.grid(row=0, column=0)

        self.right_frame = ttk.Frame() # contains the spreadsheet preview
        self.right_frame.grid(row=0, column=1)

        # labels
        self.label = ttk.Label(self.left_frame, text="Platzhalter für Icon")
        self.label.grid(row=0, column=0)

        # buttons
        self.analyse_button = ttk.Button(self.left_frame, text="auswählen")
        self.analyse_button.grid(row=1, column=0)

        self.analyse_button = ttk.Button(self.left_frame, text="importieren")
        self.analyse_button.grid(row=2, column=0)

        self.rate_button = ttk.Button(self.left_frame, text="bewerten")
        self.rate_button.grid(row=3, column=0)

        self.rate_button1 = ttk.Button(self.left_frame, text="analysieren")
        self.rate_button1.grid(row=4, column=0)

        # treeview widget for spreadsheets
        self.sp_preview = ttk.Treeview(self.right_frame)
        self.sp_preview.grid(row=0, column=0)

If I change the code as below

self.left_frame.grid(row=0, column=1)
self.right_frame.grid(row=0, column=2)

it works as intended. But that feels wrong, because I'm not using column 0.

CodePudding user response:

It is most likely because you did not specify the parent of left_frame and right_frame, so they will be children of root window, not instance of MainFrame.

If instance of MainFrame or other frame is also a child of root window and put in row 0 and column 0 as well, it may overlap/cover left_frame.

Set the parent of the two frames to self may fix the issue:

class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)

        # containers
        self.left_frame = ttk.Frame(self) # contains all the buttons and icons on the left
        self.left_frame.grid(row=0, column=0)

        self.right_frame = ttk.Frame(self) # contains the spreadsheet preview
        self.right_frame.grid(row=0, column=1)

        ...
  • Related