How to loop and call a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gogoalshop
    New Member
    • Jul 2019
    • 1

    How to loop and call a function?

    Hi all,


    I want to loop and call a function every 1000 milliseconds with wx.CallLater. I implemented this (see below), but it does not factor in the delay - it seems to execute automatically. How can I get this to wait 1000 milliseconds between function calls?

    This function is within a class. In my main module, I instantiate an object of this class and call the task_loop function. I intend to keep the loop going until will_break is set to True, and task_loop returns the work_session_ag enda in the main module.

    Thanks ahead!!!
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    how does your code look like so far?

    Comment

    • Vanisha
      New Member
      • Jan 2023
      • 26

      #3
      To loop and call a function, you can use a loop construct, such as a for loop, while loop, or do-while loop, to iterate over a set of values and call a function within the loop body. Here is an example using a for loop:
      // Define the function
      function myFunction($val ue) {
      // Do something with the value
      echo "The value is: $value <br>";
      }

      // Loop over a set of values and call the function
      for ($i = 0; $i < 5; $i++) {
      myFunction($i);
      }
      If you want to learn PHP or any other technologies, join CETPA Infotech.

      Comment

      • vipulguptaseo
        New Member
        • Feb 2023
        • 22

        #4
        To call a function repeatedly with a delay of 1000 milliseconds using wx.CallLater, you need to make sure that the function is called again after the delay has passed. Here's an example of how you can achieve this within a class:

        import wx

        class MyClass:
        def __init__(self):
        self.will_break = False

        def task_loop(self) :
        # Perform some task
        self.do_somethi ng()

        if not self.will_break :
        # Call the task_loop function again after the delay
        wx.CallLater(10 00, self.task_loop)

        def do_something(se lf):
        # Your code to perform the task goes here
        print("Doing something...")

        # Instantiate the class
        my_object = MyClass()

        # Start the task loop
        my_object.task_ loop()

        # Start the event loop in wxPython
        app = wx.App()
        app.MainLoop()

        In this example, the task_loop function is initially called manually to start the loop. Inside the function, the task is performed (replace self.do_somethi ng() with your actual task implementation) . Then, if the will_break flag is not set to True, the task_loop function is scheduled to be called again after the delay of 1000 milliseconds using wx.CallLater(10 00, self.task_loop) . This ensures that there is a delay between function calls.

        The event loop (app.MainLoop() ) in wxPython is started to allow the wx.CallLater to work correctly. It handles the internal processing of events and timers in the GUI framework.

        Make sure to set self.will_break = True when you want to stop the loop and exit the program.

        Note: Ensure that you have the necessary dependencies installed, including wxPython, to run this code successfully.

        Comment

        Working...