pythonooptkintercustomtkinter

Creating a Custom class of Label and passing arguments from the main window with Custom TKinter and Python


I'm quite new to GUI and OOP, I really tried to figure out but I'm not able to find the solution.

I started a GUI project which started to be very messy very quickly. So I started to recreate from scratch using Oriented Object (Classes etc...)

To simplify, I would like to create a custom label class which will have base information as default (font / background color ... from the main App)

But it doesn't display properly, what would be the right method to this please ?

Here is my code :

class App(CTk):
    def __init__(self, title, size):
        super().__init__()
    
        # MAIN SETUP
        self.title(title)
        self.size = f'{size[0]}x{size[1]}'
        self.minsize(480, 360)
        self.iconbitmap("logo.ico")
        self._set_appearance_mode('dark')
        set_default_color_theme('dark-blue')
        self.App_color = self._fg_color[1]

        # WIDGETS
        Home_Page(self)


        # RUN
        self.mainloop()

class Home_Page(CTkFrame):
    def __init__(self, parent):
        super().__init__(parent)
        self._border_color = 'purple'
        self._border_width = 2
        self._bg_color = parent.App_color
        self._fg_color = parent.App_color
        self.App_color = parent.App_color

        # WIDGETS
        My_CTk_Label(self, self.master, text='test label')

        # DISPLAY
        self.pack(expand=YES, fill=BOTH)

class My_CTk_Label(CTkLabel):
    def __init__(self, parent, app, text):
        super().__init__(parent)
        self._bg_color = app.App_color
        self._text = text
        self._font = ("Arial",25)
        self._text_color = 'white'
        self._fg_color ='red'

        self.pack()

App('MEAL PLANNER',(720,720))

Basically I have my main window App() which is parent of a frame 'home page' which is parent of my custom label. I don't succeed to display properly my label in the frame while passing some information (here the bg color, called 'App_color' from the App to the label Class.

I can see it is there, but without anything (just cut the border of my frame) :

enter image description here


Solution

  • You need to set those options in super().__init__(...) instead:

    from customtkinter import *
    
    class App(CTk):
        def __init__(self, title, size):
            super().__init__()
    
            # MAIN SETUP
            self.title(title)
            self.size = f'{size[0]}x{size[1]}'
            self.minsize(480, 360)
            self.iconbitmap("logo.ico")
            self._set_appearance_mode('dark')
            set_default_color_theme('dark-blue')
            self.App_color = self._fg_color[1]
    
            # WIDGETS
            self.home_page = Home_Page(self)
            self.home_page.pack(expand=YES, fill=BOTH)
    
            # RUN
            #self.mainloop() # not recommended to call it inside __init__()
    
    class Home_Page(CTkFrame):
        def __init__(self, parent):
            super().__init__(
                parent,
                border_color = 'purple',
                border_width = 2,
                bg_color = parent.App_color,
                fg_color = parent.App_color
            )
            self.App_color = parent.App_color
    
            # WIDGETS
            self.label = My_CTk_Label(self, self.master, text='test label')
            self.label.pack()
    
            # DISPLAY
            #self.pack(expand=YES, fill=BOTH) # not recommended to call by widget itself
    
    class My_CTk_Label(CTkLabel):
        def __init__(self, parent, app, text):
            super().__init__(
                parent,
                bg_color = app.App_color,
                text = text,
                font = ("Arial",25),
                text_color = 'white',
                fg_color ='red'
            )
    
            #self.pack() # not recommended to call by widget itself
    
    app = App('MEAL PLANNER',(720,720))
    app.mainloop()