multithreading - Python GUI Threading -
i want have function run continuously within tkinter gui. have attached shell code:
#!/usr/bin/env python3 import tkinter tk 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 gui tends run 'run_continuously' function once , gui freezes up. suspect poking around due sleep function (which call in run_continuously function)
how go implementing 'run_continuously' function in simple thread around issue? running function in thread me around problem? 'run_continuously' function not need interact @ application class. want run in background , stop when mainloop finished.
here end of code:
def run_continuously(quit_flag): print("in it") if not quit_flag: gpio.output(dir_pin, true) 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) 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 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 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 minimal, complete, , verifiable example exits cleanly if 'quit' button pressed or ctrl-c 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): """ method required allow mainloop receive keyboard interrupts when frame not have 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()
Comments
Post a Comment