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.
There are few issues in your code:
you should not create another instance of Entries
inside Buttons.calculate()
. Suggest to pass the instance to Buttons
when it is created;
the function self.calculate()
is not called actually in the expression command=lambda: self.calculate
. Use command=self.calculate
instead. Same issue for command=lambda: self.clear
;
if the three inputs are numbers, you need to convert them to numbers before doing the calculation.
Updated code:
import tkinter as tk
from tkinter import ttk
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, self.entries) ### pass self.entries to class Buttons
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()
class Buttons(ttk.Frame):
def __init__(self, parent, entries): ### added argument entries
super().__init__(parent)
self.place(x = 200, y = 0)
self.entries = entries ### save the passed entries
self.buttonGen()
self.buttonPlacer()
def buttonGen(self):
self.clear = ttk.Button(self, text = "Clear", command = self.clear) ### remove lambda
self.calculate = ttk.Button(self, text = "Calculate", command = self.calculate) ### remove lambda
def buttonPlacer(self):
self.clear.pack()
self.calculate.pack()
def clear(self):
pass
def calculate(self):
try:
### convert inputs to numbers before calculation
print(float(self.entries.firstTerm.get()) + float(self.entries.commonDifference.get()) + float(self.entries.numberOfTerms.get()))
except Exception as ex:
### better show the execption
print(ex)