在MainLoop之前的wxPython [英] wxPython before MainLoop

查看:73
本文介绍了在MainLoop之前的wxPython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在开始主循环之前刷新显示屏。


我有这样的代码:


app = App()

app.Show()

app.long_slow_init()

app.MainLoop()

主框架在Show处部分加载,但因为mainloop尚未启动
,所以在long_slow_init()完成之前显示不会更新。


或者,我可以编码


app = App()

app.long_slow_init()

app.Show()

app.MainLoop()


这会给我一个清晰的节目,但在应用显示之前会有一个漫长的缓慢等待

任何活动都没有。我需要一个闪屏。


我宁愿没有闪屏(我也不知道怎么回事)。我想

喜欢在运行之前让app.Show()正确完成

long_slow_init。


有没有在我运行long_slow_init()之前,我可以使用wx内部方法为Windows提供

完成绘制框架的机会?


或者是否有更好的主意?


(大卫)

解决方案

8月8日晚上11点25分,[david] ]" < da ... @nospam.spamwrote:


我想在开始主循环之前刷新显示。

我的代码如下:


app = App()

app.Show()

app .long_slow_init()

app.MainLoop()


主框架部分加载Show,但因为mainloop没有

开始了,直到long_slow_init()结束才显示。


或者,我可以编码


app = App()< br $>
app.long_slow_init()

app.Show()

app.MainLoop()


这将给我一个清晰的节目,但在应用程序显示任何活动之前,将会有一个漫长的缓慢等待

。我需要一个闪屏。


我宁愿没有闪屏(我也不知道怎么回事)。我想

喜欢在运行之前让app.Show()正确完成

long_slow_init。


有没有在我运行long_slow_init()之前,我可以使用wx内部方法为Windows提供

完成绘制框架的机会?


或者是否有更好的主意?


(david)



你可以使用一个单独的线程来执行long_slow_init():


--------------------------

import wx

导入线程

导入时间


类MyFrame(wx.Frame):

def __init __(个体经营):

wx.Frame .__ init __(self,None,-1," My Window")


panel = wx.Panel(self,-1)

button = wx.Button(panel,-1,click me,quick!,pos =(40,

40))

self .Bind(wx.EVT_BUTTON,self.onclick)


def onclick(self,event):

print" button clicked"


def receive_r esult(self,result):

print"嘿,我已经完成了那个漫长而缓慢的初始化。

print"结果是:" ;,结果

类MyApp(wx.App):

def __init __(自我):

wx.App .__ init __(self,redirect =假)

def OnInit(个体经营):

the_frame = MyFrame()

the_frame.Show()

t = MyThread(the_frame)

t.start()#calls run()在t'的班级


返回True

class MyThread(threading.Thread):

def __init __(self,a_frame):

threading.Thread .__ init __(self)


self.frame_obj = a_frame


def run(self):

self.result = self.long_slow_init()


def long_slow_init(self):

print" start long_slow_init()..."

time.sleep(6)

结果= 20.5


wx.CallAfter(self.frame_obj.receive_result,result)


app = MyApp()

app.MainLoop()

---------------- ------------


我重新组织了我的Thread类:


- ----------

class MyThread(threading.Thread):

def __init __(self,a_frame):

threading.Thread .__ init __(self)

self.frame_obj = a_frame


def run(self):

result = self .long_slow_init()

wx.CallAfter(self.frame_obj.receive_result,result)

#CallAfter()使用指定的

参数

#当执行下一次暂停执行时。


def long_slow_init(self):

print" start long_slow_init()..."

time.sleep(6)

result = 20.5

返回结果

--------------


[david]写道:
< blockquote class =post_quotes>
我想参考在我开始主循环之前重新显示。

[...]

我想在运行之前让app.Show()正确完成/>
long_slow_init。



恕我直言,这不会带来收益。如果你看到一个反应迟钝的用户

界面是否毫无意义。


还是有更好的主意吗?



根据建议,使用线程的解决方案是可行的。


问候,

Bj?


-

BOFH借口#422:


别人偷了你的IP地址,打电话给互联网侦探!


I''d like to refresh the display before I start the main loop.

I have code like this:

app = App()
app.Show()
app.long_slow_init()
app.MainLoop()
The main frame partly loads at Show, but because the mainloop has not
started yet, the display does not update until long_slow_init() finishes.

Alternatively, I could code

app = App()
app.long_slow_init()
app.Show()
app.MainLoop()

Which would give me a crisp Show, but there would be a long slow wait
before the app showed any activity at all. I would need a splash screen.

I''d rather not have a splash screen (and I don''t know how anyway). I''d
like to just make app.Show() finish correctly before running
long_slow_init.

Is there a wx internal method that I can use to give Windows the
opportunity to finish painting the frame before I run long_slow_init()?

Or is there a better idea?

(david)

解决方案

On Aug 8, 11:25 pm, "[david]" <da...@nospam.spamwrote:

I''d like to refresh the display before I start the main loop.

I have code like this:

app = App()
app.Show()
app.long_slow_init()
app.MainLoop()

The main frame partly loads at Show, but because the mainloop has not
started yet, the display does not update until long_slow_init() finishes.

Alternatively, I could code

app = App()
app.long_slow_init()
app.Show()
app.MainLoop()

Which would give me a crisp Show, but there would be a long slow wait
before the app showed any activity at all. I would need a splash screen.

I''d rather not have a splash screen (and I don''t know how anyway). I''d
like to just make app.Show() finish correctly before running
long_slow_init.

Is there a wx internal method that I can use to give Windows the
opportunity to finish painting the frame before I run long_slow_init()?

Or is there a better idea?

(david)

You can use a separate thread to execute long_slow_init():

--------------------------
import wx
import threading
import time

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "My Window")

panel = wx.Panel(self, -1)
button = wx.Button(panel, -1, "click me, quick!", pos=(40,
40))
self.Bind(wx.EVT_BUTTON, self.onclick)

def onclick(self, event):
print "button clicked"

def receive_result(self, result):
print "Hey, I''m done with that long, slow initialization."
print "The result was:", result
class MyApp(wx.App):
def __init__(self):
wx.App.__init__(self, redirect=False)
def OnInit(self):
the_frame = MyFrame()
the_frame.Show()

t = MyThread(the_frame)
t.start() #calls run() in t''s class

return True
class MyThread(threading.Thread):
def __init__(self, a_frame):
threading.Thread.__init__(self)

self.frame_obj = a_frame

def run(self):
self.result = self.long_slow_init()

def long_slow_init(self):
print "starting long_slow_init()..."
time.sleep(6)
result = 20.5

#Send result to frame:
wx.CallAfter(self.frame_obj.receive_result, result)

app = MyApp()
app.MainLoop()
----------------------------


I reorganized my Thread class a little bit:

------------
class MyThread(threading.Thread):
def __init__(self, a_frame):
threading.Thread.__init__(self)
self.frame_obj = a_frame

def run(self):
result = self.long_slow_init()

wx.CallAfter(self.frame_obj.receive_result, result)
#CallAfter() calls the specified function with the specified
argument
#when the next pause in execution occurs in this thread.

def long_slow_init(self):
print "starting long_slow_init()..."
time.sleep(6)
result = 20.5
return result
--------------


[david] wrote:

I''d like to refresh the display before I start the main loop.
[...]
I''d like to just make app.Show() finish correctly before running
long_slow_init.

IMHO, this will bring no gain. If you see an inresponsive user
interface or not is quite meaningless.

Or is there a better idea?

As suggested, a solution using threads is feasible.

Regards,
Bj?rn

--
BOFH excuse #422:

Someone else stole your IP address, call the Internet detectives!


这篇关于在MainLoop之前的wxPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆