eventle是极其方便的绿色线程(协程)库,协程比线程、进程要易用(主要是不必考虑什么同步、信号等因素),而且由于没有cpu和os的切换动作,协程响应速度更快。
tkinter是古老的gui库,python原生自带,由于一些所见即所得的gui工具,给它新的生命力。我比较喜欢的是pygubu-designer,最大的好处是工具轻巧、简单,不需要网络在线,不需要import其他的库,可以直接生成界面python代码,不影响自己的原有思路。
关于如何在tkinter的gui界面上刷新数据,网上已有一些探索,主要是使用root.after的定时器方法进行刷新,不免有些"粗俗、笨重”。
经过两天的研究,把tkinter的root.mainloop重新写成协程化,就可以愉快的使用eventlet了,而且动态刷新数据也可以使用协程操作。mainloop中的协程主循环,先完成tkinter本身的两个update,然后在把协程切换出去。
def coroutine_mainloop(self, n=0):
while True:
self.mainwindow.update_idletasks()
self.mainwindow.update()
eventlet.sleep(n)
不过这样处理后,gui界面关闭时会出现奇怪的情况,若有其他协程没有关闭,gui界面关闭了,但是其他协程还在继续运行,tkinter的mainloop也会继续运转,必须用ctrl+c来退出程序。为此只好重新写一个close事件的处理,注册“WM_DELETE_WINDOW"的tkinter事件处理,绑定一个关闭处理过程,进行强制退出关闭。
def on_closing(self):
print ("on_closing")
self.mainwindow.quit()
self.mainwindow.destroy()
print ("self.mainwindow.destroy()")
os._exit(0)
def run_First(self):
#self.hub = eventlet.hubs.get_hub()
self.C1_running = False
self.C2_running = False
self.mainwindow.protocol("WM_DELETE_WINDOW", self.on_closing)
整个程序可以从下面复制,协程刷新数据的过程也很简单,启动一个新的协程,然后在新协程里对界面元素进行update。界面元素已经绑定了tk变量,直接set变量值,再对界面元素update,就能看到效果。
因为是协程的原因,所有代码都属于同一个主线程,都是公平的地位,对名字空间、变量的访问也是平等的,带来了清晰的思路和方便的操作。
这里设置一个self.C1_running变量,防止协程被重入。
def C1(self, event=None):
def C1_loop(self, Count):
print ("C1 Run")
while True:
if Count <= 0:
break
Count -= 1
print (Count, end=" ", flush=True)
eventlet.sleep(0.1)
self.intBt1.set(Count)
self.bt1.update()
self.C1_running = False
self.intBt1.set(100)
self.bt1.update()
if self.C1_running:
return
self.C1_running = True
run_loop = eventlet.spawn(C1_loop, self, 100)
整个示例程序在这里,直接复制后,即可运行
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function
# A example for use eventlet in tkinter to update gui data in real time
# programe by makefool@163.com
import eventlet
import os
#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
# Powered by pygubu-designer
# pygubu-designer
# pip install pygubu-designer
class GuiEventletApp:
def __init__(self, master=None):
# build ui
self.toplevel1 = tk.Tk() if master is None else tk.Toplevel(master)
self.toplevel1.configure(height=200, width=200)
self.toplevel1.resizable(False, False)
self.bt1 = ttk.Button(self.toplevel1)
self.intBt1 = tk.IntVar(value=100)
self.bt1.configure(text='100', textvariable=self.intBt1, width=30)
self.bt1.pack(side="top")
self.bt1.bind("<ButtonPress>", self.C1, add="")
self.bt2 = ttk.Button(self.toplevel1)
self.intBt2 = tk.IntVar(value=30)
self.bt2.configure(text='30', textvariable=self.intBt2, width=30)
self.bt2.pack(side="top")
self.bt2.bind("<ButtonPress>", self.C2, add="")
# Main widget
self.mainwindow = self.toplevel1
def run(self):
self.mainwindow.mainloop()
def C1(self, event=None):
def C1_loop(self, Count):
print ("C1 Run")
while True:
if Count <= 0:
break
Count -= 1
print (Count, end=" ", flush=True)
eventlet.sleep(0.1)
self.intBt1.set(Count)
self.bt1.update()
self.C1_running = False
self.intBt1.set(100)
self.bt1.update()
if self.C1_running:
return
self.C1_running = True
run_loop = eventlet.spawn(C1_loop, self, 100)
def C2(self, event=None):
def C2_loop(self, Count):
while True:
if Count <= 0:
break
Count -= 1
eventlet.sleep(0.1)
self.intBt2.set(Count)
self.bt2.update()
self.C2_running = False
self.intBt2.set(30)
self.bt2.update()
if self.C2_running:
return
self.C2_running = True
run_loop = eventlet.spawn(C2_loop, self, 30)
def on_closing(self):
print ("on_closing")
self.mainwindow.quit()
self.mainwindow.destroy()
print ("self.mainwindow.destroy()")
os._exit(0)
def run_First(self):
#self.hub = eventlet.hubs.get_hub()
self.C1_running = False
self.C2_running = False
self.mainwindow.protocol("WM_DELETE_WINDOW", self.on_closing)
def coroutine_mainloop(self, n=0):
while True:
self.mainwindow.update_idletasks()
self.mainwindow.update()
eventlet.sleep(n)
if __name__ == "__main__":
app = GuiEventletApp()
app.run_First()
app.coroutine_mainloop()