Home > Enterprise >  tkinter classes problem: __init__() got an unexpected keyword argument 'master'
tkinter classes problem: __init__() got an unexpected keyword argument 'master'

Time:10-12

I am using customtkinter which is the exact same as tkinter but prettier.

how can I set the master of a tkinter frame class when the class inherits from the frame class in tkinter ??

my problem is because i want frame_right to be the master of wave_prop_inputs frame; which basically means, a frame inside a frame.

I tried using master=frame_right when making an object of the wave_prop_inputs class but that gave me the error init() got an unexpected keyword argument 'master'

here is my main file:

import customtkinter as ctk
import main
import a_wave_prop.a_view as wave_prop

from constants import TITLE, WIDTH, HEIGHT

#Window Visuals
ctk.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
ctk.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

#Main Window
class App(ctk.CTk):

    def __init__(self):
        super().__init__()

        self.title(TITLE)
        self.geometry(f"{WIDTH}x{HEIGHT}")
        self.protocol("WM_DELETE_WINDOW", self.on_closing)  # call .on_closing() when app gets closed
    
        self.grid_columnconfigure(1, weight=1)
        self.grid_rowconfigure(0, weight=1)

        #Create Two Main Frames
        #Left Frame
        self.frame_left = main.main_left()
        self.frame_left.configure(width=180, corner_radius=0)
        self.frame_left.grid_rowconfigure(0, minsize=10)   # empty row with minsize as spacing
        self.frame_left.grid_rowconfigure(8, weight=1)  # empty row as spacing
        self.frame_left.grid(row=0, column=0, sticky="nswe")
    
        #Right Frame
        self.frame_right = main.main_right()
        self.frame_right.rowconfigure((1), weight=1)
        self.frame_right.columnconfigure((0, 1, 2, 3, 4, 5, 6, 7), weight=1)
        self.frame_right.grid(row=0, column=1, sticky="nswe", padx=10, pady=10)
    
        #Right Frame Children
        #wave_prop Inputs Frame
        self.wave_prop_inputs = wave_prop.inputs(master=self.frame_right)
        self.wave_prop_inputs.grid(row=0, column=0, columnspan=8, rowspan=1, pady=10, padx=10, sticky="nsew")
    
    def on_closing(self, event=0):
        self.destroy()
    
if __name__ == "__main__":
app = App()
app.mainloop()

and here is my 2nd file:

import customtkinter as ctk

class inputs(ctk.CTkFrame):    
    def __init__(self):
        super().__init__() 
    
        self.label_1 = ctk.CTkLabel(self, text="Incident Wave Inputs",
                                text_font=("Roboto Medium", -24,))
        self.label_1.grid(row=1, column=0, columnspan=2, sticky='W', pady=10, padx=10)
    
        self.label_2 = ctk.CTkLabel(self, text="Wave Height (H) = ",
                                text_font=("Roboto Medium", -16))
        self.label_2.grid(row=2, column=0, sticky='W', pady=10, padx=10)
    
        self.label_3 = ctk.CTkLabel(self, text="Water Depth (d) = ",
                                text_font=("Roboto Medium", -16))
        self.label_3.grid(row=3, column=0, sticky='W', pady=10, padx=10)
    
        self.label_4 = ctk.CTkLabel(self, text="Wave Period (T) = ",
                                text_font=("Roboto Medium", -16))
        self.label_4.grid(row=4, column=0, sticky='W', pady=10, padx=10)
    
        self.label_5 = ctk.CTkLabel(self, text="Wave Angle (\u03B1) = ",
                                text_font=("Roboto Medium", -16))
        self.label_5.grid(row=2, column=2, sticky='E', pady=10, padx=10)
    
        self.label_6 = ctk.CTkLabel(self, text="Bed Slope (s) = ",
                                text_font=("Roboto Medium", -16))
        self.label_6.grid(row=3, column=2, sticky='E', pady=10, padx=10)
    
        self.label_7 = ctk.CTkLabel(self, text="Depth of Interest (z) = ",
                                text_font=("Roboto Medium", -16))
        self.label_7.grid(row=4, column=2, sticky='E', pady=10, padx=10)
    
        self.entry2 = ctk.CTkEntry(self, placeholder_text="meter", width=120, 
                               height=25, border_width=2, corner_radius=10)
        self.entry2.grid(row=2, column=1, sticky='W', pady=10, padx=10)
    
        self.entry3 = ctk.CTkEntry(self, placeholder_text="meter", width=120,
                               height=25, border_width=2, corner_radius=10)
        self.entry3.grid(row=3, column=1, sticky='W', pady=10, padx=10)
    
        self.entry4 = ctk.CTkEntry(self, placeholder_text="second", width=120,
                               height=25, border_width=2, corner_radius=10)
        self.entry4.grid(row=4, column=1, sticky='W', pady=10, padx=10)
    
        self.entry5 = ctk.CTkEntry(self, placeholder_text="degree", width=120,
                               height=25, border_width=2, corner_radius=10)
        self.entry5.grid(row=2, column=3, sticky='W', pady=10, padx=10)
    
        self.entry6 = ctk.CTkEntry(self, placeholder_text="meter/meter", width=120,
                               height=25, border_width=2, corner_radius=10)
        self.entry6.grid(row=3, column=3, sticky='W', pady=10, padx=10)
    
        self.entry7 = ctk.CTkEntry(self, placeholder_text="- meter", width=120,
                               height=25, border_width=2, corner_radius=10)
        self.entry7.grid(row=4, column=3, sticky='W', pady=10, padx=10)

        self.button_6 = ctk.CTkButton(self, text="Calculate", command=self.button_event)
        self.button_6.grid(row=5, column=0, pady=10, padx=10, sticky="W")
    
    def button_event(self):
        print("Button pressed")

CodePudding user response:

At this line you try to parse the keyword argument master:

wave_prop.inputs(master=self.frame_right)

To this class that does not expect any sort of arguments:

class inputs(ctk.CTkFrame):    
    def __init__(self):
        super().__init__() 

To enable this class to accept an argument with a default value you need to change it like this:

class inputs(ctk.CTkFrame):    
    def __init__(self, master=None):

Last but not least you need to parse that value to your super class in order to make the super class use it.

        super().__init__(master) 

You should take a look at a basic tutorial about OOP to benefit by this concept more. In addition I suggest you to use the PEP-8 (Python Style Guide) see naming convention for classes for example. Your class looks like a method for a regular python developer.

  • Related