Is passing 'self' to another class possible
Posted: Sat Apr 17, 2021 8:30 pm
I have a class and I want to pass the class as an argument to a function in another class. I'm thinking I can't do this because I have not instantiated an object of either class.
In the above class I want to call a function in the following class:
I'm trying to separate the programming logic from the UI. As I write this post, I'm thinking I need to do this from yet another class or in the main.py.
Code: Select all
# constants
PLAYER1 = 0
PLAYER2 = 1
RACKS = 2
BALLSREMAINING = 3
ADD = 1
SUBTRACT = 2
INITALIZE = 0
LOW = 0
HIGH = 1
class Score:
def __init__(self, display, buttonLeds, numDisplays, numDigits):
"""
display is the object that will display the calculations
Constructor:
"""
self.numPlayers = 2
self.numButtons = self.numPlayers * 2
self.playerScore = []
self.playerUp = 0
self.numRacks = 0
self.ballsOnTable = 15
self.ballsOnFoul = 0
self.buttonLeds = buttonLeds
self.ledDisplay = [numDisplays]
self.ledDigit = [numDigits]
self.decimalPoint = False
self.quickStart = False
for i in range(self.numPlayers):
self.playerScore.append(0)
def updateDisplay(self, display):
if isinstance(mc2Display, mc2Max7219.SevenSegment):
LedDisplay.updateDisplay(self, display)
else:
LcdDisplay.updateDisplay(self, display)
Code: Select all
class LedDisplay:
def __init__(self):
"""
This handles the display of the score. We keep them separate so that, if we move to LCD from LED display,
the Display and Score classes can potentially be the same or just swapped
"""
self.quickStart = False
self.displayText = '000000 00 00' #default display pattern
def updateDisplay(self, display, score):
#display is a mc2Max7219 object
numDisplays = 4
digits = []
for i in range(numDisplays):
digits.append(' ')
digits[0] = "{:03d}".format(score.playerScore[0])
digits[1] = "{:03d}".format(score.playerScore[1])
if self.ballsOnTable < 0:
digits[2] = "{:03d}".format(score.ballsOnTable)
else:
digits[2] = " {:02d}".format(score.ballsOnTable)
if self.numRacks < 10:
digits[3] = " {:01d}".format(score.numRacks)
else:
digits[3] = " {:02d}".format(score.numRacks)
display.displayText(digits[0] + digits[1] + digits[2] + digits[3])