I'm learning OOP and am making a calculator. I have classes for the main program (the parent program), buttons, and entries.
Main program:
class Program(tk.Tk):
def __init__(self, title, size):
super().__init__()
self.title(title)
self.geometry(f"{size[0]}x{size[1]}")
# Program elements split into widget type
self.entries = Entries(self)
self.output = Output(self)
self.buttons = Buttons(self)
Entries:
class Entries(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.place(x = 0, y = 0)
# Calling widget generator and placer functions
self.entryGen()
self.entryPlacer()
def entryGen(self): # Create entry widgets
self.firstTerm = ttk.Entry(self)
self.commonDifference = ttk.Entry(self)
self.numberOfTerms = ttk.Entry(self)
def entryPlacer(self): # Place entry widgets
self.firstTerm.pack()
self.commonDifference.pack()
self.numberOfTerms.pack()
Buttons:
class Buttons(ttk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.place(x = 0, y = 0)
self.buttonGen()
self.buttonPlacer()
def buttonGen(self):
self.clear = ttk.Button(self, text = "Clear", command = lambda: self.clear)
self.calculate = ttk.Button(self, text = "Calculate", command = lambda: self.calculate)
def buttonPlacer(self):
self.clear.pack()
self.calculate.pack()
def clear(self):
pass
def calculate(self):
entries = Entries()
try:
print(entries.firstTerm.get() + entries.commonDifference.get() + entries.numberOfTerms.get()) # Just some test code to see if it works, which it doesnt
except:
pass
I expect that when I create a reference to the Entries() class in my Buttons() class, I can access the variables I made in the Entries() class. This is not the case and I can't figure out why. I have looked at similar fixes and none seem to work.