I can't put the Tk widgets on the windows

62 views Asked by At

I am extending the tk widgets to make a gui app. The problem is that I can put them on the main window without an error, but they are not visible. I have called the super constructor, but it still not showing up. i even tried to use ai to debug it, but that din't work to, so I am asking for help

from tkinter import *
from functions import *

class InventoryLabel (Label):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.config(text=transform_data())


class FirstFrame(Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.configure(bg="red")
        self.grid(row=0, column=0, sticky="nsew")
        InventoryLabel(self)


class Window(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1250x950")
        self.title("Inventory")
        text_frame = FirstFrame(self)
        self.mainloop()
1

There are 1 answers

1
yosi On

The problem is in the window.

That happens because you're doing self.mainloop() in the class itself, so the other elements doesn't have time to load.

It's like that:

  1. creating the class, and then opens the window
  2. adds the elements - but window is already open

Do that:

from tkinter import *
from functions import *

class InventoryLabel (Label):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.config(text=transform_data())


class FirstFrame(Frame):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.configure(bg="red")
        self.grid(row=0, column=0, sticky="nsew")
        InventoryLabel(self)


class Window(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1250x950")
        self.title("Inventory")
        self.text_frame = FirstFrame(self)

window = Window()

window.mainloop()

And hopefully it would work.

If you want to add the elemnts AFTER the window is created, you can do window.after(1, function)