Basic python programming. where do I put my code? =/

Hi there. I have the library working, but I’m currently writing my code in the def run(self): function and I’m not sure this is right? I basically want to change the text and write to the screen when I choose. My program does this but I’m not sure I’m doing it right.

Could someone cast an eye over my code and see if it looks correct, or perhaps you could send me an example program to show how it should be done?

The program updates the screen upon a button press reading on an mcp23017,

many thanks in advance

#!/usr/bin/env python
from samplebase import SampleBase
from rgbmatrix import graphics
from gpiozero import Button
from gpiozero import LED
from time import sleep
from rgbmatrix import RGBMatrix, RGBMatrixOptions
import time
import pygame
import wiringpi
import sys
import threading

pin = 65
toggleVar = 1
runScreenBool = True 


pin_base = 65
i2c_addr = 0x20
i2c_addr_2 = 0x24
#pins = [65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80]

wiringpi.wiringPiSetup()
wiringpi.mcp23017Setup(pin_base,i2c_addr)
wiringpi.mcp23017Setup(pin_base+16,i2c_addr_2)

soundNumber = 100
myVar =1
ledPin = LED(24)
teenseyPin19 = Button(19, pull_up = False)
teenseyPin25 = Button(25, pull_up = False)

pygame.mixer.pre_init(44100, -16,1,512)
pygame.init()
sound19 = pygame.mixer.Sound('/home/pi/Desktop/19.wav')


def func25():   
    sys.exit()

    
for x in range(65, 80):
    print("x")
    wiringpi.pinMode(x,1) #turn pin output
    wiringpi.digitalWrite(x,1)
    
for x in range(80, 96):
    print("x")
    wiringpi.pinMode(x,0) #turn pin input
    button_state = wiringpi.digitalRead(x)
    print(" button_state = ")
    print(button_state)


def buttonReadFunc():
    for x in range(82, 94):
        global myVar
        global runScreenBool 
        if wiringpi.digitalRead(x) == 1:
            sound19.play()
            #func19()
            print ("button read func = ")
           
            myVar = not myVar
            runScreenBool = True  
           # pulsing_colors.run() # this does seem to run the function


class PulsingColors(SampleBase):
    def __init__(self, *args, **kwargs):
        super(PulsingColors, self).__init__(*args, **kwargs)

    def run(self):
        self.offscreen_canvas = self.matrix.CreateFrameCanvas()
        continuum = 0
        self.offscreen_canvas = self.matrix.CreateFrameCanvas()
        offscreen_canvas = self.matrix.CreateFrameCanvas()
        pos = offscreen_canvas.width
        global myVar
        global runScreenBool


        while True:
            
            global myVar    
            buttonReadFunc()

            if runScreenBool == True:
                runScreenBool = False 

                if myVar:
                    print ("myVar in run func = ", myVar)
                    green = 0
                    blue = 50 
                    red = 0
                    self.offscreen_canvas.Fill(red, green, blue)
                    
                    font = graphics.Font()
                    font.LoadFont("/home/pi/rpi-rgb-led-matrix/bindings/fonts/5x7.bdf")
                    textColor = graphics.Color(255, 255, 0)
                    stock2 = graphics.Color(139, 0, 0)
                    stock3 = graphics.Color(64, 0, 255)
                    stock4 = graphics.Color(0, 255, 255)
        
                    my_text =  "xxx"
                    my_text1 = "OOO"
                    my_text2 = "ppp"
                    my_text3 = "ddd"
        
                    offscreen_canvas.Clear()
                    len1 = graphics.DrawText(offscreen_canvas, font, pos, 7, textColor, my_text)
                    len2 = graphics.DrawText(offscreen_canvas, font, pos, 15, stock2, my_text1)
                    len3 = graphics.DrawText(offscreen_canvas, font, pos, 23, stock3, my_text2)
                    len4 = graphics.DrawText(offscreen_canvas, font, pos, 31, stock4, my_text3)
                    pos = 1
                    offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
                    
                else: 
                    print ("myVar in run func = ", myVar)
                    red = 50
                    blue = 0 
                    green = 0
                    self.offscreen_canvas.Fill(red, green, blue)
                    self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas) 
                sleep(1)
                       
        
def func19():
    sound19.play()
    global myVar 
    print ("myVar in func 19 = ", myVar)
     
        
teenseyPin19.when_pressed = func19
teenseyPin25.when_pressed = func25
        
if __name__ == "__main__":
    pulsing_colors = PulsingColors()
    
    if (not pulsing_colors.process()):
        pulsing_colors.print_help()