Skip to content

Instantly share code, notes, and snippets.

@kaleocheng
Forked from sp3c73r2038/gui.py
Last active August 29, 2015 14:25
Show Gist options
  • Save kaleocheng/234adba66d32b1428bab to your computer and use it in GitHub Desktop.
Save kaleocheng/234adba66d32b1428bab to your computer and use it in GitHub Desktop.
responsible GUI with multiprocessing.
# -*- coding: utf-8 -*-
from datetime import datetime
from multiprocessing import Process
from time import sleep
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.is_parent = True
# self.doing_work = False
def createWidgets(self):
self.hi_there = tk.Button(self)
self.hi_there['text'] = 'Do Work!\n(clikc me)'
self.hi_there['command'] = self.issue_command
self.hi_there.pack(side='top')
self.QUIT = tk.Button(self, text='QUIT', fg='red',
command=self.i_quit)
self.QUIT.pack(side='bottom')
def check_process(self):
self.mt.join(0.1)
if self.mt.is_alive():
self.after(500, self.check_process)
else:
print("ok")
def issue_command(self):
self.mt = Process(target=self.do_work)
self.mt.start()
# self.check_process()
def do_work(self):
self.is_parent = False
while True:
print(datetime.now())
sleep(0.5)
def i_quit(self):
self.mt.terminate()
while True:
print('waiting for thread ending...')
self.mt.join(1.0)
if not self.mt.is_alive():
if self.is_parent:
self.master.destroy()
return
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment