Python Forum
Button to stop while loop from another script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button to stop while loop from another script
#1
Hi, i have 2 script where one is the Gui i.e script A and another is an api call functions i.e script B.

As i import script B into script A and perform Api call using my Gui button. In my script B , the Api call function have a while loop which will be checking request from the server till time out.

I would like to use a button in script A gui to terminate the while loop in script B. I have tried using global variable from script A and import to script B but it results in error.

Is there a way to terminate the while loop or restart the whole script with a button?

For ex.
Script A :
Import script B 

Some_functions() 
Script B

Some_functions()
...
....
While (time<maxtime)
.... ; request from server
....
Reply
#2
Change some variable in script B from True to False You should consider a class structure for this.
while (time<maxtime):
   if not script_b.variable:
        break 
Reply
#3
(Sep-24-2023, 08:06 PM)woooee Wrote: Change some variable in script B from True to False You should consider a class structure for this.
while (time<maxtime):
   if not script_b.variable:
        break 

Hi thanks for the reply. Is it possible to change the script b variable using script A?
Reply
#4
What you are asking cannot be done. If your GUI calls a function, it will not process any button press events until the function is done running and the mainloop() function is allowed to run. You can push your stop button as many times as you want and nothing will happen if mainloop() is blocked by some function. Doing what you want will require significant changes.

One approach you could try is running your B script in a separate thread or process. This lets the A script mainloop() function run while the B script while loop is trying to connect. You will need to write the B script to allow ending the loop, and provide a means for A script to signal B script that it is time to stop trying to connect. A global variable should work for this.

A second approach is to break up script B into multiple functions, each function taking a small amount of time to execute. You would then use tkinter.after() to implement a loop in script A, calling the functions in script B.

I cannot suggest which approach to use without knowing more about the code that is blocking mainloop().
Reply
#5
I appreciate the reply. Yes i am currently using thread on script A to call api so when i use the stop button in script A it does work. However it does not stop the while loop on the thread that was running. I tried to import global variable to script B but receive error as script A imports the function from B that contains the global variable.
Reply
#6
The global variable belongs in script B, not script A.

napper.py is a script that that contains a function I want to interrupt.
import time

wake_up = True

def take_a_nap():
    global wake_up
    wake_up = False
    print("I'm taking a nap.")
    while not wake_up:
        time.sleep(1)
        print("Zzzzzz")
    print("I'm awake")
test.py is a script that uses napper.
import threading
import time
import napper

t1 = threading.Thread(None, napper.take_a_nap)
t1.start()
time.sleep(5)
print("Time to wake up!")
napper.wake_up = True
t1.join()
print("Good morning")
Output:
I'm taking a nap. Zzzzzz Zzzzzz Zzzzzz Zzzzzz Time to wake up! Zzzzzz I'm awake Good morning
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Linux] Script in cron stops after first run in loop Winfried 2 932 Nov-16-2022, 07:58 PM
Last Post: Winfried
  How to break out of a for loop on button press? philipbergwerf 6 1,784 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  non-stop ping script kucingkembar 1 1,367 Aug-23-2022, 06:29 AM
Last Post: menator01
  get out of while loop and stop repeat Frankduc 11 2,997 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Script stop work after 3 actioins - PLEASE WHERE IS THE PROBLEM? rondon442 0 1,565 Sep-27-2021, 05:40 PM
Last Post: rondon442
  how to loop a script? ZYSIA 1 2,015 Jul-22-2021, 06:46 AM
Last Post: Gribouillis
  Stop/continue While loop block Moris526 68 25,609 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  Using a button to kill and restart a script duckredbeard 3 3,336 Sep-01-2020, 12:53 AM
Last Post: duckredbeard
  How can I stop the script when I run the game on my own computer? StatTark 2 2,239 Jun-19-2020, 11:20 AM
Last Post: StatTark
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,906 May-10-2020, 03:22 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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