Problems with GenericDirCtrl events

Hello there...

This is a copy of a post I made somewhere else a few hours ago, and I'd like
to see your opinions too. I've searched in this forum, and found interesting
things about the GenericDirCtrl, but couldn't solve my problem. I hope this
is not a repeated post. Here we go:

-----------ORIGINAL POST(edited a little though)-------------:

- [b]Introducing[/b]:

I’m very new with both Python and wxPython. Actually I’m new with
programming :D, started just two years ago with C, and Python a month ago. I
don’t usually ask for help, but I feel that I’m… how’s the phrase… “between
a rock and a hard place”? Well, I’ve been asked by some astrophysics
engineers to make a GUI some time ago. I already have a 5,000 lines program
in Python, ready to roll, but now they want to execute that program with an
user-friendly interface. And I’m finding it really difficult to learn… I
mean programming a GUI. The GUI program is still a WIP, so don’t worry about
the looking :P.

- [b]Problem[/b] (code in wxPython):

···

---
Some initial data:

OS: Ubuntu server.
Python version: I don’t know. I downloaded the last repository version with
apt-get, and I’m using #!/usr/bin/python, so I guess it’s 2.7.1.
GUI toolkit: wxPython?
---

The main problem is I have some events not working. I want that when I pick
a file or a directory from the tree control (with double click or just
selecting, doesn’t matter), an event triggers and the path is copied and
pasted on the text control. It seems like the EVT_TREE_SEL_CHANGED and
EVT_TREE_ITEM_ACTIVATED events don’t work for some reason, they don’t
trigger. I’ve been searching the web and trying the wxPython tutorials, but
couldn’t find anything to solve this issue. If you know where I could find
some help about this, or you want to give me a hint, please tell me.

I also have a minor problem with the help button which triggers a dialog
screen. I don’t know why, but everything (Ok button and static texts) appear
in the top left corner of the panel, despite the sizers. I changed the
positions many times with no avail. It's really scratching my head lol.

I’ve asked my colleagues to help me out, but they don’t know where's the
problem either. And they are too busy to look deep into it, so… I have no
choice but to post my problem in a forum. First time I do it, so please
don’t be bad lol.

I hope you pardon this annoying newbie :wink: ,

Alex.

[b]Screenshots and Code[/b]:

--------------Here’s the interface:

[img]http://i285.photobucket.com/albums/ll44/AlexNavarroW/parsershot1.png[/img]

--------------Here’s the help panel:

[img]http://i285.photobucket.com/albums/ll44/AlexNavarroW/helpshot1.png[/img]

--------------Here’s the [b]GUI program[/b] (WIP, thus some things don’t
work yet):

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
try:
  import wx
  import os
except ImportError:
  raise ImportError,"The wxPython module is required to run this program"
class MessageDialog(wx.Dialog):
  def __init__(self, parent, id, title):
    wx.Dialog.__init__(self, parent, id, "Ayuda", size=(300, 300))
    #textoayuda=wx.TextCtrl(self, label='Ayuda:')
    texto1='''The program is divided into three stages.'''
    texto2='''\n\nFirst stage, selecting the measurements file. \nSecond
stage, selecting or creating the destination file. \nThird, executing the
program by pressing the Parse File button.'''
    texto3='''\n\nYou can use the file-directory tree browsers to select the
measurements file, and the directory where you want to create the
destination file, if it is not created yet.)'''
    sizer=wx.GridBagSizer()
    st1=wx.StaticText(self, -1, texto1, style=wx.ALIGN_LEFT)
    st2=wx.StaticText(self, -1, texto2, style=wx.ALIGN_LEFT)
    st3=wx.StaticText(self, -1, texto3, style=wx.ALIGN_LEFT)
    sizer.Add(st1, pos=(0,0), flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=15)
    sizer.Add(st2, pos=(4,0), flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=15)
    sizer.Add(st3, pos=(8,0), flag=wx.EXPAND|wx.TOP|wx.BOTTOM, border=15)
    button4=wx.Button(self, label='Ok')
    sizer.Add(button4,pos=(12,4), flag=wx.BOTTOM|wx.EXPAND)
    self.Centre()
    self.Show(True)
    self.Bind(wx.EVT_BUTTON, self.CloseHelp)
    self.SetSizer(sizer)
  def CloseHelp(self, event):
    self.Destroy()
class Main(wx.Frame):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(1100,600))
    self.parent=parent
    self.initialize()
    self.Centre()
    self.Show(True)
  def initialize(self):
    sizer=wx.GridBagSizer()
    text1=wx.StaticText(self, label='Measurements File:')
    sizer.Add(text1, pos=(0,1), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=10)
    self.entry=wx.TextCtrl(self,-1,value=u"File path and name... Pick it down
there.")
    sizer.Add(self.entry,(1,1),(1,30),wx.EXPAND)
    self.dir=wx.GenericDirCtrl(self, -1, dir='/home')
    sizer.Add(self.dir,(3,1),(20,30),wx.EXPAND)
    text2=wx.StaticText(self, label='Destination File:')
    sizer.Add(text2, pos=(0,40), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=10)
    self.entry2=wx.TextCtrl(self,-1,value=u"File path and name... Pick a file
or create a new one after choosing the directory.")
    sizer.Add(self.entry2,(1,38),(1,34),wx.EXPAND)
    self.dir2=wx.GenericDirCtrl(self, -1, dir='/home')
    sizer.Add(self.dir2,(3,38),(20,34),wx.EXPAND)
    #Botones
    button1=wx.Button(self, label='Parse File')
    sizer.Add(button1,(26,31),(1,6), flag=wx.BOTTOM, border=6)
    button2=wx.Button(self, label='Cancel')
    sizer.Add(button2,(26,24),(1,4), flag=wx.BOTTOM, border=4)
    button3=wx.Button(self, label='Help')
    sizer.Add(button3,(26,39),(1,4), flag=wx.BOTTOM, border=4)
    #self.SetSizerAndFit(sizer)
    self.SetSizer(sizer)
    #Eventos
    self.Bind(wx.EVT_BUTTON, self.Cerrar, id=button2.GetId())
    self.Bind(wx.EVT_BUTTON, self.OnHelp, id=button3.GetId())
    self.Bind(wx.EVT_BUTTON, self.Parser, id=button1.GetId())
    #These are the problematic events:
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, id=self.dir.GetId())
    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated2,
id=self.dir2.GetId())
    self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnActivated, id=self.dir.GetId())
    self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnActivated2,
id=self.dir2.GetId())
  def Cerrar(self, event):
    self.Close()
  def Parser(self, event):
    os.system("/home/alex/programas/parser.py")
  def OnHelp(self, event):
    dlg=MessageDialog(None, -1, "")
    dlg.ShowModal()
  def OnActivated(self, event):#This is the problematic def…
    origen=self.dir.GetPath()
    TextCtrlValue(self.entry, origen)
    #self.entry.SetValue(origen)
    #origen.Destroy()
    origen=os.listdir(self.dir.GetPath())
    origen=self.GetItemText(event.GetItem(self.dir))
    SetValue(self.entry, origen)
    event.Skip()
  def OnActivated2(self, event):#Ignore this one. Same as OnActivated.
    destino=os.listdir(self.dir.GetPath())
    destino=self.GetItemText(event.GetItem())
    #SetValue(self.entry2, destino)
    event.Skip()
if __name__=="__main__":
  app=wx.App()
  frame=Main(None,-1,"Parser Interface...")
  #MessageDialog(None,-1,"Diálogo de ayuda...")
  app.MainLoop()

You have my thanks in advance.

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Problems-with-GenericDirCtrl-events-tp4425502p4425502.html
Sent from the wxPython-users mailing list archive at Nabble.com.

AlexNW25 wrote:

The main problem is I have some events not working. I want that when I pick
a file or a directory from the tree control (with double click or just
selecting, doesn�t matter), an event triggers and the path is copied and
pasted on the text control. It seems like the EVT_TREE_SEL_CHANGED and
EVT_TREE_ITEM_ACTIVATED events don�t work for some reason, they don�t
trigger. I�ve been searching the web and trying the wxPython tutorials, but
couldn�t find anything to solve this issue. If you know where I could find
some help about this, or you want to give me a hint, please tell me.

The GenericDirCtrl is a composite control, consisting of several smaller
controls. The key concept here is that you need to bind to the tree
control it contains, not the DirCtrl itself. So:

    #These are the problematic events:
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated, id=self.dir.GetId())
    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated2,
id=self.dir2.GetId())
    self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnActivated, id=self.dir.GetId())
    self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnActivated2,
id=self.dir2.GetId())

Do this:

    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
self.dir.TreeCtrl )
    self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
self.dir2.TreeCtrl )
    self.Bind(wx.EVT_TREE_ITEM_SEL_CHANGED, self.OnActivated,
self.dir.TreeCtrl )
    self.Bind(wx.EVT_TREE_ITEM_SEL_CHANGED, self.OnActivated,
self.dir2.TreeCtrl )

Note the use of the control itself instead of its ID. That's generally
more convenient.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thank you, Roberts. That definitively helped .
And thank you for the IDs tip.

···

--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/Problems-with-GenericDirCtrl-events-tp4425502p4427771.html
Sent from the wxPython-users mailing list archive at Nabble.com.