是wx.StaticText文件线程安全?

2024-09-26 17:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我使用wx.StaticText对象作为计数器,它是线程安全的吗?例如:

class TaskFrame(wx.Frame):
    def __init__(self):
        self.Counter = wx.StaticText(MainPanel, id = -1, label = "0")

如果我GetLabelSetLabel同时在不同的线程中self.Counter,会发生什么?我会有麻烦吗?在

^{pr2}$

Tags: 对象selfidinitdefcounter计数器线程
1条回答
网友
1楼 · 发布于 2024-09-26 17:36:46

它不是线程安全的。引用这个优秀的blog post

In the wxPython world, there are three related “threadsafe” methods. If you do not use one of these three when you go to update your user interface, then you may experience weird issues. Sometimes your GUI will work just fine. Other times, it will crash Python for no apparent reason. Thus the need for the threadsafe methods: wx.PostEvent, wx.CallAfter and wx.CallLater.

简而言之,您可以在发布对象上创建接收器:

from wx.lib.pubsub import Publisher
Publisher().subscribe(update_function, 'update') # Params: callback, event name

然后从线程中使用^{}

^{pr2}$

尽管如此,如果您只想在进程之间共享变量,请使用supported way of doing this。目前,您正在滥用GUI工具箱来保存变量,而应该使用内置类型来保存变量。正确地共享这些数据,并使用GUI显示数据。在

相关问题 更多 >

    热门问题