I'm trying to write a tkinter program with start and stop buttons. For both the start and stop buttons, I am importing two different functions from different .py files. When I click on the start button, tkinter freezes and my cursor is constantly "processing" and does not let me click on the stop button.
My tkinter program is below:
import tkinter as tk
from start import hello
from stop import quitprog
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="Collecting Data Now...")
label.pack(side="top", fill="both", expand=True)
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
label = tk.Label(self, text="Analyzing Data Now...")
label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
b1 = tk.Button(buttonframe, text="start", command=hello)
b2 = tk.Button(buttonframe, text="stop", command=quitprog)
b1.pack(side="left")
b2.pack(side="left")
p1.show()
if __name__ == "__main__":
root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("400x400")
root.mainloop()
My start.py is below:
import time
def hello():
for i in range(20):
print("hello world")
time.sleep(1)
My stop.py:
import sys
def quitprog():
sys.exit()
My frozen window:
loading/processing cursor image
Please let me know how to fix this.
Edit: Instead of the start.py program, I'm actually invoking a live twitter stream and that doesn't make use of .sleep(). Instead, there is a continuous flow of twitter data, which causes the program to freeze. Any fixes for this?