0

I'm working on a webscraping code, and the process is quite long. Thus, I would like to start and stop it in a fancy way, with a button using tkinter. But I couldn't find any solution on the web.

  • The .after() function doesn't seems to be a good option as I don't want to restart my loop each time. My loop is already working based on urls readed from another document and parsing for each.
  • I tried the thread option with .join(). But couldn't manage to solve my problem with it. But maybe I don't write my code correctly.

My problem can be formulate as follow:

Start → Read urls doc and launch parsing loop.

Stop → Stop parsing and save content.

For instance I would like something like the following code to work:

from tkinter import *

flag=False

def test_loop():
    while flag==True:
        print("True")
    while flag==False:
        print("False")

def start():
    global flag
    flag=True
    test_loop()

def stop():
    global flag
    flag=False
    test_loop()

root = Tk()
root.title("Test")
root.geometry("500x500")

app = Frame(root)
app.grid()

start = Button(app, text="Start", command=start)
stop = Button(app, text="Stop", command=stop)

start.grid()
stop.grid()

root.mainloop()
Cmisner
  • 1
  • 2
  • 1
    In a tkinter app, you'll probably need to use `after()` in some manner in order to avoid causing the GUI to "hang" — you can't interfere with the running of its `mainloop()`. Using `join()` isn't really an option because tkinter doesn't directly support mutlthreading. One way to work around that is to use my answer to the question [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete). – martineau Jan 07 '21 at 19:26
  • Thanks martineau. I will have a look a it. – Cmisner Jan 10 '21 at 14:21
  • Unfortunately, I couldn't manage to get it work with your method. It's quite complicated so maybe I didn't modify your code properly to adapt to my case. I will try to be more explicit. – Cmisner Jan 10 '21 at 16:05

3 Answers3

0

Just do this:

while links and not stopped:
     # scraping here

You can control the stopped bool flag with your button.

AlanWik
  • 326
  • 1
  • 10
  • Thanks AlanWik, I tried, but it doesn't work. While the loop is working, a command controling the stopped flag have no impact on it. – Cmisner Jan 10 '21 at 14:17
0

You need to import threading library as python takes only one command at a time so threading will create multiple threads to handle multiple tasks simultaneously. This is the updated code:

from tkinter import *
import threading

flag=False

def test_loop():
    while flag==True:
        print("True")
    if flag==False:
        print("False")

def start():
    global flag
    flag=True
    thread.start() #thread start

def stop():
    global flag
    flag=False
    thread.join() #thread stop

#this is the point where you will create a thread
thread=threading.Thread(target=test_loop)

root = Tk()
root.title("Test")
root.geometry("500x500")
app = Frame(root)
app.grid()

start = Button(app, text="Start", command=start)
stop = Button(app, text="Stop", command=stop)
start.grid()
stop.grid()

root.mainloop()
0

Ok I managed to solve the problem, using a flag system indeed.

from tkinter import *
import threading


def st1():
    global go1
    global go2
    go1=True
    while go1==True:
        go2=False
        print('Start')

def st2():
    global go1
    global go2
    go2=True
    while go2==True:
        go1=False
        print('Stop')


def thread1():
    threading.Thread(target=st1).start()

def thread2():
    threading.Thread(target=st2).start()


root = Tk()
root.title("Test")
root.geometry("500x500")

app = Frame(root)
app.grid()

start = Button(app, text="Start", command=thread1)
stop = Button(app, text="Stop", command=thread2)

start.grid()
stop.grid()

root.mainloop()

Thanks for your help it was helpfull.

Cmisner
  • 1
  • 2