Python Forum
Issue referencing new instance from other class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue referencing new instance from other class
#1
Hi all,

I have a large program that I distilled down hopefully to the bare issue that I am having. I have a GUI button that starts a thread. This thread would be started and stopped many times so it is necessary to create a new instance each time the button is pressed. My issue is that I am not sure how to pass the newly created instance back to my button callback. The callback only works the first time, but after the new instance is created the button cannot shut it off or even correctly reference the actual instance running the thread.

I'm sorry the code does not compile (or has something else wrong or missing), I just tried my best to cut away all unnecessary code to illustrate my current problem.

class MainPage(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)

    self.heating_btn = tk.Button(self, text="Thermostat OFF", command=self.heatCallback)
    self.heating_btn.grid(row=0, column=0)

    def heatCallback(self):
        if h.isRunning:        # error is here, new instance is created in HeatingController class (heating_instance) and I am using wrong instance here to check if it’s running
            print(“Heating already started”)
        else:
            h.startHeating()


class HeatingController(Thread):
    def __init__(self, main_instance):
        Thread.__init__(self)
        self._running = False
        self.main_instance = main_instance
   
    @property
    def isRunning(self):
        return self._running

    @isRunning.setter
    def isRunning(self, value):
        self._running = value
    

    def run(self):
        # some code
        # more code
        self._running = False

    def startHeating(self):
        if self._running:
            print("Heating already started")
        else:
            heating_instance = HeatingController(self.main_instance)
            heating_instance.isRunning = True
            heating_instance.start()



if __name__ == "__main__":
    main = MainPage(root)
    h = tempController.HeatingController(main)
    GPIO.add_event_detect(raspberryGPIO.startButton, GPIO.RISING, callback=run_instance.startCycleCallback, bouncetime=2000)
Reply
#2
Can the button callback be responsible for making the thread? If not it sounds like you need an intermediary, somebody who knows about the current thread and provides a function for doing whatever the button needs to do. This could be a class method of your thread creator class (Heating Control), or a completely separate class designed to manage the interaction between GUI and threads.
Reply
#3
you need a way to communicate with your threads, (called messaging semaphore)
I'd suggest reading the following: Little Book of Semaphores
with this you can set up a means of communicating with threads individually
Reply
#4
@deanhystad Yeah the intermediary sounds like the way I need to go because it is a bit more complicated than making the button responsible for making the thread, but I accidentally cut that code out trying to make it easy to read for y'all. Thanks for the advice as always.

@Larz60+ Cool I haven't even heard of semaphores yet. Thanks for introducing me to the idea! Once I read into it maybe that'll be the fix.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,470 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Child class inheritance issue eakanathan 3 1,299 Apr-21-2022, 12:03 PM
Last Post: deanhystad
  Dictionary Referencing nickdavis2017 1 1,566 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Access instance of a class Pavel_47 5 2,034 Nov-19-2021, 10:05 AM
Last Post: Gribouillis
  Referencing string names in df to outside variables illmattic 1 1,328 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Class Instance angus1964 4 2,407 Jun-22-2021, 08:50 AM
Last Post: angus1964
  Referencing a fixed cell Mark17 2 2,020 Dec-17-2020, 07:14 PM
Last Post: Mark17
  Can we access instance variable of parent class in child class using inheritance akdube 3 13,887 Nov-13-2020, 03:43 AM
Last Post: SalsaBeanDip
Sad need help in referencing a list n00bdev 2 1,810 Nov-01-2020, 12:06 PM
Last Post: buran
  Class variable / instance variable ifigazsi 9 4,220 Jul-28-2020, 11:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020