Python Forum
Call a varaible from class in the parent class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a varaible from class in the parent class
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 209 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 393 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 526 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 580 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 380 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,703 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 504 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  super() in class akbarza 1 386 Dec-19-2023, 12:55 PM
Last Post: menator01
  error occuring in definition a class akbarza 3 599 Nov-26-2023, 09:28 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020