A9M
A9M

Reputation: 55

Calling Functions within Classes from Tkinter Buttons

I am currently trying to use classes within my code which I have not used before. My goal is to When the power on button is pressed the system the system checks whether the power is on or not and if it is no it returns "Power On" and the same for Power Off and so on. I am currently struggling when the button is pressed I am unsure how to send a command to the function within control with the specified text e.g "PWR" Below is my code any help is much appreciated thank you

import tkinter as tk

window = tk.Tk()



class Power:

    def __init__(self, x):
        if x == 0:
            # Run def control
            # Change function in def control to Power
            print ("Power On")
        if x == 1:
            # Run def control
            # Change function in def control to Power
            print("Power Off")

        if x == 2:
            # Run def Control
            # Change Function to Inp
            # Add which input has been selected to the end of the return
            print ("Input 1")

        if x == 3:
            # Run def Control
            # Change Function to Inp
            # Add which input has been selected to the end of the return
            print ("Input 2")
        

    def control(self, function):
        if function[:3] == "PWR":
            if "on" in function.lower():
                return ("Power On")

            elif "off" in function.lower():
                return("Power Off")

            else:
                return ("Power Error")

        elif function[:3] == 'INP':
            inp = function[3:]
            return 'Input ' + inp

            




    
CtrlBtnsTxt = ["Power On", "Power Off", "Input 1", "Input 2"]
for i in range (4):
    CtrlBtns = tk.Button(window, width = 10, height = 5, text = CtrlBtnsTxt[i], command = lambda x = i: Power (x) )
    CtrlBtns.grid (row = i, column = 0)




window.mainloop()

Upvotes: 1

Views: 1067

Answers (1)

dspr
dspr

Reputation: 2423

It will be better for the class instance to persist across commands. This way you could simply memorize the current button states and use that to adjust the behavior during the next call :

import tkinter as tk

window = tk.Tk()

class Power:
    # ====> Memorizes states
    power = False
    curInput = 0
       
    # ====> Replace __init__ by a method you can call without creating a new instance
    def command(self, x):
        if x == 0:
            print (self.control("PWR on"))
        if x == 1:
            print (self.control("PWR off"))
        if x == 2:
            print (self.control("INP 1"))
        if x == 3:
            print (self.control("INP 2"))
      
    def control(self, function):
        if function[:3] == "PWR":

            # ====> Includes the stored state in the tests and stores the new state
            if "on" in function.lower() and self.power == False:
                self.power = True
                return "Power On"
            # ====> Idem
            elif "off" in function.lower() and self.power == True
                self.power = False
                return "Power Off"
            else:
                return ("Power Error")

        elif function[:3] == 'INP':
            inp = function[3:]

            # ====> Optionally you can do the same for the input
            if (inp == self.curInput): return 'Input already set'
            self.curInput = inp
            return 'Input ' + inp

   
CtrlBtnsTxt = ["Power On", "Power Off", "Input 1", "Input 2"]

# ====> Creates a unique instance of the class and uses it below
power = Power()

for i in range (4):
    CtrlBtns = tk.Button(window, width = 10, height = 5, text = CtrlBtnsTxt[i], command = lambda x = i: power.command(x) )
    CtrlBtns.grid (row = i, column = 0)

window.mainloop()

Upvotes: 1

Related Questions