473,811 Members | 3,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython before MainLoop

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_i nit()
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_i nit()
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)
Aug 9 '07 #1
19 3559
On Aug 8, 11:25 pm, "[david]" <da...@nospam.s pamwrote:
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_i nit()
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_i nit()
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.Fram e):
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.EV T_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_fr ame)
t.start() #calls run() in t's class

return True
class MyThread(thread ing.Thread):
def __init__(self, a_frame):
threading.Threa d.__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(se lf.frame_obj.re ceive_result, result)

app = MyApp()
app.MainLoop()
----------------------------
Aug 9 '07 #2
I reorganized my Thread class a little bit:

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

def run(self):
result = self.long_slow_ init()

wx.CallAfter(se lf.frame_obj.re ceive_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
--------------

Aug 9 '07 #3
[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!

Aug 9 '07 #4
On Aug 9, 12:25 am, "[david]" <da...@nospam.s pamwrote:
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_i nit()
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_i nit()
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)
Yeah, I think 7stud's thread is the way to go. It's what I do with
long running tasks, see also:
http://wiki.wxpython.org/LongRunningTasks

If your screen doesn't load correctly, be sure to call the Layout()
method.

Mike

Aug 9 '07 #5
On Aug 8, 11:25 pm, "[david]" <da...@nospam.s pamwrote:
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_i nit()
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_i nit()
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)
I don't see my original post, so here it is again....
You can use another thread to execute long_slow_init( ):

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

class MyFrame(wx.Fram e):
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.EV T_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): #called by wx.Python
the_frame = MyFrame()
the_frame.Show( )

t = MyThread(the_fr ame)
t.start() #calls t.run()

return True

class MyThread(thread ing.Thread):
def __init__(self, a_frame):
threading.Threa d.__init__(self )
self.frame_obj = a_frame

def run(self):
result = self.long_slow_ init()

wx.CallAfter(se lf.frame_obj.re ceive_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
app = MyApp()
app.MainLoop()

Aug 9 '07 #6
I'm disappointed that I didn't get a wxPython solution.

If the only way to get wxPython to correctly handle
this simple task is to code around it, I don't think
wxPython is really ready for Windows.

Is there a better place to ask?

Regarding the suggestions:

Bjoern, you're wrong. The GUI needs to be displayed
for the user to analyse. A delay between display and
readiness is much better than a delay before display
or a delay with the GUI half-drawn.

Mike, the screen does display correctly, it's just
that in Windows, screen updates are not processed
while the application is busy.

7Stud, that's a solution. Unless anyone comes up
with a direct solution, I guess I'll have to do that.

[david]
[david] wrote:
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_i nit()
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_i nit()
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)
Aug 10 '07 #7
[david] wrote:
I'd like to refresh the display before I start the main loop.
We have this kind of situation in Chandler, where we display and update
the splash screen before we enter MainLoop.

1. Create app object
http://lxr.osafoundation.org/source/...handler.py#080

2. During app object creation, in OnInit, put up splash screen and update it

http://lxr.osafoundation.org/source/...ication.py#433

3. The splash screen refresh is basically: draw new stuff,
self.Layout(), self.Update(), wx.Yield()
http://lxr.osafoundation.org/source/...cation.py#1421

3. Start MainLoop
http://lxr.osafoundation.org/source/...handler.py#086

--
Heikki Toivonen
Aug 10 '07 #8
On Aug 9, 8:51 pm, "[david]" <da...@nospam.s pamwrote:
I'm disappointed that I didn't get a wxPython solution.

If the only way to get wxPython to correctly handle
this simple task is to code around it, I don't think
wxPython is really ready for Windows.

Is there a better place to ask?

Regarding the suggestions:

Bjoern, you're wrong. The GUI needs to be displayed
for the user to analyse. A delay between display and
readiness is much better than a delay before display
or a delay with the GUI half-drawn.

Mike, the screen does display correctly, it's just
that in Windows, screen updates are not processed
while the application is busy.

7Stud, that's a solution. Unless anyone comes up
with a direct solution, I guess I'll have to do that.

[david]

[david] wrote:
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_i nit()
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_i nit()
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)
Chris is right. The only way to interact with a gui is with a separate
thread, otherwise you're blocking the gui's mainloop thread. That's
why I gave that link. That's how to do it in every GUI I'm aware of.

Mike

Aug 10 '07 #9
[david] wrote:
I'm disappointed that I didn't get a wxPython solution.

If the only way to get wxPython to correctly handle
this simple task is to code around it,
LOL -- did you try coding this app with native windows means, like
MFC? You will have *exactly* the same problem, and *exactly* for
the same reason. The organisation of wxWidgets (and thus, wxPython)
is very near to Windows GUI coding philosophy.
I don't think wxPython is really ready for Windows.
I suggest you first went getting experience with other GUI libraries
before you make such statements.

Also, wxPython is a thin wrapper around wxWidgets C++ library which
is widely used for Windows apps. And with wxWidgets, you'd *also*
have the same problem.
Bjoern, you're wrong. The GUI needs to be displayed
for the user to analyse. A delay between display and
readiness is much better than a delay before display
or a delay with the GUI half-drawn.
This may be, but it strongly depends on the application itself.
Mike, the screen does display correctly, it's just
that in Windows, screen updates are not processed
while the application is busy.
That's the matter in just about *every* GUI framework using an event
loop. And I don't know any that doesn't. Thus, there are two widely
used standard solutions:

* use a worker thread, or

* call a "process all pending events now" function repeatedly during
the work (here: wx.Yield, wx.SafeYield, wx.YieldIfNeede d).

Regards,
Björn

--
BOFH excuse #92:

Stale file handle (next time use Tupperware(tm)! )

Aug 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2548
by: Anand | last post by:
I am calling a python script from LabVIEW. This is achieved by making a dll call to python22.dll. It works perfectly well for most of my code. I now want to throwup dialog boxes from python. It works just fine if i run the code once. but when i call the same piece of code the second time, wxPython.wx.miscc does not exist for some reason. I dont know how this gets deleted. I guess this happens during some kind of clean up. The code that...
0
1968
by: Richard Townsend | last post by:
I've been experimenting with passing a window handle from a wxPython app to a Tkinter app. The Tkinter app should embed a Toplevel window containing a Canvas widget in the wxPython app's Frame (see example below). Both apps run, but the Tkinter app's output doesn't appear. Can anyone see why this isn't working? Using wxPython-2.5.1/Python 2.3.3/Win98 and Win2000.
2
1681
by: flupke | last post by:
Hi, i'm developing an app in wxPython but when i have an error in the wxPython code i've written, the app starts, an extra windows is opened where the error messages are print and then it closes again. Off course, this is to fast for the human eye to read :) How can i avoid this and have the wxPython messages arrive at the console just like the "normal" python error messages? Thanks,
0
1687
by: Sven Tissot | last post by:
Hello, I am trying to build an editable ListCtrl_edit via TextEditMixin. It displays o.k. and I can edit the first field with this is the code piece: class VokabelListCtrl(wxListCtrl, listmix.ListCtrlAutoWidthMixin, listmix.TextEditMixin): def __init__(self, parent, ID, pos=wxDefaultPosition,
2
1715
by: rodmc | last post by:
I am totally new to Python and WxPython and need to write an application which can open up an external windows from a plug-in within GAIM (using pyGAIM). I have managed to hack some code together from what little I have covered so far, however GAIM refuses to open until I have closed the extra window. I appreciate this is probably a simple point but I would be grateful for any advice people can offer on how I can make them both appear so...
3
3219
by: John Salerno | last post by:
I'm using the sample code of the file 'simple.py' and trying to make a single window with a panel in it, but I keep getting an error. Here's my code: (I know I might need something else, like a Show() method for the panel, but the error stops on the first panel line anyway. I've tried a Layout() method but it didn't get that far). import wx class MyFrame(wx.Frame):
9
5559
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame. But the "app.MainLoop()" in wxpython looks like conflicting with the "while 1:" in socket server. After I commented the "app.MainLoop()", everything is working except two things: 1. if I click anywhere on the screen with the mouse, the image is...
9
4454
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the "Run" button, the GUI would close, and the 12 input variables would then be available for the rest of the program. So far, what I have been able to do is mostly a reverse engineering job to get the frame to look right and return the text variable...
4
1879
by: Marcpp | last post by:
Hi I need to call a widget from a button in WXPYTHON. I've tried to this from a function like this, but when push the button, the program opens a window and do error. Any idea? ...... def DialogRRHH(self,event): prog = wx.PySimpleApp(0) wx.InitAllImageHandlers() DialogRRHH = MTRRHH(None, -1, "")
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.