473,856 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wxPython: TextCtrl delayed update when using TE_RICH(2)

I made a small wxPython app that retrieves web data; for visual
logging I use a TextCtrl widget, and stdout is redirected to it,
something like this:

class RedirectOutput:
def __init__(self, objectTxtCtrl):
self.out = objectTxtCtrl

def write(self, string):
self.out.WriteT ext(string)

[...]

messages = wx.TextCtrl(pan el, -1, "", size = (-1, 200), style =
wx.TE_MULTILINE | wx.TE_RICH, name = "messages")

myout = RedirectOutput( messages)
sys.stdout = myout
The web query is inside a function (def Execute), binded to a button.
To simplify the story, consider the function looks like this:

def Execute(self, evt):
print "Start query"
time.sleep(5)

The "Start query" message should show in the *messages* box when I
press the button. Instead, it shows only after the time.sleep(5)
delay.

If I don't use the wx.TE_RICH / wx.TE_RICH2 style on *messages*, the
text shows before the time.sleep(5)

I want to use wx.TE_RICH / wx.TE_RICH2 because of the 64k limitation
of the standard TextCtrl.

I'm using python 2.4.3 and wxpython 2.8.1.1 unicode, on WinXP SP2.
Windows extensions are also installed.

Feb 3 '07 #1
5 2871
Hi,
def Execute(self, evt):
print "Start query"
time.sleep(5)

The "Start query" message should show in the *messages* box when I
press the button. Instead, it shows only after the time.sleep(5)
delay.

If I don't use the wx.TE_RICH / wx.TE_RICH2 style on *messages*, the
text shows before the time.sleep(5)
For this kind of stuff, I'd try to put "self.out.Write Text(string)" in
some 'Idle' event, which avoid to fall in focus loops or other objects
events management problems not easy to solve.

Did you try something like that :

def write(self, string):
self.outBuffer= string
def onIdle(self,eve nt):
if self.outBuffer != None:
self.out.WriteT ext(self.outBuf fer)
self.outBuffer= None
Feb 4 '07 #2
On 2/4/07, jean-michel bain-cornu <py********@nos pam.jmbc.frwrot e:
Hi,
def Execute(self, evt):
print "Start query"
time.sleep(5)

The "Start query" message should show in the *messages* box when I
press the button. Instead, it shows only after the time.sleep(5)
delay.

If I don't use the wx.TE_RICH / wx.TE_RICH2 style on *messages*, the
text shows before the time.sleep(5)

For this kind of stuff, I'd try to put "self.out.Write Text(string)" in
some 'Idle' event, which avoid to fall in focus loops or other objects
events management problems not easy to solve.
This doesn't have anything to do with focus loops or otherwise, it's
because the OP isn't familiar with event based programming.

You're performing a long-running task which is preventing the event
loop from processing, so your text isn't updating and your application
is unresponsive. You need to rewrite your task - either do everything
asynchronously, or use a threaded approach. If you use the thread
approach, be sure to not call the updates directly, you can use the
wx.CallAfter mechanism to call gui functions in a threadsafe manner.

There is a lot of information about this on the wxPython wiki and in
the archives of the wxpython-users ML.
Feb 5 '07 #3
>For this kind of stuff, I'd try to put "self.out.Write Text(string)" in
>some 'Idle' event, which avoid to fall in focus loops or other objects
events management problems not easy to solve.

This doesn't have anything to do with focus loops or otherwise, it's
because the OP isn't familiar with event based programming.

You're performing a long-running task which is preventing the event
loop from processing, so your text isn't updating and your application
is unresponsive. You need to rewrite your task - either do everything
asynchronously, or use a threaded approach. If you use the thread
approach, be sure to not call the updates directly, you can use the
wx.CallAfter mechanism to call gui functions in a threadsafe manner.
So it is an event management problem.
The event loop is not yet finished when the program want to display
something, potentially initiating a new event loop.

If you don't want to bother with threads, the idle event approach is not
so bad. Put something to display in a buffer, and display it only one
time the gui have nothing else to do.
I use it every time I can, and it's very safe and easy to do.
Furthermore, you can still step into the program with a debugger, which
can be tricky if the program uses threads (I'd say impossible, but I
didn't try in fact).

Regards
jm
Feb 5 '07 #4
On 2/5/07, jean-michel bain-cornu <py********@nos pam.jmbc.frwrot e:
For this kind of stuff, I'd try to put "self.out.Write Text(string)" in
some 'Idle' event, which avoid to fall in focus loops or other objects
events management problems not easy to solve.
This doesn't have anything to do with focus loops or otherwise, it's
because the OP isn't familiar with event based programming.

You're performing a long-running task which is preventing the event
loop from processing, so your text isn't updating and your application
is unresponsive. You need to rewrite your task - either do everything
asynchronously, or use a threaded approach. If you use the thread
approach, be sure to not call the updates directly, you can use the
wx.CallAfter mechanism to call gui functions in a threadsafe manner.

So it is an event management problem.
The event loop is not yet finished when the program want to display
something, potentially initiating a new event loop.
This is almost totally wrong. There is no "new event loop" involved.
The OP is running a long task in the main thread, which is blocking
the event loop. When the event loop is blocked, the application will
not update and cannot be interacted with. It's that simple. The event
loop in a gui application doesn't "finish" until the application
exits.
If you don't want to bother with threads, the idle event approach is not
so bad. Put something to display in a buffer, and display it only one
time the gui have nothing else to do.
The problem is not putting the text into the control - the OP is
mislead by the symptoms. The problem is the task he's perform in
addition to the logging, which is a long running task (and in his
sample code is represented by time.sleep). The logging issue is a red
herring, the real problem is the way he's structured his application,
with work blocking the event loop. Until he changes this, nothing
about the way he writes to his log is going to fix the problem.
I use it every time I can, and it's very safe and easy to do.
Furthermore, you can still step into the program with a debugger, which
can be tricky if the program uses threads (I'd say impossible, but I
didn't try in fact).
Using idle events for continual calculation actually has several
caveats you need to be aware of, and I don't recommend it. A
background thread or a timer is generally a better solution.
Feb 5 '07 #5
>
This is almost totally wrong. There is no "new event loop" involved.
The OP is running a long task in the main thread, which is blocking
the event loop. When the event loop is blocked, the application will
not update and cannot be interacted with. It's that simple. The event
loop in a gui application doesn't "finish" until the application
exits.
I appreciate the fact that it is not *completely* wrong...
Try to get out of your point of view, maybe you'll find something
interesting. Maybe the 'true' part of mine.

Regards
jm
Feb 5 '07 #6

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

Similar topics

3
3027
by: Logan | last post by:
I asked a similar question already in the wxPython mailing list, but did not get an answer up to now: 1.) When using a TextCtrl with styles (color etc.) in wxPython, is it then possible to get a 'list' (or anything) from the widget which tells you which styles are used in the widget (and where!; 'beginning of style', 'end of style', 'type of style' ...)? 2.) Is it possible to set something like a mark in a TextCtrl?
3
7556
by: Lo?c Mah? | last post by:
Hello I try to use a Validator for a TxtCtrl placed in a Panel with a Button in order to trigger the Validator and test the content of TxtCtrl. I have looked into wxPython documentation and demo and searched in google, but I am still unsuccessful yet. My problem is that I do not manage to call the Validate() method of my Validator from the button handler.
1
2091
by: sillyemperor | last post by:
I was a new guy of Python,when i want to test my wxPython app by unittest,it couldn`t work.I fund a stubmaker.py but it only for wxDialog but all widgets.Can someone can tell me how test wxPython by unittest?Thanks
0
1667
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this version, many of which are listed below and at http://wxpython.org/recentchanges.php. What is wxPython?
0
1546
by: Robin Dunn | last post by:
Announcing ---------- The 2.6.3.0 release of wxPython is now available for download at http://wxpython.org/download.php. There have been many enhancements and fixes implemented in this version, many of which are listed below and at http://wxpython.org/recentchanges.php. What is wxPython?
1
2810
by: PeterG | last post by:
Hi, I am relatively new to Python, and am learning it as part of a university module... Im currently undertaking a project to create an IM server and IM gui client. I have a very basic server, and a basic gui client. I can get my
9
4463
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...
1
4594
by: aeroumr | last post by:
In the following code, I have created a panel with a button and a textctrl object on it. I have also created a menubar that will create a new text file (i.e. textctrl object). My problem is that when I create a new file, it displays the textctrl over the top left of my existing panel. I know that the reason is because I have specified the parent as self (i.e. Frame). How do I get the new file to display in the textctrl widget on my panel? I...
4
2856
by: Jimmy | last post by:
hi, all I'm having a problem with creating custom events in wxpython. I have a class A handling some data processing work and another class B of GUI matter. I need GUI to display information when data in A is updated. I know cutom events in wxpython may work. But I found no material paricularly helpful :(
0
9920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9764
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
11063
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
10699
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...
0
10390
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
9535
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
7098
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
5762
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.