6691 views|0 replies

2145

Posts

8

Resources
The OP
 

How to solve the problem of syntax error when importing wx.lib.pubsub when python2.x + wxpython multithreading [Copy link]

This post was last edited by wsmysyn on 2018-7-12 17:58 RT Background: Recently, I am going to use python + wxpython multithreading for automatic testing; the environment is windows10 + python2.7.15 + wxpython4.0.3. Since the automatic testing needs to communicate with the lower computer for a long time, it will take a long time. If multithreading is not used, the UI interface will be blocked and cause pseudo-death. Problem phenomenon: I found a multithreading tutorial on the Internet. . I tried it with great joy, and found that it prompted the following syntax error: It felt strange, why would the automatically installed package prompt a syntax error (I have seen the syntax of python3. ) I found the following prompt in the instructions in the package installation directory: It prompted that version 4.0.0 had stopped supporting python2.x, so the question is, why would it download a package that does not support python2? ? Solution: Since it is not supported, find a supported package; the pypubsub installed by pip by default are all the latest packages. You can force the installation of a lower version of the package through pip install PyPubSub==3.3.0 (3.3.0 version is to enter a version number at random, pip will prompt which are the current correct version numbers, and choose the lowest one, which is 3.3.0). Check the note in the package directory to confirm that python2 can be supported. After installation, run the script again and you will find that there is no syntax error. Problem solved... Reference The following is an online method about wxpython multithreading
Some time ago, I wrote an industrial control software. There has always been a problem in use. When the software searches for devices, because this function takes a long time to execute, the GUI interface freezes, making it difficult for users to tell whether the software is still executing or has really crashed. (Although I designed a synchronous log display, this also freezes.) The program screenshot is as follows: The code analysis is as follows:
  1. # -*- coding: utf-8 -*- import time import wx from threading import Thread from wx.lib.pubsub import Publisher
复制代码
The time library is used to execute timing functions and simulate functions that require a long time to execute. Publisher is used to transmit messages between threads.
  1. class TestThread(Thread): def __init__(self): #Start the thread immediately when it is instantiated Thread.__init__(self) self.start() def run(self): #Code for the thread to execute for i in range(101): time.sleep(0.03) wx.CallAfter(Publisher().sendMessage, "update", i) time.sleep(0.5) wx.CallAfter(Publisher().sendMessage, "update", u"Thread ends")
复制代码
The __init__ function of TestThread defines that the thread will start when it is instantiated, and the run function is the program that needs to be executed for a long time. wx.CallAfter and Publisher().sendMessage are used to send messages to the GUI.
  1. class MyForm ( wx.Frame ): def __init__( self, parent ): wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Leniy, 20140627", pos = wx.DefaultPosition, size = wx.Size( -1,-1 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL ) self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize ) gSizer2 = wx.GridSizer( 0, 3, 0, 0 ) self.m_button2 = wx.Button( self, wx.ID_ANY, u"execution thread", wx.DefaultPosition, wx.DefaultSize, 0 ) gSizer2.Add( self.m_button2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 ) self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"MyLabel", wx.DefaultPosition, wx.DefaultSize, 0 ) self.m_staticText2.Wrap( -1 ) gSizer2.Add( self.m_ staticText2, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5) self.m_gauge1 = wx.Gauge( self, wx.ID_ANY, 100, wx.DefaultPosition, wx.DefaultSize, wx.GA_HORIZONTAL) self.m_gauge1.SetValue( 0 ) g Sizer2.Add( self.m_gauge1, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 5 ) self.SetSizer( gSizer2 ) self.Layout() gSizer2.Fit( self ) self.Centre( wx.BOTH ) self.m_button2.Bind( wx.EVT_BUTTON, self.onButton ) Publisher().subscribe(self.updateDisplay, "update") def updateDisplay(self, msg): t = msg.data if isinstance(t, int):#If it is a number, it means the thread is executing and the number is displayed self.m_staticText2.SetLabel("%s%%" % t) self.m_gauge1.SetValue( t ) else:#Otherwise the thread is not executed, turn the button back on self.m_staticText2.SetLabel("%s" % t) self.m_button2.Enable() def onButton( self, event ): TestThread() self.m_staticText2.SetLabel(u"Thread Start") event.GetEventObject().Disable()
复制代码
This is the main program of the GUI. In __init__, Publisher().subscribe(self.updateDisplay, "update") is used to declare the method of getting thread messages and displaying them.
  1. if __name__ == "__main__": app = wx.PySimpleApp() MyForm(None).Show() app.MainLoop()
复制代码
Finally, display the window MyForm(None).Show() and the program is complete.


Personal signature坐而言不如起而行
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list