Python Forum
Running function from parent module which has a loop in it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running function from parent module which has a loop in it.
#1
Hi all,

I have a parent module that imports various other modules and runs a “While True” loop within it.

I’m essentially building a spotify radio clock with the Parent module and a UI module that gets commands depending on input on the interface.
- The parent calls a UI.window.update() to refresh the TKinter.

Currently I am using a messy setup where the parent module checks whether a variable is “None” otherwise executes a command based on whatever string that “None” is replaced with.

A more optimal solution would be for the UI class to be able to run a function in the parent. Lets say for example “play music”.

What is the best way for two modules to work but with one slave child module to have the ability to run functions in the master function?

If I use a double import then things get messy with circular referencing etc...
Reply
#2
ta2909i Wrote:What is the best way for two modules to work but with one slave child module to have the ability to run functions in the master function?
I think a simple way is to use classes and pass class instances as object parameters, for example
# parent.py
import uipart

class MusicPlayer:
    def play_music(self):
        ...

if __name__ == '__main__':
    player = MusicPlayer()
    ui = uipart.Ui(player)
    while True:
        ...
# uipart.py

class Ui:
    def __init__(self, player):
        self.player = player

    def spam(self):
        # call the parent's callback as a player instance's method
        self.player.play_music()

...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  the order of running code in a decorator function akbarza 2 479 Nov-10-2023, 08:09 AM
Last Post: akbarza
  help RuntimeError: no running event loop marpaslight 5 3,618 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  bleak library RuntimeError: This event loop is already running alice93 3 4,027 Sep-30-2021, 08:06 AM
Last Post: alice93
  loop running indefinitely shantanu97 6 2,510 Sep-29-2021, 08:03 PM
Last Post: deanhystad
  Running A Loop Until You See A Particular Result knight2000 6 31,606 Sep-04-2021, 08:55 AM
Last Post: knight2000
Question Stopping a parent function from a nested function? wallgraffiti 1 3,620 May-02-2021, 12:21 PM
Last Post: Gribouillis
  Running loop at specific frequency mdsousa 3 5,847 Apr-21-2021, 11:22 AM
Last Post: jefsummers
  RuntimeError: This event loop is already running newbie2019 2 6,906 Sep-30-2020, 06:59 PM
Last Post: forest44
  Class function is not running at all even though it is clearly run in the code SheeppOSU 2 2,034 Sep-26-2020, 10:54 PM
Last Post: SheeppOSU
  Exit Function - loop Tetsuo30 2 2,025 Sep-17-2020, 09:58 AM
Last Post: Tetsuo30

Forum Jump:

User Panel Messages

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