text control on toolbar

Hello,
     I am trying to figure out why code I developed under Windows
doesn't behave as expected when run under Linux. It seems that objects
such as text controls that have a toolbar as parent are not displayed.
The following code exhibits the problem. The text control is present
and correct under XP, wx 2.8.8.1, Python 2.5.2. No evidence of the text
control under Ubuntu 8.04, wx 2.8.7.1, Python2.5.2. The code is partly
generated by Boa Constructor. The code for the frame is pasted below.
Any pointers appreciated. Thanks.

Duncan

---------------------TestFrame.py-------------------
#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1TOOLBAR1,
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(624, 393), size=wx.Size(782, 408),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(782, 408))

        self.toolBar1 = wx.ToolBar(id=wxID_FRAME1TOOLBAR1, name='toolBar1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(782, 32),
              style=wx.TB_HORIZONTAL | wx.NO_BORDER)
        self.SetToolBar(self.toolBar1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.textCtrl1 = wx.TextCtrl(id=-1, name='textCtrl1',
              parent=self.toolBar1, pos=wx.Point(200, 0),
size=wx.Size(40, 20))

Hello,
     I am attempting to get an application developed under Windows
running as intended under Linux. One issue is binding an event handler
to a text control which is an attribute of a canvas (ogl.ShapeCanvas):

self.textCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

The text control is only shown when I want to edit attributes associated
with shapes drawn on the canvas. On Windows, the text control appears
to retain the focus. So any key down events invoke the handler which
can be used to e.g. remove selected shapes from the canvas when the
Delete key is pressed. On Linux the text control appears to not have
the focus unless it is shown (and given the focus by an explicit call to
SetFocus) and the handler isn't invoked. Is there a clean way of
ensuring that the text control retains focus? Any other clean,
crossplatform, way of handling events that don't propagate to higher
levels? Thanks.

Duncan

p.s. I still haven't found a solution to the problem I posted on the
15th. That relates to the same application. So if anyone knows how I
can place a text control on a toolbar (under Linux) I'd be grateful.

Duncan,

<snip>

Duncan

p.s. I still haven't found a solution to the problem I posted on the
15th. That relates to the same application. So if anyone knows how I
can place a text control on a toolbar (under Linux) I'd be grateful.

Did you look at the wxPython Demo? It shows how to do it. All you need to do is something like this:

myToolbar.AddControl( wx.TextCtrl( myToolbar, wx.ID_ANY, ""))

This worked for me under Ubuntu 8.04 Hardy Heron, wxPython 2.8.9.1, and Python 2.5.2.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Mike Driscoll wrote:

Duncan,

<snip>

Duncan

p.s. I still haven't found a solution to the problem I posted on the
15th. That relates to the same application. So if anyone knows how I
can place a text control on a toolbar (under Linux) I'd be grateful.

Did you look at the wxPython Demo? It shows how to do it. All you need
to do is something like this:

myToolbar.AddControl( wx.TextCtrl( myToolbar, wx.ID_ANY, ""))

This worked for me under Ubuntu 8.04 Hardy Heron, wxPython 2.8.9.1, and
Python 2.5.2.

Thanks! The AddControl method was what I was missing. Still not quite
the same as under Windows; it doesn't respect the 'pos' arguments. But
at least the buttons and text controls etc. are there. Cheers.

Duncan

duncan smith wrote:

Hello,
     I am trying to figure out why code I developed under Windows
doesn't behave as expected when run under Linux. It seems that objects
such as text controls that have a toolbar as parent are not displayed.
The following code exhibits the problem. The text control is present
and correct under XP, wx 2.8.8.1, Python 2.5.2. No evidence of the text
control under Ubuntu 8.04, wx 2.8.7.1, Python2.5.2. The code is partly
generated by Boa Constructor. The code for the frame is pasted below.
Any pointers appreciated. Thanks.

Duncan

---------------------TestFrame.py-------------------
#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1TOOLBAR1,
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(624, 393), size=wx.Size(782, 408),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(782, 408))

        self.toolBar1 = wx.ToolBar(id=wxID_FRAME1TOOLBAR1, name='toolBar1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(782, 32),
              style=wx.TB_HORIZONTAL | wx.NO_BORDER)
        self.SetToolBar(self.toolBar1)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.textCtrl1 = wx.TextCtrl(id=-1, name='textCtrl1',
              parent=self.toolBar1, pos=wx.Point(200, 0),
size=wx.Size(40, 20))

You need to use toolbar.AddControl to add controls to it. You also need to call toolbar.Realize after all the tools and controls have been added to it. See the toolbar sample in the demo.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

duncan smith wrote:

Hello,
     I am attempting to get an application developed under Windows
running as intended under Linux. One issue is binding an event handler
to a text control which is an attribute of a canvas (ogl.ShapeCanvas):

self.textCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

The text control is only shown when I want to edit attributes associated
with shapes drawn on the canvas. On Windows, the text control appears
to retain the focus. So any key down events invoke the handler which
can be used to e.g. remove selected shapes from the canvas when the
Delete key is pressed. On Linux the text control appears to not have
the focus unless it is shown (and given the focus by an explicit call to
SetFocus) and the handler isn't invoked. Is there a clean way of
ensuring that the text control retains focus?

Not if it is not shown.

Any other clean,
crossplatform, way of handling events that don't propagate to higher
levels? Thanks.

It sounds like what you really want to use is an AcceleratorTable. That is a way to convert specific keystrokes into menu events, as long as something in the container the accelerator table is associated with has the focus.

Duncan

p.s. I still haven't found a solution to the problem I posted on the
15th. That relates to the same application. So if anyone knows how I
can place a text control on a toolbar (under Linux) I'd be grateful.

Sorry, I missed that one. Answered now.

···

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

duncan smith wrote:

Mike Driscoll wrote:
  

Duncan,

<snip>

Duncan

p.s. I still haven't found a solution to the problem I posted on the
15th. That relates to the same application. So if anyone knows how I
can place a text control on a toolbar (under Linux) I'd be grateful.
      

Did you look at the wxPython Demo? It shows how to do it. All you need
to do is something like this:

myToolbar.AddControl( wx.TextCtrl( myToolbar, wx.ID_ANY, ""))

This worked for me under Ubuntu 8.04 Hardy Heron, wxPython 2.8.9.1, and
Python 2.5.2.

Thanks! The AddControl method was what I was missing. Still not quite
the same as under Windows; it doesn't respect the 'pos' arguments. But
at least the buttons and text controls etc. are there. Cheers.

Duncan
_____________

My guess is that is just a difference in the implementations of the TextCtrl in Windows vs. Ubuntu. I thought there was a generic text control, but I'm not finding it now. If it exists, then you could use that.

Mike

Hello,

Is possible then you explain what are thats differences?

Thanks in advance!

···

2008/10/24 Mike Driscoll mike@pythonlibrary.org

My guess is that is just a difference in the implementations of the TextCtrl in Windows vs. Ubuntu. I thought there was a generic text control, but I’m not finding it now. If it exists, then you could use that.

Mike


Saludos / Best regards

Mario Lacunza
Consultor de Sistemas - Webmaster

Email: mlacunza [AT] gmail [DOT] com
Lima - Peru

Mario,

2008/10/24 Mike Driscoll <mike@pythonlibrary.org <mailto:mike@pythonlibrary.org>>

    My guess is that is just a difference in the implementations of
    the TextCtrl in Windows vs. Ubuntu. I thought there was a generic
    text control, but I'm not finding it now. If it exists, then you
    could use that.

    Mike

Hello,

Is possible then you explain what are thats differences?

Thanks in advance!

--
Saludos / Best regards

Mario Lacunza
Consultor de Sistemas - Webmaster

Email: mlacunza [AT] gmail [DOT] com
Lima - Peru

I'm really not that technical, but I've seen Robin mention these kinds of inconsistencies in widgets across platforms. For example, some widgets on Windows uses the winforms library, whereas on Linux, the widgets are created with GTK or QT or whatever (so-to-speak). Since wxPython wraps the native implementation as much as possible, if the native textctrl doesn't have such-and-such a function, then neither will wx when it wraps the control.

This is why there are some generic controls. The generics are written in pure python, so they can do just about anything! Hopefully I didn't make that more confusing than it already is. If someone else has a more elegant explanation, please give it.

Mike

Mike Driscoll wrote:

My guess is that is just a difference in the implementations of the TextCtrl in Windows vs. Ubuntu. I thought there was a generic text control, but I'm not finding it now. If it exists, then you could use that.

In this case it's different behaviors in the toolbar widgets. On Windows it forces things to use packed positioning by the order of addition of the tools, and the widget's self set position is ignored.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

duncan smith wrote:

Hello,
     I am attempting to get an application developed under Windows
running as intended under Linux. One issue is binding an event handler
to a text control which is an attribute of a canvas (ogl.ShapeCanvas):

self.textCtrl.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

The text control is only shown when I want to edit attributes associated
with shapes drawn on the canvas. On Windows, the text control appears
to retain the focus. So any key down events invoke the handler which
can be used to e.g. remove selected shapes from the canvas when the
Delete key is pressed. On Linux the text control appears to not have
the focus unless it is shown (and given the focus by an explicit call to
SetFocus) and the handler isn't invoked. Is there a clean way of
ensuring that the text control retains focus?

Not if it is not shown.

Any other clean,
crossplatform, way of handling events that don't propagate to higher
levels? Thanks.

It sounds like what you really want to use is an AcceleratorTable. That
is a way to convert specific keystrokes into menu events, as long as
something in the container the accelerator table is associated with has
the focus.

I'm struggling to get the accelerator table solution working. I've
tried adding the following lines to the __init__ method of the canvas class,

        X = wx.MenuItem()
        self.Bind(wx.EVT_MENU, self.Handler, id=X.GetId())
        a_table = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_DELETE,
X.GetId())])
        self.SetAcceleratorTable(a_table)

and added a basic handler for testing purposes,

    def Handler(self, event):
        print 'success'

The above handler is only called when the text control has the focus.
It seems that the wx.EVT_KEY_DOWN event is propagated to the accelerator
table, because if I comment out the event.Skip() line in the OnKeyDown
method 'success' isn't printed. Quite confusing.

Duncan

Hello,
     I want a canvas that I can add shapes to and that can be edited by,
for instance, deleting selected shapes when the delete key is pressed.
No problem on Windows; I add a text control and hide it, it retains
focus, and it captures mouse events. On Linux that doesn't work. I've
tried AcceleratorTables and delved into the ogl source code to forage
for clues. But I can't find a working solution, or any pertinent
examples on the web. Binding to the text control doesn't work, binding
to the canvas doesn't work, the accelerator table is only invoked when
the text control has focus. When the text control doesn't have focus,
I'm not sure what does. It has been several days now, on and off.
Maybe there's some other widget I could create and hide which would
retain focus on Windows and Linux? Any pointers appreciated. I can't
think of anything I haven't already tried.

Duncan

duncan smith wrote:

I'm struggling to get the accelerator table solution working. I've
tried adding the following lines to the __init__ method of the canvas class,

        X = wx.MenuItem()
        self.Bind(wx.EVT_MENU, self.Handler, id=X.GetId())
        a_table = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_DELETE,
X.GetId())])
        self.SetAcceleratorTable(a_table)

No need to use a wx.MenuItem here, just get an ID with wx.NewId() and use that.

and added a basic handler for testing purposes,

    def Handler(self, event):
        print 'success'

The above handler is only called when the text control has the focus.
It seems that the wx.EVT_KEY_DOWN event is propagated to the accelerator
table, because if I comment out the event.Skip() line in the OnKeyDown
method 'success' isn't printed. Quite confusing.

Yes, you've pretty much figured it out. Either the window with the accelerator table or something contained within that window needs to have the focus, and that item can usually steal the accelerator key with a EVT_KEY_DOWN event.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

duncan smith wrote:

Hello,
     I want a canvas that I can add shapes to and that can be edited by,
for instance, deleting selected shapes when the delete key is pressed.
No problem on Windows; I add a text control and hide it, it retains
focus, and it captures mouse events. On Linux that doesn't work. I've
tried AcceleratorTables and delved into the ogl source code to forage
for clues. But I can't find a working solution, or any pertinent
examples on the web. Binding to the text control doesn't work, binding
to the canvas doesn't work, the accelerator table is only invoked when
the text control has focus. When the text control doesn't have focus,
I'm not sure what does. It has been several days now, on and off.
Maybe there's some other widget I could create and hide which would
retain focus on Windows and Linux? Any pointers appreciated. I can't
think of anything I haven't already tried.

Have you tried forcing the canvas to have the focus? I would do it for every EVT_LEFT_DOWN that it gets, like this:

  def OnLeftDown(self, evt):
    self.SetFocus()
    evt.Skip()

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Duncan,

I too am using wx.lib.ogl on a project. Could you suggest me any tutorials/books
for reference for ogl ?

Thanks.

Lokesh Batra

Quoting Robin Dunn <robin@alldunn.com>:

···

duncan smith wrote:
> Hello,
> I want a canvas that I can add shapes to and that can be edited by,
> for instance, deleting selected shapes when the delete key is pressed.
> No problem on Windows; I add a text control and hide it, it retains
> focus, and it captures mouse events. On Linux that doesn't work. I've
> tried AcceleratorTables and delved into the ogl source code to forage
> for clues. But I can't find a working solution, or any pertinent
> examples on the web. Binding to the text control doesn't work, binding
> to the canvas doesn't work, the accelerator table is only invoked when
> the text control has focus. When the text control doesn't have focus,
> I'm not sure what does. It has been several days now, on and off.
> Maybe there's some other widget I could create and hide which would
> retain focus on Windows and Linux? Any pointers appreciated. I can't
> think of anything I haven't already tried.

Have you tried forcing the canvas to have the focus? I would do it for
every EVT_LEFT_DOWN that it gets, like this:

  def OnLeftDown(self, evt):
    self.SetFocus()
    evt.Skip()

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Hello,
     I have developed an application that seems to work fine on Ubuntu
and XP, but not on Vista (Python 2.5.2 and recent versions of wxPython).
It has been reported to me (I don't have a Vista machine to check) that
file dialogs appear twice. The initial dialog is closed, then a second
identical dialog appears immediately. The dialogs are invoked in
handlers responding to menu events. I have had a good search and can't
find anyone else with similar issues. Does anyone have any idea what
the issue might be? Any pointers would be appreciated. Trying to solve
this without a Vista box is a bit awkward. Cheers.

Duncan

Hello,

···

On Tue, Nov 25, 2008 at 1:48 PM, duncan smith buzzard@contactbox.co.uk wrote:

Hello,

 I have developed an application that seems to work fine on Ubuntu

and XP, but not on Vista (Python 2.5.2 and recent versions of wxPython).

It has been reported to me (I don’t have a Vista machine to check) that

file dialogs appear twice. The initial dialog is closed, then a second

identical dialog appears immediately. The dialogs are invoked in

handlers responding to menu events. I have had a good search and can’t

find anyone else with similar issues. Does anyone have any idea what

the issue might be? Any pointers would be appreciated. Trying to solve

this without a Vista box is a bit awkward. Cheers.

In your event handler are you calling event.Skip()? I have seen in the past that sometimes calling event.Skip() when you don’t need to can cause an event handler to get called more than once.

Cody

Duncan,

duncan smith wrote:

Hello,
     I have developed an application that seems to work fine on Ubuntu
and XP, but not on Vista (Python 2.5.2 and recent versions of wxPython).
It has been reported to me (I don't have a Vista machine to check) that
file dialogs appear twice. The initial dialog is closed, then a second
identical dialog appears immediately. The dialogs are invoked in
handlers responding to menu events. I have had a good search and can't
find anyone else with similar issues. Does anyone have any idea what
the issue might be? Any pointers would be appreciated. Trying to solve
this without a Vista box is a bit awkward. Cheers.
  

Can you provide some small app which uses the same code as your app and which works fine for you. I can then try it on my Vista machine (Py 2.5, wxPython 2.8.9

Werner

Cody Precord wrote:

Hello,

    Hello,
        I have developed an application that seems to work fine on Ubuntu
    and XP, but not on Vista (Python 2.5.2 and recent versions of wxPython).
     It has been reported to me (I don't have a Vista machine to check) that
    file dialogs appear twice. The initial dialog is closed, then a second
    identical dialog appears immediately. The dialogs are invoked in
    handlers responding to menu events. I have had a good search and can't
    find anyone else with similar issues. Does anyone have any idea what
    the issue might be? Any pointers would be appreciated. Trying to solve
    this without a Vista box is a bit awkward. Cheers.

In your event handler are you calling event.Skip()? I have seen in the
past that sometimes calling event.Skip() when you don't need to can
cause an event handler to get called more than once.

Possibly. I use Boa Constructor, so event.Skip() gets inserted
automatically and I generally leave it. It occurred to me immediately
after I posted (doesn't it always?) that this might be the problem so I
removed it from my latest version; but I won't know until I get feedback
from the Vista user tomorrow. In other code I've had a handler being
called 3 times, rather than once. That was fixed by removing
event.Skip(), so I should probably have thought of it earlier. I'd be
interested to know how this happens though. It doesn't quite square
with what I (mis-)understand about wxPython event propagation. Thanks.

Duncan

···

On Tue, Nov 25, 2008 at 1:48 PM, duncan smith <buzzard@contactbox.co.uk > <mailto:buzzard@contactbox.co.uk>> wrote:

Werner F. Bruhin wrote:

Duncan,

duncan smith wrote:

Hello,
     I have developed an application that seems to work fine on Ubuntu
and XP, but not on Vista (Python 2.5.2 and recent versions of wxPython).
It has been reported to me (I don't have a Vista machine to check) that
file dialogs appear twice. The initial dialog is closed, then a second
identical dialog appears immediately. The dialogs are invoked in
handlers responding to menu events. I have had a good search and can't
find anyone else with similar issues. Does anyone have any idea what
the issue might be? Any pointers would be appreciated. Trying to solve
this without a Vista box is a bit awkward. Cheers.
  

Can you provide some small app which uses the same code as your app and
which works fine for you. I can then try it on my Vista machine (Py
2.5, wxPython 2.8.9

Thanks. Will do if it turns out not to be the call to event.Skip().

Duncan