wxpython 多线程_在wxPython中使用线程连续更新GUI的好方法?
我正在開發一個使用pythonv2.7和wxpythonv3.0的GUI應用程序。
我必須不斷更新我的圖形用戶界面,其中包含許多面板。每個面板包含一個wx.StaticText。我必須不斷更新這些wx.StaticTexts。我想用threads。我還使用pubsub模塊與GUI通信來更新這些wx.StaticTexts。每件事都按計劃進行。
我創建了一個簡短的演示下面我的真實問題。在
問題:在我下面的代碼中,創建了兩個線程。這兩個線程都能夠使用wx.CallAfter()更新GUI。如果我有100個面板要更新怎么辦?我需要為每個更新特定面板的線程創建100個類嗎?我希望線程獨立于其他線程工作。
還有什么比這個更好的方法呢?在
代碼:請找到下面的示例代碼來進行演示:import wx
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub
import time
from threading import Thread
import threading
class GUI(wx.Frame):
def __init__(self, parent, id, title):
screenWidth = 500
screenHeight = 400
screenSize = (screenWidth,screenHeight)
wx.Frame.__init__(self, None, id, title, size=screenSize)
self.locationFont = locationFont = wx.Font(12, wx.MODERN, wx.NORMAL, wx.BOLD)
mainSizer = wx.BoxSizer(wx.VERTICAL)
myPanelA = wx.Panel(self, style=wx.SIMPLE_BORDER)
myPanelA.SetBackgroundColour('#C0FAE0')
self.myTextA = wx.StaticText(myPanelA, -1, "I have a problem :( ")
myPanelB = wx.Panel(self, style=wx.SIMPLE_BORDER)
myPanelB.SetBackgroundColour('#C0FAFF')
self.myTextB = wx.StaticText(myPanelB, -1, "Me too :( ")
mainSizer.Add(myPanelA, 1, wx.EXPAND, 5)
mainSizer.Add(myPanelB, 1, wx.EXPAND, 5)
self.SetSizer(mainSizer)
pub.subscribe(self.updatePanelA, 'Update-panelA')
pub.subscribe(self.updatePanelB, 'Update-panelB')
def updatePanelA(self, message):
self.myTextA.SetLabel(message)
def updatePanelB(self, message):
self.myTextB.SetLabel(message)
class threadA(Thread):
def __init__(self):
Thread.__init__(self)
self.start()
def run(self):
ObjA = updateGUI()
ObjA.methodA()
class threadB(Thread):
def __init__(self):
Thread.__init__(self)
self.start()
def run(self):
ObjB = updateGUI()
ObjB.methodB()
class updateGUI():
def methodA(self):
while True:
time.sleep(3)
wx.CallAfter(pub.sendMessage, 'Update-panelA', message='Problem solved')
def methodB(self):
while True:
time.sleep(5)
wx.CallAfter(pub.sendMessage, 'Update-panelB', message='Mine too')
if __name__=='__main__':
app = wx.App()
frame = GUI(parent=None, id=-1, title="Problem Demo")
frame.Show()
threadA()
threadB()
app.MainLoop()
感謝您抽出時間!在
總結
以上是生活随笔為你收集整理的wxpython 多线程_在wxPython中使用线程连续更新GUI的好方法?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鬼谷子的徒弟有哪些(历史上鬼谷子的徒弟哪
- 下一篇: 红米note9参数(红米note9参数刷