IgnisNoctum
IgnisNoctum

Reputation: 43

Tkinter: Cant get the grid geometry manager to work with frames

I'm new to Tkinter and trying to create a program where I can display a few frames like this: A rough sketch of what I want where all the different colours are different frames.

my current code looks like this:

import tkinter as tk

root = tk.Tk()
root.geometry('700x400')


class Creation(tk.Frame):

    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)
        self.master = master

        self.master.title('Creation')
        self.frame_test()

    def frame_test(self):
        info_frame = tk.Frame(bg='Green')
        file_frame = tk.Frame(bg='Grey')
        data_frame = tk.Frame(bg='Gold')
        button_frame = tk.Frame(bg='Red')

        self.rowconfigure(0, weight=5)
        for i in range(1, 13):
            self.rowconfigure(i, weight=1)
        for j in range(6):
            self.columnconfigure(j, weight=1)

        info_frame.grid(column=0, columnspan=6, row=0)
        file_frame.grid(column=0, columnspan=6, row=1)
        data_frame.grid(column=0, columnspan=6, rowspan=10, row=2)
        button_frame.grid(column=0, columnspan=6, row=12)



program = Creation(root)
program.grid()
program.mainloop()

But every time I run the code, all I get is an empty tkinter window. Can anyone explain what is wrong with this code?

Upvotes: 0

Views: 45

Answers (1)

Thingamabobs
Thingamabobs

Reputation: 8037

First you need to parse the positional argument master=self to get it on your frame, otherwise it will be on the root_window. Then you need to give the optional arguments width and height how you like, otherwise the fell to 0 width and 0 height, which is nothing as long as they dosent contain something.

import tkinter as tk

root = tk.Tk()
root.geometry('700x400')


class Creation(tk.Frame):

    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)
        self.master = master

        self.master.title('Creation')
        self.frame_test()

    def frame_test(self):
        info_frame = tk.Frame(self,bg='Green',width=100,height=100)
        file_frame = tk.Frame(self,bg='Grey')
        data_frame = tk.Frame(self,bg='Gold')
        button_frame = tk.Frame(self,bg='Red')

        self.rowconfigure(0, weight=5)
        for i in range(1, 13):
            self.rowconfigure(i, weight=1)
        for j in range(6):
            self.columnconfigure(j, weight=1)

        info_frame.grid(column=0, columnspan=6, row=0)
        file_frame.grid(column=0, columnspan=6, row=1)
        data_frame.grid(column=0, columnspan=6, rowspan=10, row=2)
        button_frame.grid(column=0, columnspan=6, row=12)



program = Creation(root)
program.grid()
program.mainloop()

Upvotes: 1

Related Questions