https://blog.csdn.net/u013700771/article/details/103321783
- demo1 ```python from tkinter import * from tkinter import ttk import time
class GUI():
def __init__(self, root):self.initGUI(root)def initGUI(self, root):root.title("test")root.geometry("400x200+700+500")root.resizable = Falseself.button_1 = Button(root, text="run A", width=10, command=self.A)self.button_1.pack(side="top")self.button_2 = Button(root, text="run B", width=10, command=self.B)self.button_2.pack(side="top")root.mainloop()def A(self):print ("start to run proc A")time.sleep(3)print( "proc A finished")def B(self):print ("start to run proc B")time.sleep(3)print ("proc B finished")
if name == “main“:
root = Tk()myGUI = GUI(root)
- 多线程```pythonfrom tkinter import *from tkinter import ttkimport threadingimport timeclass GUI():def __init__(self, root):self.initGUI(root)def initGUI(self, root):root.title("test")root.geometry("400x200+700+500")root.resizable = Falseself.button_1 = Button(root, text="run A", width=10, command=self.A)self.button_1.pack(side="top")self.button_2 = Button(root, text="run B", width=10, command=self.B)self.button_2.pack(side="top")root.mainloop()def __A(self):print ("start to run proc A")time.sleep(3)print ("proc A finished")def A(self):T = threading.Thread(target=self.__A)T.start()def __B(self):print( "start to run proc B")time.sleep(3)print ("proc B finished")def B(self):T = threading.Thread(target=self.__B)T.start()if __name__ == "__main__":root = Tk()myGUI = GUI(root)
- demo2 ```python from tkinter import * from tkinter import ttk import threading import time import sys
def fmtTime(timeStamp): timeArray = time.localtime(timeStamp) dateTime = time.strftime(“%Y-%m-%d %H:%M:%S”, timeArray) return dateTime
class re_Text():
def __init__(self, widget):self.widget = widgetdef write(self, content):self.widget.insert(INSERT, content)self.widget.see(END)
class GUI():
def __init__(self, root):self.initGUI(root)def initGUI(self, root):root.title("test")root.geometry("400x200+700+500")root.resizable = Falseself.button = Button(root, text="click", width=10, command=self.show)self.button.pack(side="top")self.scrollBar = Scrollbar(root)self.scrollBar.pack(side="right", fill="y")self.text = Text(root, height=10, width=45, yscrollcommand=self.scrollBar.set)self.text.pack(side="top", fill=BOTH, padx=10, pady=10)self.scrollBar.config(command=self.text.yview)sys.stdout = re_Text(self.text)root.mainloop()def show(self):i = 0while i < 3:print (fmtTime(time.time()))time.sleep(1)i += 1
if name == “main“:
root = Tk()myGUI = GUI(root)
- 子线程更新GUI```pythonfrom tkinter import *from tkinter import ttkimport threadingimport timeimport sysdef fmtTime(timeStamp):timeArray = time.localtime(timeStamp)dateTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)return dateTimeclass re_Text():def __init__(self, widget):self.widget = widgetdef write(self, content):self.widget.insert(INSERT, content)self.widget.see(END)class GUI():def __init__(self, root):self.initGUI(root)def initGUI(self, root):root.title("test")root.geometry("400x200+700+500")root.resizable = Falseself.button = Button(root, text="click", width=10, command=self.show)self.button.pack(side="top")self.scrollBar = Scrollbar(root)self.scrollBar.pack(side="right", fill="y")self.text = Text(root, height=10, width=45, yscrollcommand=self.scrollBar.set)self.text.pack(side="top", fill=BOTH, padx=10, pady=10)self.scrollBar.config(command=self.text.yview)sys.stdout = re_Text(self.text)root.mainloop()def __show(self):i = 0while i < 3:print (fmtTime(time.time()))time.sleep(1)i += 1def show(self):T = threading.Thread(target=self.__show, args=())T.start()if __name__ == "__main__":root = Tk()myGUI = GUI(root)
- 队列 ```python from tkinter import * from tkinter import ttk import queue import time
q = queue.Queue() for i in range(0, 4): time.sleep(0.5) element = “element %s”%i print (“put %s”%element) q.put(element)
while not q.empty(): time.sleep(0.5) print (“get %s”% q.get())
```pythonfrom tkinter import *from tkinter import ttkclass GUI():def __init__(self, root):self.initGUI(root)def loop(self):print ("loop proc running")self.root.after(1000, self.loop)def initGUI(self, root):self.root = rootself.root.title("test")self.root.geometry("400x200+80+600")self.root.resizable = Falseself.root.after(1000, self.loop)self.root.mainloop()if __name__ == "__main__":root = Tk()myGUI = GUI(root)
from tkinter import *from tkinter import ttk# coding=utf-8import threadingimport timeimport sysimport queuedef fmtTime(timeStamp):timeArray = time.localtime(timeStamp)dateTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)return dateTime#自定义re_Text,用于将stdout映射到Queueclass re_Text():def __init__(self, queue):self.queue = queuedef write(self, content):self.queue.put(content)class GUI():def __init__(self, root):#new 一个Quue用于保存输出内容self.msg_queue = queue.Queue()self.initGUI(root)#在show_msg方法里,从Queue取出元素,输出到Textdef show_msg(self):while not self.msg_queue.empty():content = self.msg_queue.get()self.text.insert(INSERT, content)self.text.see(END)#after方法再次调用show_msgself.root.after(100, self.show_msg)def initGUI(self, root):self.root = rootself.root.title("test")self.root.geometry("400x200+700+500")self.root.resizable = Falseself.button = Button(self.root, text="click", width=10, command=self.show)self.button.pack(side="top")self.scrollBar = Scrollbar(self.root)self.scrollBar.pack(side="right", fill="y")self.text = Text(self.root, height=10, width=45, yscrollcommand=self.scrollBar.set)self.text.pack(side="top", fill=BOTH, padx=10, pady=10)self.scrollBar.config(command=self.text.yview)#启动after方法self.root.after(100, self.show_msg)#将stdout映射到re_Textsys.stdout = re_Text(self.msg_queue)root.mainloop()def __show(self):i = 0while i < 3:print (fmtTime(time.time()))time.sleep(1)i += 1def show(self):T = threading.Thread(target=self.__show, args=())T.start()if __name__ == "__main__":root = Tk()myGUI = GUI(root)
