wxpython multithreading

Copyright: original works, welcome to forward! Forwarding attached link https://blog.csdn.net/qq_26369907/article/details/90408513

He jumped into a pit while doing UI with wxpython, in this record.
Summary:
1. First check whether the language itself comes with a module to learn the language. For example python3 powerful multi-threaded threading capabilities, combined with jion () and setDaemon () function is more flexible, first thought of threading when using multiple threads, but wxpython is a UI framework written in python, which comes with its own multithreaded function:

  • wx.PostEvent
  • wx.CallAfter
  • wx.CallLater

The three rounding, threading directly cause the UI Caton, stuck and so on. Combined wxpython comes with multi-thread function works well.
2. When using multiple threads do UI, recommended that the main thread UI display relevant data is plotted, and the child thread only logical processing. For example: Message wxPython neutron thread calls the method wx.CallAfter () after the main UI thread can handle child thread sent after the end of the current event handler, this is the easiest way to wxPython news neutron thread sends to the main thread.

Case
implement start timing in the main thread, the child thread output progress bar.

# coding = utf-8
import wx
import time
import threading

class ProgressBarThread(threading.Thread):
    """进度条类  """
    def __init__(self, parent):
        """
        :param parent:  主线程UI
        :param timer:  计时器
        """
        super(ProgressBarThread, self).__init__()  # 继承
        self.parent = parent
        self.setDaemon(True)  # 设置为守护线程, 即子线程是守护进程,主线程结束子线程也随之结束。

    def run(self):
        count = 0
        while count < 5:
            count = count + 0.5
            time.sleep(0.5)
            wx.CallAfter(self.parent.update_process_bar, count)  # 更新进度条进度
        wx.CallAfter(self.parent.close_process_bar)  #  destroy进度条

class CounterThread(threading.Thread):
“”计时类“”
    def __init__(self, parent):
        super(CounterThread, self).__init__()
        self.parent = parent
        self.setDaemon(True)

    def run(self):
        i = 0
        while i < 5:
            i += 1
            print(i)
            time.sleep(1)
            
class AppUI(wx.Frame):
    def __init__(self, parent, title="test thread"):
        wx.Frame.__init__(self, parent, title=title)
        self.Center()

        self.panel = wx.Panel(parent=self)
        self.button_panel = wx.Panel(self.panel)
        self.button_start = wx.Button(self.panel, wx.ID_ANY, label="开始")

        self.button_start.Bind(wx.EVT_BUTTON, self.on_click_start)
        self.dialog = None

    def update_process_bar(self, count):
        self.dialog.Update(count)

    def close_process_bar(self):
        self.dialog.Destroy()

    def on_click_start(self, evt):
        self.dialog = wx.ProgressDialog("录音进度", "Time remaining", 5, style=wx.PD_AUTO_HIDE | wx.PD_ELAPSED_TIME | wx.PD_REMAINING_TIME)
        self.progress_1 = ProgressBarThread(self)
        self.progress_1.start()
        self.counter = CounterThread(self)
        self.counter.start()


if __name__ == "__main__":
    app = wx.App(False)
    AppUI(None).Show()
    app.MainLoop()

UI Figure:
Here Insert Picture Description
Reference links:
https://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
https://blog.csdn.net/fengmm521/article/details/78446413

Guess you like

Origin blog.csdn.net/qq_26369907/article/details/90408513