wxpython TextCtrl和无限循环问题

发布于 2024-10-21 05:04:12 字数 618 浏览 5 评论 0原文

由于某种原因,TextCtrl 在无限循环内时无法工作,这是我的代码:

   while 1:
        localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        i = i + 1
        #print str(i)

        serRead = ser.readline()
        serSplit = serRead.split(",")

        #this works
        print str(i)+', '+tempOrShock+', '+localtime+', '+serSplit[1]

        #this doesn't
        self.displayTextCtrl.WriteText(str(i)+', '+tempOrShock+', '+
                                        localtime+', '+serSplit[1])

这个无限 while 循环位于按钮单击事件内,我基本上在单击按钮后运行无限循环,并告诉我的 TextCtrl 连续写出内容并它不工作。但是,打印语句工作正常。知道为什么会出现这种情况吗?

TextCtrl is not working when it's inside an infinite loop for some reason, here's my code:

   while 1:
        localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        i = i + 1
        #print str(i)

        serRead = ser.readline()
        serSplit = serRead.split(",")

        #this works
        print str(i)+', '+tempOrShock+', '+localtime+', '+serSplit[1]

        #this doesn't
        self.displayTextCtrl.WriteText(str(i)+', '+tempOrShock+', '+
                                        localtime+', '+serSplit[1])

This infinite while loop is inside a button click event, I basically run an infinite loop after a button is clicked and tell my TextCtrl to continuously write stuff out and it is not working. However, the print statement works fine. Any idea why this might be the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-10-28 05:04:12

我怀疑 wxpython 在其主循环中需要进行一些处理(调度事件等)。但在您从事件处理程序返回之前,该循环无法运行。您最好设置一个计时器来定期更新文本控件。

I suspect wxpython has some processing it needs to do (dispatching events and so forth) in its main loop. But until you return from your event handler, that loop can't run. You'd probably be better off setting up a timer to update your text control periodically.

迷乱花海 2024-10-28 05:04:12

您可能应该使用 wx.Timer 而不是无限循环。另一个值得探索的选项是 wx.Yield。

You should probably use wx.Timer instead of infinite loop. Another option to explore is wx.Yield.

山田美奈子 2024-10-28 05:04:12

GUI 程序通过无限循环来工作,该循环将事件从队列中拉出并执行它们。除了用户生成的事件(例如按下按钮)之外,还有一些低级事件告诉小部件自行绘制。除非这些事件得到处理,否则即使您修改它们的属性(例如更改颜色、添加文本等),窗口也不会重绘。因为您有自己的无限循环,所以您正在阻止处理这些低级别事件。

一个简单的解决方案是,从您自己的循环中将事件从队列中取出并处理它们。由于已经有一个无限循环正在运行(“事件循环”),更好的解决方案是不要编写自己的无限循环。 wx.CallLater添加处理低级事件后要执行的工作(即:当GUI“空闲”时)

相反,使用wx.CallAfter或 效果是,每次调用 wx.CallAfter 都会成为您自己的循环的一次迭代。

GUI programs work by having an infinite loop that pulls events off of a queue and executes them. In addition to user generated events such as button presses, there are low level events that tell widgets to draw themselves. Unless these events get processed the window wont redraw even if you modify their properties (such as changing colors, adding text, etc). Because you have your own infinite loop you are preventing these low level events from being processed.

A naive solution is, from within your own loop you pull events off the queue and process them. Since there is already an infinite loop running (the "event loop") a better solution is to not write your own infinite loop. Instead, use wx.CallAfter or wx.CallLater to add work to be performed once the low level events have been processed (ie: when the GUI is "idle")

In effect, each call to wx.CallAfter becomes one iteration of your own loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文