https://blog.csdn.net/u013700771/article/details/103321783

    • demo1 ```python from tkinter import * from tkinter import ttk import time

    class GUI():

    1. def __init__(self, root):
    2. self.initGUI(root)
    3. def initGUI(self, root):
    4. root.title("test")
    5. root.geometry("400x200+700+500")
    6. root.resizable = False
    7. self.button_1 = Button(root, text="run A", width=10, command=self.A)
    8. self.button_1.pack(side="top")
    9. self.button_2 = Button(root, text="run B", width=10, command=self.B)
    10. self.button_2.pack(side="top")
    11. root.mainloop()
    12. def A(self):
    13. print ("start to run proc A")
    14. time.sleep(3)
    15. print( "proc A finished")
    16. def B(self):
    17. print ("start to run proc B")
    18. time.sleep(3)
    19. print ("proc B finished")

    if name == “main“:

    1. root = Tk()
    2. myGUI = GUI(root)
    1. - 多线程
    2. ```python
    3. from tkinter import *
    4. from tkinter import ttk
    5. import threading
    6. import time
    7. class GUI():
    8. def __init__(self, root):
    9. self.initGUI(root)
    10. def initGUI(self, root):
    11. root.title("test")
    12. root.geometry("400x200+700+500")
    13. root.resizable = False
    14. self.button_1 = Button(root, text="run A", width=10, command=self.A)
    15. self.button_1.pack(side="top")
    16. self.button_2 = Button(root, text="run B", width=10, command=self.B)
    17. self.button_2.pack(side="top")
    18. root.mainloop()
    19. def __A(self):
    20. print ("start to run proc A")
    21. time.sleep(3)
    22. print ("proc A finished")
    23. def A(self):
    24. T = threading.Thread(target=self.__A)
    25. T.start()
    26. def __B(self):
    27. print( "start to run proc B")
    28. time.sleep(3)
    29. print ("proc B finished")
    30. def B(self):
    31. T = threading.Thread(target=self.__B)
    32. T.start()
    33. if __name__ == "__main__":
    34. root = Tk()
    35. 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():

    1. def __init__(self, widget):
    2. self.widget = widget
    3. def write(self, content):
    4. self.widget.insert(INSERT, content)
    5. self.widget.see(END)

    class GUI():

    1. def __init__(self, root):
    2. self.initGUI(root)
    3. def initGUI(self, root):
    4. root.title("test")
    5. root.geometry("400x200+700+500")
    6. root.resizable = False
    7. self.button = Button(root, text="click", width=10, command=self.show)
    8. self.button.pack(side="top")
    9. self.scrollBar = Scrollbar(root)
    10. self.scrollBar.pack(side="right", fill="y")
    11. self.text = Text(root, height=10, width=45, yscrollcommand=self.scrollBar.set)
    12. self.text.pack(side="top", fill=BOTH, padx=10, pady=10)
    13. self.scrollBar.config(command=self.text.yview)
    14. sys.stdout = re_Text(self.text)
    15. root.mainloop()
    16. def show(self):
    17. i = 0
    18. while i < 3:
    19. print (fmtTime(time.time()))
    20. time.sleep(1)
    21. i += 1

    if name == “main“:

    1. root = Tk()
    2. myGUI = GUI(root)
    1. - 子线程更新GUI
    2. ```python
    3. from tkinter import *
    4. from tkinter import ttk
    5. import threading
    6. import time
    7. import sys
    8. def fmtTime(timeStamp):
    9. timeArray = time.localtime(timeStamp)
    10. dateTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    11. return dateTime
    12. class re_Text():
    13. def __init__(self, widget):
    14. self.widget = widget
    15. def write(self, content):
    16. self.widget.insert(INSERT, content)
    17. self.widget.see(END)
    18. class GUI():
    19. def __init__(self, root):
    20. self.initGUI(root)
    21. def initGUI(self, root):
    22. root.title("test")
    23. root.geometry("400x200+700+500")
    24. root.resizable = False
    25. self.button = Button(root, text="click", width=10, command=self.show)
    26. self.button.pack(side="top")
    27. self.scrollBar = Scrollbar(root)
    28. self.scrollBar.pack(side="right", fill="y")
    29. self.text = Text(root, height=10, width=45, yscrollcommand=self.scrollBar.set)
    30. self.text.pack(side="top", fill=BOTH, padx=10, pady=10)
    31. self.scrollBar.config(command=self.text.yview)
    32. sys.stdout = re_Text(self.text)
    33. root.mainloop()
    34. def __show(self):
    35. i = 0
    36. while i < 3:
    37. print (fmtTime(time.time()))
    38. time.sleep(1)
    39. i += 1
    40. def show(self):
    41. T = threading.Thread(target=self.__show, args=())
    42. T.start()
    43. if __name__ == "__main__":
    44. root = Tk()
    45. 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())

    1. ```python
    2. from tkinter import *
    3. from tkinter import ttk
    4. class GUI():
    5. def __init__(self, root):
    6. self.initGUI(root)
    7. def loop(self):
    8. print ("loop proc running")
    9. self.root.after(1000, self.loop)
    10. def initGUI(self, root):
    11. self.root = root
    12. self.root.title("test")
    13. self.root.geometry("400x200+80+600")
    14. self.root.resizable = False
    15. self.root.after(1000, self.loop)
    16. self.root.mainloop()
    17. if __name__ == "__main__":
    18. root = Tk()
    19. myGUI = GUI(root)
    1. from tkinter import *
    2. from tkinter import ttk
    3. # coding=utf-8
    4. import threading
    5. import time
    6. import sys
    7. import queue
    8. def fmtTime(timeStamp):
    9. timeArray = time.localtime(timeStamp)
    10. dateTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    11. return dateTime
    12. #自定义re_Text,用于将stdout映射到Queue
    13. class re_Text():
    14. def __init__(self, queue):
    15. self.queue = queue
    16. def write(self, content):
    17. self.queue.put(content)
    18. class GUI():
    19. def __init__(self, root):
    20. #new 一个Quue用于保存输出内容
    21. self.msg_queue = queue.Queue()
    22. self.initGUI(root)
    23. #在show_msg方法里,从Queue取出元素,输出到Text
    24. def show_msg(self):
    25. while not self.msg_queue.empty():
    26. content = self.msg_queue.get()
    27. self.text.insert(INSERT, content)
    28. self.text.see(END)
    29. #after方法再次调用show_msg
    30. self.root.after(100, self.show_msg)
    31. def initGUI(self, root):
    32. self.root = root
    33. self.root.title("test")
    34. self.root.geometry("400x200+700+500")
    35. self.root.resizable = False
    36. self.button = Button(self.root, text="click", width=10, command=self.show)
    37. self.button.pack(side="top")
    38. self.scrollBar = Scrollbar(self.root)
    39. self.scrollBar.pack(side="right", fill="y")
    40. self.text = Text(self.root, height=10, width=45, yscrollcommand=self.scrollBar.set)
    41. self.text.pack(side="top", fill=BOTH, padx=10, pady=10)
    42. self.scrollBar.config(command=self.text.yview)
    43. #启动after方法
    44. self.root.after(100, self.show_msg)
    45. #将stdout映射到re_Text
    46. sys.stdout = re_Text(self.msg_queue)
    47. root.mainloop()
    48. def __show(self):
    49. i = 0
    50. while i < 3:
    51. print (fmtTime(time.time()))
    52. time.sleep(1)
    53. i += 1
    54. def show(self):
    55. T = threading.Thread(target=self.__show, args=())
    56. T.start()
    57. if __name__ == "__main__":
    58. root = Tk()
    59. myGUI = GUI(root)