Home > OS >  How to access/use variables in methods defined in a Mixin class?
How to access/use variables in methods defined in a Mixin class?

Time:06-24

I'm trying to break a tkinter app with several classes into multiple .py files. I'm using a Mixin class to import methods into each class. However, I'm struggling to access variables.

main.py contains a class creating the main window with a button to open a top level window and a button to get a variable from the Mixin.

# main.py
import tkinter
from tkinter import Tk
import customtkinter
import sys
sys.path.insert(1, "path/Classes")
from Classes import topLevel

# Main Window
class App(customtkinter.CTk):

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

        # Top Level Button
        self.topLevel_button = customtkinter.CTkButton(master=self,
                                text="Open",
                                command=self.openTopLevel)
        self.topLevel_button.grid(row=0, column=0)

        # Get Variable
        self.get_button = customtkinter.CTkButton(master=self,
                                text="Get Variable",
                                command=self.getVariable)
        self.get_button.grid(row=1, column=0)


    def openTopLevel(self):
        window = topLevel.topLevel(self)

    def getVariable(self):
        print(var)          # Can't access var


if __name__ == "__main__":
    app = App()
    app.mainloop()

topLevel.py is a class creating the top level window. It contains a variable fileSep to be used by a method in the Mixin Class:

# topLevel.py
sys.path.insert(1, "path/Methods")
from Methods import _topLevel

class topLevel(customtkinter.CTkToplevel, _topLevel.Mixin):

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

        # Create Variable
        global fileSep
        fileSep = customtkinter.StringVar(value="Comma")
        print("Inside Class: "   fileSep.get())             # Works

        # Create Button
        self.loadButton = customtkinter.CTkButton(master=self,
                                text="Load",
                                command=self.loadFile)
        self.loadButton.grid(row=0, column = 0, sticky='nswe')
        
        # Attempt to access variable
        def access_method(self):
            print("Access Method: "   self.fileSep.get())


And _topLevel.py contains the mixin class:

# _topLevel.py
class Mixin:

    def loadFile(self):

        # Trying to access fileSep variable
        print("Inside Mixin: "   fileSep.get())         # Error
        topLevel().access_method()                      # Error
        
        # I'm trying to access this variable from a function in main.py
        var = "Hello World"                            

I get the following errors because the variables aren't accessible.

NameError: name 'var' is not defined
NameError: name 'fileSep' is not defined

I've tried making variables global as well as creating methods inside the class ( access_method() ) to print the variables as described here https://www.geeksforgeeks.org/python-using-variable-outside-and-inside-the-class-and-method/, but get the error:

TypeError: __init__() missing 1 required positional argument: 'parent'

How do I access variables that are defined in a class in the Mixin class? How would I access a variable created by the loadFile function in the Mixin Class for use in methods in the class App?

CodePudding user response:

Thanks to @acw1668 comment, I just needed to make fileSep an instance variable:

self.fileSep = customtkinter.StringVar(value="Comma")

And to access it in the mixin class:

print("Inside Mixin: "   self.fileSep.get())

Equally I could create an instance variable to assign variable var to. For example in the topLevel class I added:

self.a = ""

And then assigned var to this variable within the function:

var = "Hello World"
self.a = var
  • Related