473,399 Members | 3,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Forcing the position of scroll bars on a wxTextCtrl

Hello, this is my first post here so apologies if it's in the wrong
place, inappropriate or embarrassingly stupid - please let me know :)

My problem seems quite simple - I've redirected stdout to a wxTextCtrl,
so that any trace messages appear in a log window at the bottom of my
app. The problem is that whenever I update the text, the scrollbar
resets to the top - i.e. to the earliest log message. What I really
want it to do is reset to the bottom, so that the most recent log
messages are on screen.

Calling SetScrollPos( GetScrollRange() ) sets the scrollbar slider to
the correct position, but *doesn't update the text in the window* :(

Is there a call to tell it to update the visible text based on the new
slider position? Or is there a better way to update the slider
position, e.g. by sending the control an event?

I'm new to wxWindows and new to Python, so any help (and as much
detail!) as possible would be appreciated! Here's the class I'm using
to capture stdout:

class LogControl:
""" Simple helper to redirect stdout to a panel in the GUI """
def __init__( self, textCtrl ):
self._ctrl = textCtrl
self._log = ""
self.write( "Application Started...\n" )

def write( self, Message ):
self._log = self._log + Message
self._ctrl.SetValue( self._log )
# Force scroll bars to end of window - does not update text in
control!
self._ctrl.SetScrollPos( wx.VERTICAL,
self._ctrl.GetScrollRange( wx.VERTICAL) )

Thanks!

Nov 2 '05 #1
4 8677
Someone who's probably not really called Clans Of Intrigue wrote:
Hello, this is my first post here so apologies if it's in the wrong
place, inappropriate or embarrassingly stupid - please let me know :)
No, that's ok. The wxpython mailing list might give better answers though.
My problem seems quite simple - I've redirected stdout to a wxTextCtrl,
so that any trace messages appear in a log window at the bottom of my
app. The problem is that whenever I update the text, the scrollbar
resets to the top - i.e. to the earliest log message. What I really
want it to do is reset to the bottom, so that the most recent log
messages are on screen.


It's a few years since I coded wx, but I guess you should do
something along the lines of:

self._ctrl.ShowPosition(self._ctrl.GetLastPosition ())

Don't mess manually with the scroll bars (I think).

(I know I've gone through this once...)
Nov 2 '05 #2
Thanks, that did the trick perfectly :)

also got rid of the self._log member so the class is now just:

class LogControl:
""" Simple helper to redirect stdout to a panel in the GUI """
def __init__( self, textCtrl ):
self._ctrl = textCtrl
self.write( "Application Started..." )

def write( self, Message ):
# Add message to log and force scroll bars to end of window
self._ctrl.SetValue( self._ctrl.GetValue() + Message )
self._ctrl.ShowPosition(self._ctrl.GetLastPosition ())

Lovely :)

Nov 2 '05 #3
Hello Clans,

as a first suggestion, it is usually recommended that you post a small
*working* sample, in order to help others in understanding the problem and
also to give others the possibility to test your code. Noting that I am not
able to run your code as it is, I can just speculate one suggestion:
Calling SetScrollPos( GetScrollRange() ) sets the scrollbar slider to
the correct position, but *doesn't update the text in the window* :(


This is because SetScrollPos only affects scrollbars, not the wx.TextCtrl
itself. What happens if you add a self._ctrl.Refresh() after the
SetScrollPos() call? Something like:

class LogControl:
""" Simple helper to redirect stdout to a panel in the GUI """
def __init__( self, textCtrl ):
self._ctrl = textCtrl
self._log = ""
self.write( "Application Started...\n" )

def write( self, Message ):
self._log = self._log + Message
self._ctrl.SetValue( self._log )
# Force scroll bars to end of window - does not update text in
control!
self._ctrl.SetScrollPos(wx.VERTICAL, self._ctrl.GetScrollRange(
wx.VERTICAL))
self._ctrl.Refresh()
I also suggest that you use AppendText() instead of the bunch of calls to
write(). Something along the lines (untested code!!!):

class LogControl:
""" Simple helper to redirect stdout to a panel in the GUI """
def __init__( self, textCtrl ):
self._ctrl = textCtrl
self._log = ""
self.write( "Application Started...\n" )

def write( self, Message ):
self._log = self._log + Message
self._ctrl.AppendText( self._log )
# Force scroll bars to end of window - does not update text in control!
# self._ctrl.SetScrollPos(wx.VERTICAL, self._ctrl.GetScrollRange(
wx.VERTICAL))
# self._ctrl.Refresh()
HTH.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77
Nov 2 '05 #4
Clans Of Intrigue wrote:
Thanks, that did the trick perfectly :)

also got rid of the self._log member so the class is now just:

class LogControl:
""" Simple helper to redirect stdout to a panel in the GUI """
def __init__( self, textCtrl ):
self._ctrl = textCtrl
self.write( "Application Started..." )

def write( self, Message ):
# Add message to log and force scroll bars to end of window
self._ctrl.SetValue( self._ctrl.GetValue() + Message )
self._ctrl.ShowPosition(self._ctrl.GetLastPosition ())


But if you have 100kB text in the control, and add another 1kB log
message, it certainly seems suboptimal to use:
self._ctrl.SetValue( self._ctrl.GetValue() + Message )
Here, you first copy all the text in the control to a Python
string, build a new string with old+new text, and replace the old
text with the context of this bigger string. You should simply use
self._ctrl.AppendText( Message ). I'm not sure exactly what happens
under he hood, but I suspect that's more efficient, besides being
prettier in the source...

Thou shalt study thy libraries and strive not to reinvent them
without cause, that thy code may be short and readable and thy
days pleasant and productive.

E.g. http://www.wxpython.org/docs/api/wx.TextCtrl-class.html
Nov 3 '05 #5

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

Similar topics

12
by: Arlie Rahn | last post by:
I would like to ad a custom scroll bar control to my app that has a customizable and "flat" look to it (not the normal VB look). Does anyone have any ideas on where to find a good one?
5
by: Analysis&Solutions | last post by:
Hey Folks: I'm probably dreaming here, but perhaps there's a way... I just gave a presentation. It's in XHTML and uses CSS for making things look nice. Right now there's a small navigation...
2
by: GrantS | last post by:
I am trying to convert the VB.Net code example povided by http://authors.aspalliance.com/JimRoss/Articles/MaintainScrollPos.aspx into C# (ASP.Net)without success. No errors are thrown in the VB...
2
by: Leonid Shirmanov | last post by:
Hi, I have a form with control placed on it. Control has the image painted on its graphics. Size of the contol is greater than size of the form and scroll bars appear in the form. I scroll the...
2
by: usenet | last post by:
When I open a form design window in Access 2003 it *always* has scroll bars, this is even when the form itself is tiny. It's as if the 'page' on which the form is being designed is very large. ...
3
by: zazu | last post by:
We have a database that can be accessed by a small number of users on a small network. The database is on the server. One form has a combo box with a large number of selections. Usually there is a...
1
by: David_from_Chicago | last post by:
I am developing an application in Access 2000 (A2K) which has multiple forms and subforms. Until now, all subforms displayed scroll bars properly (according to the subform's property setting). ...
5
by: Lord Zoltar | last post by:
Hello, I'm trying to force a listView to scroll to some location when a certain button is clicked on. I've found that the SendMessage function seems to be the choice way of doing it. Here's what...
6
by: bgold12 | last post by:
I was using quirks mode (without a doctype) and set the overflow CSS property to :auto for the body tag like so: <body style="overflow:auto"> And it worked; it got rid of IE's default scroll...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.