1
\$\begingroup\$

I hope this is the right spot to ask this, if not tell me and I will delete the post.

I need my GUI/program to update whenever the active_bots variable is changed, to display the contents of this variable.

Is this the correct way to speak to the GUI instance? Pass the instance in the module-call and then set the active_bots-variable by calling the GUI-setter from the module. Or is this bad practice/is there a better way?

Cutting out a lot my code looks like this:

import curses
import modules.bots.quote_bot as quoteBot

class GUI:

    def __init__(self, stdscr):

        self.active_bots = [] #This variable is the one that needs to be watched
        
        #All kinds of preparation etc.

        self.run() # This would normally call run and start the program

    def run(self):

        while True:
            key = self.screen.getch()
            if key == ord('e'):
                #End program
                break
            elif (key == ord('q')):
                # Start bot
                quoteBot.start(self)
            else:
                #do nothing
                continue

        #Revert terminal settings and end
        self.screen.clear()
        curses.endwin()

    def active_bots_setter(self, operator, botname):
        if (operator == '-'):
            self.active_bots.remove(botname)
        elif (operator == '+'):
            self.active_bots.append(botname)
        else:
            raise ValueError
        self.init_user_window() #Refresh user-window 

#Start program.
#Use wrapper to restore original terminal settings even if exception raised
curses.wrapper(GUI)

quote_bot.py

def start(GUI_instance):
    #Check whether QuoteBot is already running
    thread_list = [] #List of alive threads
    for thread in threading.enumerate():
        thread_list.append(thread.getName())
    if "QuoteBot-Thread" in thread_list:
        logger.info("QuoteBot: Shutting down...")
        stop()
    else:
        logger.info("QuoteBot: Starting up...")
        GUI_instance.active_bots_setter('+', "QuoteBot")
        if (check_connection() == True):
            t = threading.Thread(target=tweet_quote)
            t.name = 'QuoteBot-Thread'
            t.daemon = True
            t.start()
        else:
            logger.error("Quotebot: Quote API Connection Test - Failed to connect! /nCancelling start up.")
\$\endgroup\$
  • 1
    \$\begingroup\$ "Is this the correct way to speak to the GUI instance?" Did you test it? Does it work? \$\endgroup\$ – Mast Oct 1 '20 at 14:23
  • 2
    \$\begingroup\$ Please take a look at our FAQ on asking questions. \$\endgroup\$ – Mast Oct 1 '20 at 14:25
  • \$\begingroup\$ @Mast Yes I tested it, it does indeed work. Passing self makes my module able to speak to the gui-instance. I was just wondering whether there exists a better, cleaner way. \$\endgroup\$ – Elekam Oct 5 '20 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.