I have a program (say p1.py) which calls another python script (say p2.py) on click of a button. I would like to have a button which stops the execution of the p2.py but all the buttons freeze when it is running.
The only way to stop it is to use a keyboard interrupt in the console. I have read about the after() function but do I have to implement it in p1 or p2? Or is there any other way to do it without the after() function?
import tkinter
import os
window = tkinter.Tk()
window.title("Detecting")
def clicked():
os.system('python extract_frames.py')
bt = tkinter.Button(window,text="Start",command=clicked)
bt.pack()
stop = tkinter.Button(window,text="Stop",command="break") #also what command should I use for the interrupt?
stop.pack()
window.geometry('400x400')
window.mainloop()
You should use subprocess.Popen() instead of os.system():
import tkinter
import subprocess
proc = None
def clicked():
global proc
proc = subprocess.Popen(['python', 'extract_frames.py'])
def kill_task():
if proc and proc.poll() is None:
print('killing process ...')
proc.kill()
window = tkinter.Tk()
window.geometry('400x400')
window.title("Detecting")
bt = tkinter.Button(window, text="Start", command=clicked)
bt.pack()
stop = tkinter.Button(window, text="Stop", command=kill_task)
stop.pack()
window.mainloop()
Related
This is a simple gui.
I am taking url from the user in the Entry. When the button is pressed, url is saved in a file and another function is called to start another process through call (within subprocess).
while the process runs, I want to show the indeterminate progress bar (until the button is hit the bar needs to be hidden) and when the process is completed a showinfo message displays to destroy the gui.
Problem: The bar doesn't show up until the process is finished. After the showinfo dialog is displayed, only then it starts progressing. Means, the bar starts progressing the moment it should actually get destroyed.
What is wrong with my code?
import scrapy
import tkinter as tk
from tkinter import messagebox as tkms
from tkinter import ttk
import shlex
from subprocess import call
def get_url():
# get value from entry and write to a file
def scrape():
progress_bar = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=300, mode='indeterminate')
progress_bar.grid(row=3, column=2)
progress_bar.start(10)
command_line = shlex.split('scrapy runspider /media/mayank/Local/Coding/Lab/Scraping/Practices/img.py')
call(command_line)
mes = tkms.showinfo(title='progress', message='Scraping Done')
if mes == 'ok':
root.destroy()
root = tk.Tk()
root.title("Title")
entry1 = tk.Entry(root, width=90, textvariable=url)
entry1.grid(row=0, column=0, columnspan=3)
my_button = tk.Button(root, text="Process", command=lambda: [get_url(), scrape()])
my_button.grid(row=2, column=2)
root.mainloop()
----Updated Code ---
import scrapy
import tkinter as tk
from tkinter import messagebox as tkms
from tkinter import ttk
import shlex
from subprocess import call
def get_url():
# get value from entry and write to a file
scrapy = None
def watch():
global scrapy
if scrapy:
if scrapy.poll() != None:
# Update your progressbar to finished.
progress_bar.stop()
progress_bar.destroy()
# Maybe report scrapy.returncode?
print(f'scrapy return code =--######==== {scrapy.returncode}')
scrapy = None
else:
# indicate that process is running.
progress_bar.start(10)
print(f'scrapy return code =--######==== {scrapy.returncode}')
# Re-schedule `watch` to be called again after 0.1 s.
root.after(100, watch)
def scrape():
global scrapy
command_line = shlex.split('scrapy runspider ./img.py')
scrapy = Popen(command_line)
watch()
mes = tkms.showinfo(title='progress', message='Scraping Done')
if mes == 'ok':
root.destroy()
root = tk.Tk()
root.title("Title")
url = tk.StringVar(root)
entry1 = tk.Entry(root, width=90, textvariable=url)
entry1.grid(row=0, column=0, columnspan=3)
my_button = tk.Button(root, text="Process", command=lambda: [get_url(), scrape()])
my_button.grid(row=2, column=2)
progress_bar = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=300, mode='indeterminate')
progress_bar.grid(row=3, column=2)
root.mainloop()
Using subprocess.call interrupts the current process until the called process is finised.
So the GUI won't update until the call is finished.
Important takeaway: Never call subprocess.run, subprocess.call or one of the other convenience functions from the main thread of a tkinter program. Doing so will freeze the GUI. You should only create subprocess.Popen objects from the main thread.
What you should do instead is create a Popen object, while at the same time disabling the start button.
To track the progress, define a function that is periodically called with root.after(), say every 0.1 s.
In this function you could call the poll() method to check if the subprocess has finished.
Alternatively, you could set stdout=subprocess.PIPE and read the data from the subprocess from the stdout attribute of the Popen object.
The code below is a working (for me) example based on your updated question.
Note that I have replaced scrapy (which I don't have) with a relative long-running command on my UNIX machine.
Since you are running scrapy as a subprocess, you should not need import scrapy.
import tkinter as tk
from tkinter import messagebox as tkms
from tkinter import ttk
from subprocess import Popen
proc = None
def watch():
global proc
if proc:
if proc.poll() is not None:
# Update your progressbar to finished.
progress_bar.stop()
progress_bar.destroy()
# Maybe report proc.returncode?
print(f'proc return code =--######==== {proc.returncode}')
proc = None
mes = tkms.showinfo(title='progress', message='Scraping Done')
if mes == 'ok':
root.destroy()
else:
# indicate that process is running.
progress_bar.start(10)
# print(f'proc return code =--######==== {proc.returncode}')
# Re-schedule `watch` to be called again after 0.1 s.
root.after(100, watch)
def scrape():
global proc
command_line = ['netstat']
proc = Popen(command_line)
watch()
root = tk.Tk()
root.title("Title")
url = tk.StringVar(root)
entry1 = tk.Entry(root, width=90, textvariable=url)
entry1.grid(row=0, column=0, columnspan=3)
my_button = tk.Button(root, text="Process", command=lambda: [get_url(), scrape()])
my_button.grid(row=2, column=2)
progress_bar = ttk.Progressbar(root, orient=tk.HORIZONTAL, length=300, mode='indeterminate')
progress_bar.grid(row=3, column=2)
root.mainloop()
I'm trying to make a button where it will start a program. So first, a button called 'Run' will appear, subsequently after 3 seconds it should come up with a new button that says 'Stop'.
The reason I want it that way. That it's because I have tried and add two buttons on an interface panel, but the problem is then, every time I run an application, the interface freezes, so it was not possible to 'Stop' the program. So, I was wondering if it would be possible to do something like that?
What I've done:
from tkinter import *
tkWindow = Tk()
tkWindow.geometry('150x50')
tkWindow.title('Tkinter Example')
print("Tkinter button is appearing...")
def Action():
from Launch import Launch
run = Launch()
run
def Off():
import sys
sys.exit()
button = Button(tkWindow,
text='Start',
command=Action)
button1 = Button(tkWindow,
text='Stop',
command=Off)
button.pack()
button1.pack()
tkWindow.mainloop()
Try something like this:
from tkinter import *
from threading import Thread
from Launch import Launch
tkWindow = Tk()
tkWindow.geometry('150x50')
tkWindow.title('Tkinter Example')
print("Tkinter button is appearing...")
def Action():
thread = Thread(target=Launch, daemon=True)
thread.start()
# If you want to disable the button use:
# button.config(state="disabled")
button = Button(tkWindow,
text='Start',
command=Action)
# Here I am going to use the built-in `exit` function as per #Matiiss' suggestion
button1 = Button(tkWindow,
text='Stop',
command=exit)
button.pack()
button1.pack()
tkWindow.mainloop()
It starts a new thread when the "Start" button is pressed. That new thread calls Launch, leaving the main thread for tkinter. Please make sure that there isn't any references to your main GUI in your Launch function.
Here is what I coded...
import tkinter as tk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
app = tk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png")
myFont = font.Font(family='Helvetica', size=20, weight='normal')
tk.Label(app, text='EX', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
start = time.time()
cmd = sys.executable + " -c 'import time; time.sleep(2)' &"
subprocess.check_call(cmd, shell=True)
assert (time.time() - start) < 1
p = subprocess.Popen(cmd, shell=True)
def ex_activation():
#Python Code
#Python Code...
def ex_stop():
sys.exit(ex_activation) #This area is basically where I have a button to terminate the other script running.
#I have tried sys.exit() and had the same result
ex_activation_button = tk.Button(app,
bg='black',
image=photo,
width=120,
height=120,
command=ex_activation)
ex_stop_button = tk.Button(app,
bg='Gray',
text='ex',
width=12,
command=ex_stop
height=3)
ex_stop_button['font'] = myFont
app.title("Example")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
app.mainloop()
I am looking for a way to get my program to stop the program the other button runs. I realized that this maybe be a "self destruct button" but I don't know how to do this with the script the other button runs. Any help greatly appreciated! I tried killing the code by putting the def ex_activation in the p.kill
This did not work...
If the other python script is made to run forever (has some kind of while True:), you can't run it on the command line as you did, because it will freeze your window while that script is running.
In order to run a python script on background you will need to do it with the subprocess library. (Find out here)
I also found an answer of another question that uses check_ouput() in order to know when the python program has finished. This can also be useful if you want to send a status to the tkinter app: you can print("33% Complete"), for example. You could add this in tkinter's main loop, so you always know if your program is running or not.
And last but not least, to kill that process (using the stop button), you should do it using os, and looking for the subprocess' ID. Here you can also find a good example.
I would try something like this:
cmd = "exec python file.py"
p = subprocess.Popen(cmd, shell=True)
# Continue running tkinter tasks.
tk.update()
tk.update_idletasks() # These both lines should be inside a while True
# Stop secondary program
p.kill()
EDIT
Example code using your question's code. WARNING: I have changed the png file location for testing, commented the app icon, and tested ONLY on Windows.
It's important to remove the mainloop() on the main file and put update...() in order to catch the keyboardInterrupt that (I don't know why) is killing both parent and child process.
I invite you to try it and be as happy as I have been when it was working after half an hour of testing!!
File 1: daemon.py - this file will run forever.
from time import sleep
from sys import exit
while True:
try:
print("hello")
sleep(1)
except KeyboardInterrupt:
print("bye")
exit()
File 2: tkinterapp.py - The name is self-explainatory
import tkinter as tk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
app = tk.Tk()
app.geometry("400x400")
app.configure(bg='gray')
photo = tk.PhotoImage(file=r"C:\Users\royal\github\RandomSketches\baixa.png")
myFont = font.Font(family='Helvetica', size=20, weight='normal')
tk.Label(app, text='EX', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
# app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("running!")
pro = subprocess.Popen("python daemon.py", shell=True)
def ex_stop():
global pro
print("stopping!")
os.kill(pro.pid, 0)
ex_activation_button = tk.Button(app,
bg='black',
image=photo,
width=120,
height=120,
command=ex_activation)
ex_stop_button = tk.Button(app,
bg='Gray',
text='ex',
width=12,
command=ex_stop, # BE CAREFUL You were missing a "," here !!!
height=3)
ex_stop_button['font'] = myFont
app.title("Example")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass
I need two scripts two run simultaneously on click of a single button. Cannot use two buttons because the gui freezes after the first button click and waits for the first program to finish.
Here's the code:
import tkinter
import os
import subprocess
window = tkinter.Tk()
window.title("GUI")
def clicked():
os.system('python inference.py')
os.system('python extract_frames.py')
# I used the subprocess approach also but it still waits for the first program to finish
subprocess.run("python inference.py & python extract_frames.py",shell=True)
bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()
window.geometry('400x400')
window.mainloop()
Try adjusting it as thus:
import tkinter
import os
from subprocess import call
import threading
window = tkinter.Tk()
window.title("GUI")
def clicked():
#os.system('python inference.py')
#os.system('python extract_frames.py')
# I used the threading approach
threading.Thread(target=call, args=("python inference.py" ,), ).start()
threading.Thread(target=call, args=("python extract_frames.py" ,), ).start()
bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()
window.geometry('400x400')
window.mainloop()
Your code will look something like this.Its just a simple implementation.
from threading import Thread
import tkinter
import os
import subprocess
window = tkinter.Tk()
window.title("GUI")
def fun1():
os.system('python inference.py')
def fun2():
os.system('python extract_frames.py')
def clicked():
Thread(target = fun1).start()
Thread(target = fun2).start()
bt = tkinter.Button(window,text="Click Here to start detecting",command=clicked).pack()
window.geometry('400x400')
window.mainloop()
You can use two subprocess.Popen(...) to run the two scripts in separate processes:
import subprocess
import tkinter as tk
proclist = []
def clicked():
proclist.clear()
for script in ('inference.py', 'extract_frames.py'):
proc = subprocess.Popen(['python', script])
proclist.append(proc)
def kill_tasks():
for proc in proclist:
if proc and proc.poll() is None:
print('Killing process with PID', proc.pid)
proc.kill()
proclist.clear()
root = tk.Tk()
root.geometry('400x400')
root.title('GUI')
tk.Button(root, text='Start detecting', width=20, command=clicked).pack()
tk.Button(root, text='Kill tasks', width=20, command=kill_tasks).pack()
root.mainloop()
I want to create two processes with python script. There is need to start and stop those processes via tkinter button. Processes start correctly, but terminate not. Which simplest way to correctly terminate processes with tkinter button?
Which best-practice way?
from tkinter import *
import multiprocessing
def print1():
global a
while a == True:
print('im process 1')
def print2():
global a
while a == True:
print('im process 2')
def start():
process1.start()
process2.start()
def stop():
global a
a = False
a = True
if __name__ == '__main__':
process1 = multiprocessing.Process(target = print1)
process2 = multiprocessing.Process(target = print2)
root = Tk()
root.title("Title")
root.geometry("200x200")
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()
process1.join()
process2.join()
The problem appears to be in the stop method.
I think I might know what the problem is, but I'm not 100% sure. The answer to why appears to be in the python documentation. This code here runs fine (I edited the syntax and used tkk for the buttons, it looks better):
from tkinter import *
from tkinter import ttk
import multiprocessing
def print1():
global a
while a is True:
print('im process 1')
def print2():
global a
while a is True:
print('im process 2')
def start():
process1.start()
process2.start()
def stop():
process1.kill()
process2.kill()
a = True
if __name__ == '__main__':
process1 = multiprocessing.Process(target=print1)
process2 = multiprocessing.Process(target=print2)
root = Tk()
root.title("Title")
root.geometry("200x200")
app = Frame(root)
app.grid()
start = ttk.Button(app, text="Start", command=start)
stop = ttk.Button(app, text="Stop", command=stop)
start.grid(padx=15, pady=20)
stop.grid(column=1, row=0)
root.mainloop()
process1.join()
process2.join()
Hope this helps!
Ordinary variables are not shared between instances of a multiprocessing.Process.
This means your:
global a
is a different global variable in each process, separate from the third global a in your main Python program. So when you use the tk button to set that last a to False, the a in the process remembered through process1 is still True, as is the a in the process remembered through process2.
You can share variables across processes. There are two ways to do that: via Manager instances, or via shared memory. Shared memory is more efficient, but more difficult to use, and sometimes has OS dependencies, so if you don't need particularly high performance, consider using a Manager.