Discussion:
WxTextCtrl and Sizer problem
Seb S
2010-07-25 06:22:25 UTC
Permalink
Hello,

I am trying to make a simple window with one panel containing a textctrl. I
had this working with absolute size for the panel/textctrl but now I am
trying to make it work with a box sizer.

The problem is the textctrl is now too short and I can't see the text when
my program starts. If I specify the size for the text control eg
size=(300,50) the text control will get a bit longer but not long enough (it
will only go to certain length no matter what I put in size).

It is probably a really obvious error but I can't seem to find it. I've
included the section with the bug in bug.py and the rest of my code in
case it's needed in gui_test.py. Also I'm pretty new at programming so any
comments on style/anything helpful are welcome :)

Thanks in advance for any help.
--
To unsubscribe, send email to wxPython-users+***@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Ray Pasco
2010-07-25 18:40:37 UTC
Permalink
import sys
import wx

#------------------------------------------------------------------------------

class RedirectText( wx.TextCtrl ) :
'''Redirects text from stdout to TextCtrl Object '''

def __init__( self, WxTextCtrl ) :
self.out = WxTextCtrl


def write( self, string ) :

string = string.strip( '\t\n\x0b\x0c\r ' )

# Can't put clear line here as if b4 clears string passed as argument, if after
self.out.WriteText(string)

#end class

#------------------------------------------------------------------------------

class MainFrame( wx.Frame ) : # Inherits wx.Frame
""" """

def __init__( self ) :

wx.Frame.__init__( self, None, -1, title='World Clock', size=(600, 250), pos=(400, 0) )

# add one wx.Panel and wx.TextCtrl

framePanel = wx.Panel( self, -1 ) # Allow panel to fill the frame

vertSizer = wx.BoxSizer( wx.VERTICAL )
vertSizer.AddSpacer( 10 )

panel_statText = wx.StaticText( framePanel, -1, 'Panel Caption' )
padding = 25 # space on all listed sides in the [ flags ] parameter
vertSizer.Add( panel_statText, 0, wx.LEFT | wx.RIGHT | wx.CENTER, padding )
vertSizer.AddSpacer( padding )

# Horizontal sizer within the vertical sizer.
horzSizer = wx.BoxSizer( wx.HORIZONTAL )

txtCtrl_statText = wx.StaticText( framePanel, -1, 'TextCtrl Caption' )
padding = 10 # space on all listed sides in the [ flags ] parameter
horzSizer.Add( txtCtrl_statText, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, padding )

# This is the text widget I'm having problems with :
self.textCtrl = wx.TextCtrl( framePanel, -1, style=wx.TE_READONLY, size=(300, 50) )

horzSizer.AddSpacer( 10 )

# EXPAND vertically on frame resize
padding = 10 # space on all listed sides in the [ flags ] parameter
horzSizer.Add( self.textCtrl, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, padding )
horzSizer.AddSpacer( 10 )

## makes PANEL use sizer and calculates min size
#self.SetSizerAndFit( horzSizer )

vertSizer.Add( horzSizer )
vertSizer.AddSpacer( 10 )
framePanel.SetSizer( vertSizer )

self.Show( True )

##Redirect stdout to RedirectText object
#self.redir = RedirectText( self.textCtrl )
#sys.stdout = self.redir

## Create timer to launch clock function
#self.time = My_Timer()
#self.time.Start( 900 )

## Bindings
#self.Bind( wx.EVT_CONTEXT_MENU, self.OnRightClick )

#end __init__

def OnRightClick( self, event ) :
"""Right-click popup menu """

menu = wx.Menu()
self.properties = wx.MenuItem( menu, wx.NewId(), 'Properties' )
self.Bind( wx.EVT_MENU, self.OnLeft, self.properties )
menu.AppendItem( self.properties )
self.PopupMenu( menu )
menu.Destroy()

def OnLeft( self, event ) :
"""create properties frame if -properties- in menu clicked """

properties_frame = Properties_Frame()

#end MainFrame class

#==============================================================================

if __name__ == '__main__' :

myApp = wx.PySimpleApp( redirect=False )
myFrame = MainFrame()
myApp.MainLoop()

#end if
Robin Dunn
2010-07-26 19:16:29 UTC
Permalink
Post by Seb S
Hello,
I am trying to make a simple window with one panel containing a
textctrl. I had this working with absolute size for the panel/textctrl
but now I am trying to make it work with a box sizer.
The problem is the textctrl is now too short and I can't see the text
when my program starts. If I specify the size for the text control eg
size=(300,50) the text control will get a bit longer but not long enough
(it will only go to certain length no matter what I put in size).
It is probably a really obvious error but I can't seem to find it. I've
included the section with the bug in bug.py and the rest of my code in
case it's needed in gui_test.py. Also I'm pretty new at programming so
any comments on style/anything helpful are welcome :)
Thanks in advance for any help.
You are putting the textctrl on the panel, but adding it to a sizer that
is assigned to the frame. Instead assign this sizer to the panel, and
if you want you can create another sizer with the panel in it and assign
that sizer to the frame.

BTW, please reread http://wiki.wxpython.org/MakingSampleApps Your
sample was not minimal and not runnable.
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
To unsubscribe, send email to wxPython-users+***@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Loading...