May-09-2019, 11:14 PM
(This post was last modified: May-09-2019, 11:15 PM by michalmonday.)
Hello, you could pass the "CreateTab" object into that inner class. Modified content of "calculator/__init__.py":
Edit: now that I look at it, you're passing
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.