Python Forum

Full Version: Call a varaible from class in the parent class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi experts.
I'm making a modular interface containing tabs with different tools.
These tools are for now :
  • A qr code display for my math teacher.
  • A real-time schedule display of my city's public transport company (the "CTCUB", aka "Bibus")
  • And a calculator.
And the upcoming tools are :
  • A text editor
  • A graphical editor and calculator for math
  • An easy formula editor and calculator for math and physics
  • A print manager for my two beautiful dot matrix printers.
  • Some other tools that I'll need in the future.

The principle, is that I have a main file, in which I import all my tools like an add-on. Then, by running a class, it automatically creates a tab and adds all the widgets in it.
And I'm having trouble making a calculator because I end up creating a class for each button (which seems to be the best way to do it). The problem, is that I end up having a class, into a class Doh . But I don't know how to access a class variable from a child class.

Here is the zip containing all the files so you can see.
http://clement2000.rf.gd/doc/tmp/utilitaireTravail.zip
I added a little comment (line 12, in the init of the calculator folder) to show exactly where I'm having trouble.
Sorry, the GUI is in french, but for international purposes, all the comments ans code is in English. If translation needed, please ask.

I hope you did understand what I want, it's really complex. Big Grin

Thanks in advance for your help,
Clément.
Hello, you could pass the "CreateTab" object into that inner class. Modified content of "calculator/__init__.py":

from tkinter import *
from tkinter import ttk

class CreateTab(): #Call this class to add the tool tab to a ttk notebook
    class CalcKey():#Calculator key class
        def __init__(self, widget, posX, posY, spanX, spanY, width, height, displayText, typeText, CreateTabObject): #args : position an d etc...
            self.CreateTabObject = CreateTabObject
            self.widget = Button(widget, width=width, height=height, text=displayText, command=self.write) #create the button
            self.widget.grid(row=posY, column=posX, rowspan=spanY, columnspan=spanX) #place thebutton
            self.typeText = typeText #text that the button is going to type in the screen when presed

        def write(self): #function to append the text that the key writes to the screen
            self.CreateTabObject.screenText += self.typeText #So my prblem is here, where I can't modify the "screenText" variable because it's in the parent class (CreateTab). HELP ! <<< 
            self.CreateTabObject.screen.config(text=self.CreateTabObject.screenText)

        def command(self, command):#use this method to change the write function of a key to an other action
            self.widget.config(command=command)

    def clear(self): #Clear the screen #NOT TESTED YET
        #import pdb; pdb.set_trace()
        self.screenText = ""
        self.screen.config(text=screenText)

    def calculate(self): #To make all the operations #NOT TESTED YET
        self.screenText = exec(self.screenText)
        self.screen.config(text=self.screenText)

    def __init__(self, notebook): #notebook is the ttk notebook to add the widget
        self.tab = ttk.Frame(notebook)
        notebook.add(self.tab, text="Calculatrice")

        #Text screen variable and widget
        self.screenText = ""
        self.screen = Label(self.tab, text=self.screenText, width=22, height=2, bg="gray50")
        self.screen.grid(row=0, column=0, columnspan=3)

        #All the number keys
        self.button0 = self.CalcKey(self.tab, 0, 5, 2, 1, 11, 3, "0", "0", self)
        self.button1 = self.CalcKey(self.tab, 0, 4, 1, 1, 4, 3, "1", "1", self)
        self.button2 = self.CalcKey(self.tab, 1, 4, 1, 1, 4, 3, "2", "2", self)
        self.button3 = self.CalcKey(self.tab, 2, 4, 1, 1, 4, 3, "3", "3", self)
        self.button4 = self.CalcKey(self.tab, 0, 3, 1, 1, 4, 3, "4", "4", self)
        self.button5 = self.CalcKey(self.tab, 1, 3, 1, 1, 4, 3, "5", "5", self)
        self.button6 = self.CalcKey(self.tab, 2, 3, 1, 1, 4, 3, "6", "6", self)
        self.button7 = self.CalcKey(self.tab, 0, 2, 1, 1, 4, 3, "7", "7", self)
        self.button8 = self.CalcKey(self.tab, 1, 2, 1, 1, 4, 3, "8", "8", self)
        self.button9 = self.CalcKey(self.tab, 2, 2, 1, 1, 4, 3, "9", "9", self)

        #All other text keys
        self.buttonPoint = self.CalcKey(self.tab, 2, 5, 1, 1, 4, 3, ".", ".", self)

        self.buttonOpenBracket = self.CalcKey(self.tab, 0, 5, 1, 1, 4, 3, "(", "(", self)
        self.buttonCloseBracket = self.CalcKey(self.tab, 1, 5, 1, 1, 4, 3, ")", ")", self)

        self.buttonPlus = self.CalcKey(self.tab, 3, 2, 1, 2, 4, 7, "+", "+", self)
        self.buttonMinus = self.CalcKey(self.tab, 3, 1, 1, 1, 4, 3, "-", "-", self)
        self.buttonTimes = self.CalcKey(self.tab, 1, 1, 1, 1, 4, 3, "×", "*", self)
        self.buttonDivide = self.CalcKey(self.tab, 2, 1, 1, 1, 4, 3, "÷", "/", self)

        #Other action keys
        self.buttonEqual = self.CalcKey(self.tab, 3, 4, 1, 2, 4, 7, "=", "", self)
        self.buttonEqual.command(self.calculate)

        #TO DO : Add a clear button and test all the functions.
Please note that I'm not experienced with GUI and OOP so can't tell if there's some obvious/proper way to improve the code, but that solution to the problem you posted seems to work.

Edit: now that I look at it, you're passing self.tab to the CalcKey anyway (where it's called widget) so you don't actually need another parameter that I added. You can replace self.tab with self, and then within CalcKeys' __init__ function you could replace widget with CreateTabObject and then use it like CreateTabObject.tab for widget and self.CreateTabObject = CreateTabObject to keep reference to it.