How to create two timers in one tkinter window using multiprocessing? - python

I want to create two times in one tkinter window using multiprocessing. I want to run two timers simultaneously. Here is the code. First it shows the window with the given text and then it shows error.
Code:
from multiprocessing import Process
from time import time, sleep
from tkinter import *
class Main:
def __init__(self):
self.root = Tk()
self.root.geometry('300x300')
self.lab1 = Label(self.root, text='None')
self.lab1.place(x=10, y=10)
self.lab2 = Label(self.root, text='None')
self.lab2.place(x=10, y=50)
self.root.mainloop()
def c1(self):
st = time()
for _ in range(10):
self.lab1['text'] = int(time()-st)
self.root.update()
sleep(1)
def c2(self):
st = time()
for _ in range(10):
self.lab2['text'] = int(time()-st)
self.root.update()
sleep(1)
def main(self):
self.p1 = Process(target=self.c1)
self.p2 = Process(target=self.c2)
self.p1.start()
self.p2.start()
self.p1.join()
self.p2.join()
if __name__ == '__main__':
start = Main()
start.main()

Related

Progress bar update in python

I search in stackoverflow how to implement a ProgressBar in tkinter. This is the code:
def worker(x):
return x+x
#time.sleep(1)
def compute():
pool = multiprocessing.Pool(2)
objects=range(1,10)
for i, _ in enumerate(pool.imap_unordered(worker, objects), 1):
#print("completed =" +str(i/len(listF)))
scanned.set((i/len(objects)*100))
if __name__ == '__main__':
window = Tk()
frame = Frame(master = window)
frame.grid(row=0,column=0)
button2 = Button(master=frame,text="Start", command=compute)
button2.pack()
frame = Frame(master = window)
scanned = IntVar()
frame.grid(row=3,column=0,padx=5,pady=10)
progress = Progressbar(master=frame, orient='horizontal',maximum=100, variable=scanned,mode='determinate')
progress.pack()
window.mainloop()
I found that this should work, but not in my case because the bar is update to 100% all in a time and I can not see the intermediate step.
Hi guys I solve the problem adding a Thread. This is the code:
def worker(x):
return x+x
#time.sleep(1)
def precompute():
t=threading.Thread(target=compute)
t.start()
def compute():
pool = multiprocessing.Pool(2)
objects=range(1,1000)
for i, _ in enumerate(pool.imap(worker, objects), 1):
#print("completed =" +str(i/len(listF)))
scanned.set((i/len(objects)*100))
if __name__ == '__main__':
window = Tk()
frame = Frame(master = window)
frame.grid(row=0,column=0)
button2 = Button(master=frame,text="Start", command=precompute)
button2.pack()
frame = Frame(master = window)
scanned = IntVar()
frame.grid(row=3,column=0,padx=5,pady=10)
progress = Progressbar(master=frame, orient='horizontal',maximum=100, variable=scanned,mode='determinate')
progress.pack()
window.mainloop()

How to call a method into a ticker Button as command

In the tkinter GUI I created a run Button. I liked to click the button then it should start counting. But When I call the method into the ttk.Button as command. it does not work. In this code two class was created. run method was created in the first class and it will be call into the second class. Could you please kindly check the code. Thanks in advance.
from tkinter import *
import threading
import queue
from time import sleep
import random
from tkinter import ttk
class Thread_0(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
count = 0
while True:
count +=1
hmi.thread_0_update(count)
sleep(random.random()/100)
class HMI:
def __init__(self):
self.master=Tk()
self.master.geometry('200x200+1+1')
self.f=ttk.Frame(self.master,height = 100, width = 100, relief= 'ridge')
self.f.grid(row=1,column=1, padx=20, pady=20)
self.l0=ttk.Label(self.f)
self.l0.grid(row=1,column=1)
self.button=ttk.Button(self.master, text = 'run')
self.button.grid(row=2,column=2)
self.q0=queue.Queue()
self.master.bind("<<Thread_0_Label_Update>>",self.thread_0_update_e)
def start(self):
self.master.mainloop()
self.master.destroy()
#################################
def thread_0_update(self,val):
self.q0.put(val)
self.master.event_generate('<<Thread_0_Label_Update>>',when='tail')
def thread_0_update_e(self,e):
while self.q0.qsize():
try:
val=self.q0.get()
self.l0.config(text=str(val), font = ('Times New Roman', 15))
except queue.Empty:
pass
##########################
if __name__=='__main__':
hmi=HMI()
t0=Thread_0()
t0.start()
hmi.start()
You can use
Button( ..., command=t0.start )
See: start is without (). But you have to create t0 before hmi
if __name__ == '__main__':
t0 = Thread_0()
hmi = HMI()
hmi.start()
Full code which works for me
from tkinter import *
import threading
import queue
from time import sleep
import random
from tkinter import ttk
class Thread_0(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
count = 0
while True:
count +=1
hmi.thread_0_update(count)
sleep(random.random()/100)
class HMI:
def __init__(self):
self.master=Tk()
self.master.geometry('200x200+1+1')
self.f = ttk.Frame(self.master, height=100, width=100, relief='ridge')
self.f.grid(row=1, column=1, padx=20, pady=20)
self.l0 = ttk.Label(self.f)
self.l0.grid(row=1, column=1)
self.button = ttk.Button(self.master, text='run', command=t0.start)
self.button.grid(row=2, column=2)
self.q0 = queue.Queue()
self.master.bind("<<Thread_0_Label_Update>>", self.thread_0_update_e)
def start(self):
self.master.mainloop()
self.master.destroy()
#################################
def thread_0_update(self,val):
self.q0.put(val)
self.master.event_generate('<<Thread_0_Label_Update>>', when='tail')
def thread_0_update_e(self,e):
while self.q0.qsize():
try:
val = self.q0.get()
self.l0.config(text=str(val), font=('Times New Roman', 15))
except queue.Empty:
pass
##########################
if __name__ == '__main__':
t0 = Thread_0()
hmi = HMI()
hmi.start()

How to start a separate new Tk() window using multiprocessing?

The following code is runnable, you can just copy/paste:
from tkinter import *
import multiprocessing
startingWin = Tk()
def createClientsWin():
def startProcess():
clientsWin = Tk()
label = Label(clientsWin, text="Nothing to show")
label.grid()
clientsWin.mainloop()
if __name__ == "__main__":
p = multiprocessing.Process(target=startProcess)
p.start()
button = Button(startingWin, text="create clients", command=lambda: createClientsWin())
button.grid()
startingWin.mainloop()
So I simply want to create a completely separated Tk() window using multiprocessing. When I click on the create button, I just get the original window (not the intended one) and it gives me this error:
AttributeError: Can't pickle local object 'createClientsWin.<locals>.startProcess'
*Could someone explain how to start a separate new Tk() window using multiprocessing? *
Update: Not A Duplicate
Even if I follow the solution provided in the possible duplicate question, that doesn't help solving my question. Simply because, Tkinter is being used in my case. The modified code:
def createClientsWin():
clientsWin = Tk()
label = Label(clientsWin, text="Nothing to show")
label.grid()
clientsWin.mainloop()
def createClientsWinProcess():
if __name__ == "__main__":
p = multiprocessing.Process(target=createClientsWin)
p.start()
startingWin = Tk()
button = Button(startingWin, text="create clients", command=lambda: createClientsWinProcess())
button.grid()
startingWin.mainloop()
Function in global scope should be used for multiprocess target function, so the startProcess() should be moved into global scope.
Also the checking of if __name__ == "__main__" inside startProcess() will cause the code inside the if block not being executed.
Finally the creation of startingWin should be put inside if __name__ == "__main__" block, otherwise every process started will create the startingWin.
Below is the proposed changes to solve the above issues:
from tkinter import *
import multiprocessing
def startProcess():
clientsWin = Tk()
label = Label(clientsWin, text="Nothing to show")
label.grid()
clientsWin.mainloop()
def createClientsWin():
p = multiprocessing.Process(target=startProcess)
p.start()
if __name__ == '__main__':
startingWin = Tk()
button = Button(startingWin, text="create clients", command=createClientsWin)
button.grid()
startingWin.mainloop()
It is easier to use classes when using multiprocessing with tkinter. Try the following code:
import tkinter as Tk
import multiprocessing as mp
class A:
def __init__(self, master):
self.master = master
label = Tk.Label(self.master, text = 'A')
label.pack()
root_b = Tk.Toplevel()
GUI_B = B(root_b)
mp.Process(target = GUI_B.mainloop())
def mainloop(self):
self.master.mainloop()
class B:
def __init__(self, master):
self.master = master
label = Tk.Label(self.master, text = 'B')
label.pack()
def mainloop(self):
self.master.mainloop()
if __name__=='__main__':
root = Tk.Tk()
GUI_A = A(root)
mp.Process(target = GUI_A.mainloop())

Display log lines on screen using parallel GUI

I'm looking for elegant way to display log lines on screen while the script is running.
from time import sleep
from threading import Thread
import tkinter as tk
class WaitGuiPrallel(Thread):
def __init__(self, TXT='Wait!', ttl='Logs'):
self.txt = TXT
Thread.__init__(self)
self.ttl = ttl
self.start() # This is starting the self.run()
def run(self):
self.root = tk.Tk()
self.root.attributes("-topmost", True)
self.root.title(self.ttl)
self.label = tk.Label(self.root, text=self.txt, font=("Helvetica", 20))
self.label.pack()
self.Location()
self.root.mainloop()
def Exit(self):
self.root.quit()
def Location(self):
w = 500 # width for the Tk root
h = 150 # height for the Tk root
ws = self.root.winfo_screenwidth() # width of the screen
self.root.geometry('%dx%d+%d+%d' % (w, h, ws - w - 20, 10))
def Update(self, newText):
self.txt1 = newText
self.label.destroy()
self.label = tk.Label(self.root, text=self.txt1,
font=("Helvetica", 12))
self.label.pack()
self.root.update()
Wait = WaitGuiPrallel(TXT='Wait! Do not touch mouse or keyboard')
sleep(2)
for t in range(5):
sleep(1)
Wait.Update(newText='Log line %s' % t)
Wait.Update(newText='Done!')
sleep(1)
Wait.Exit()
The current script got few issues:
it is not elegant - there must be a better way
it has problems when updated from different Threads
Sometime running it twice from Spyder+IPython is not possible (IPython freeze)
Tkinter doesn't really play well with threads. Using a StringVar is a little more thread friendly than other methods (in my experience). Here's how to do that plus a couple other fixes:
from time import sleep
from threading import Thread
import tkinter as tk
class WaitGuiPrallel(Thread):
def __init__(self, TXT='Wait!', ttl='Logs'):
Thread.__init__(self)
self.txt = TXT
self.ttl = ttl # what's this for?
self.daemon = True # this thread will terminate when the main thread terminates
self.start() # This is starting the self.run()
def run(self):
self.root = tk.Tk()
self.root.attributes("-topmost", True)
self.root.title(self.ttl)
self.txt = tk.StringVar(value=self.txt)
self.label = tk.Label(self.root, textvariable=self.txt, font=("Helvetica", 20))
self.label.pack()
self.Location()
self.root.mainloop()
def Location(self):
w = 500 # width for the Tk root
h = 150 # height for the Tk root
ws = self.root.winfo_screenwidth() # width of the screen
self.root.geometry('%dx%d+%d+%d' % (w, h, ws - w - 20, 10))
Wait = WaitGuiPrallel(TXT='Wait! Do not touch mouse or keyboard')
sleep(2)
for t in range(5):
sleep(1)
Wait.txt.set('Log line %s' % t)
Wait.txt.set('Done!')
sleep(1)
If you have multiple threads calling this then I would consider using a Queue and a third thread to monitor the queue.

Python GUI Threading

I want to have a function run continuously within a tkinter GUI. I have attached some shell code:
#!/usr/bin/env python3
import tkinter as tk
from time import sleep
import os
import sys
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
......
root = Tk()
def run_continously:
... -- calls sleep -- ...
root.after(3000, run_continuously)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
app = Application(root)
root.after(3000, run_continuously)
root.mainloop()
When running the GUI it tends to run the 'run_continuously' function once and the GUI freezes up. I suspect from poking around that this is due to the sleep function (which I call in the run_continuously function)
How would I go about implementing the 'run_continuously' function in a very simple thread to get around this issue? Would running the function in a thread even get me around the problem? The 'run_continuously' function does not need to interact at all with the Application class. I want it to run simply in the background and stop when the mainloop is finished.
Here is the end of the code:
def run_continuously(quit_flag):
print("in it")
if not quit_flag:
GPIO.output(DIR_PIN, True)
for i in range(steps):
print("in loop")
GPIO.output(STEP_PIN, True)
sleep(sDelay)
GPIO.output(STEP_PIN, False)
sleep(sDelay)
sleep(wait_time)
GPIO.output(DIR_PIN, False)
for i in range(steps):
GPIO.output(STEP_PIN, True)
sleep(sDelay)
GPIO.output(STEP_PIN, False)
sleep(sDelay)
print("run motor")
root.after(1000, run_continuously(quit_flag,))
#=================================================================
# main
#=================================================================
root = Tk() # Create the GUI root object
press1 = StringVar()
press2 = StringVar()
x = 275
y = 50
w = 580
h = 250
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
app = Application(root) # Create the root application window
quit_flag = False
root.after(0, app.read_pressure)
motor_thread = threading.Thread(target=run_continuously, args=(quit_flag,)).start()
root.mainloop()
quit_flag=True
motor_thread.join()
This is a minimal, complete, and verifiable example that exits cleanly if the 'QUIT' button is pressed or Ctrl-C is pressed:
from Tkinter import *
import multiprocessing
import threading
import time
import logging
class Application(Frame):
def create_widgets(self):
self.quit_button = Button(self)
self.quit_button['text'] = 'QUIT'
self.quit_button['fg'] = 'red'
self.quit_button['command'] = self.quit
self.quit_button.pack({'side': 'left'})
def __init__(self, master=None):
Frame.__init__(self, master)
self.quit_button = None
self.pack()
self.create_widgets()
self.poll()
def poll(self):
"""
This method is required to allow the mainloop to receive keyboard
interrupts when the frame does not have the focus
"""
self.master.after(250, self.poll)
def worker_function(quit_flag):
counter = 0
while not quit_flag.value:
counter += 1
logging.info("Work # %d" % counter)
time.sleep(1.0)
format = '%(levelname)s: %(filename)s: %(lineno)d: %(message)s'
logging.basicConfig(level=logging.DEBUG, format=format)
root = Tk()
app = Application(master=root)
quit_flag = multiprocessing.Value('i', int(False))
worker_thread = threading.Thread(target=worker_function, args=(quit_flag,))
worker_thread.start()
logging.info("quit_flag.value = %s" % bool(quit_flag.value))
try:
app.mainloop()
except KeyboardInterrupt:
logging.info("Keyboard interrupt")
quit_flag.value = True
logging.info("quit_flag.value = %s" % bool(quit_flag.value))
worker_thread.join()

Categories

Resources