wxpython Frame子窗口Show()之后却没有出现,代码不按照顺序执行。多线程解决

2021.11.15更新
此方法不能真正解决问题,会造成进度条卡住(虽然出现了但是因为运行了脚本会停下此进程)新的博文记录了多线程的解决方法


这两天遇到一个难题,研究了一天都没有解决,最后还是在Stack Overflow上找到了答案。

问题描述

# 初始状态
frame = GuageFrame(parent=self, id=-1, title='自动化填写', size=(400, 200))
frame.Show()

time.sleep(2)

# 完成状态
frame.gauge.SetValue(100)
frame.info.SetLabelText('完成!')

这段代码本意是让进度条从窗口显示,然后过2秒后进度条走完,文字变为“完成”。
可是实际运行效果窗口会在2秒后突然出现并变为完成状态

解决方法

使用wx.Yield(),下面是官方文档的解释

Yields control to pending messages in the event loop.
This method is a convenient wrapper for EvtLoopBase.Yield(). If the main loop is currently running, it calls this method on it. Otherwise it creates a temporary event loop and uses it instead, which can be useful to process pending messages during the program startup, before the main loop is created.
Use extreme caution when calling this function as, just as EvtLoopBase.Yield(), it can result in unexpected reentrances.

翻译一下简而言之
Yield()是用来在程序启动期间处理挂起消息,当主循环没有正在运行时创建一个临时事件循环。

frame = GuageFrame(parent=self, id=-1, title='自动化填写', size=(400, 200))
frame.Show()
wx.Yield()  # 加入这行
time.sleep(2)

# 完成状态
frame.gauge.SetValue(100)
frame.info.SetLabelText('完成!')

加入wx.Yield()后达到预期效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值