Hola:
Te envío el código, adaptado para windows.
Debería funcionarte
Como anticipo te comento que ahora usa EVT_CHAR_HOOK
self.m_listBox1.Bind( wx.EVT_CHAR_HOOK, self.test2 )
Con esto ya puedes capturar la tecla pulsada.
# -*- coding: utf-8 -*-
import wx
import wx.xrc
classMyFrame1 ( wx.Frame ):
def__init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY,
title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size(
500,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_panel1 = wx.Panel(
self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
m_listBox1Choices = [ u"Uno", u"Dos", u"Tres" ]
self.m_listBox1 = wx.ListBox(
self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_listBox1Choices,
0 )
bSizer2.Add( self.m_listBox1,
1, wx.ALL|wx.EXPAND|wx.TE_PROCESS_ENTER, 5 )
self.m_panel1.SetSizer( bSizer2 )
self.m_panel1.Layout()
bSizer2.Fit( self.m_panel1 )
bSizer1.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_listBox1.Bind( wx.EVT_CHAR_HOOK, self.test2 )
def__del__( self ):
pass
# Virtual event handlers, overide them in your derived class
deftest2( self, event ):
print(event.KeyCode)
event.Skip()
classMyPanel1 ( wx.Panel ):
def__init__( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition,
size = wx.Size( 500,300 ), style = wx.TAB_TRAVERSAL,
name = wx.EmptyString ):
wx.Panel.__init__ ( self, parent, id = id, pos = pos,
size = size, style = style, name = name )
def__del__( self ):
pass
if__name__ == '__main__':
app = wx.PySimpleApp()
view = MyFrame1(None)
view.Show()
app.MainLoop()
Dani
O 29/08/21 ás 20:32, Antonio Cascales escribiu:
Hola de nuevo a todos:
Dani, en primer lugar y ante todo quiero darte las gracias por tu ejemplo.
En segundo lugar, pedirte disculpas, porque al ver tu respuesta me
di cuenta que cometí un error al realizar mi consulta el otro día. Y
mi error fue no especificar que utilizaba Windows, con lo que
obviamente, pese a que he probado tu código, no me ha funcionado.
Según he podido leer, en sistemas Linux, existe GTK o GTK2, que
hacen que el EVT_LISTBOX_DCLICK de WXPython pueda ejecutarse mediante
la tecla RETURN. Sin embargo, eso no ocurre en sistemas Windows, que
es el problema que me estoy encontrando yo y que trataba de
solucionar.
De todos modos, gracias de nuevo por tu ayuda.
Un saludo.
El 27/8/21, Dani<d...@damufo.eu> escribió:
Hola Antonio y lista:
Prueba esto que acabo de crear en base a tu consulta.
A mi me responde a la tecla Retorno (ubuntu 20.04 con python 3.8.10)
Ejecútalo desde un terminal
$ python3 prueba_listbox.py
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version 3.9.0 Jun 11 2020)
##http://www.wxformbuilder.org/
##
## PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
## Class MyFrame1
###########################################################################
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title =
wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,300 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition,
wx.DefaultSize, wx.TAB_TRAVERSAL )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
m_listBox1Choices = []
self.m_listBox1 = wx.ListBox( self.m_panel1, wx.ID_ANY,
wx.DefaultPosition, wx.DefaultSize, m_listBox1Choices, 0 )
bSizer2.Add( self.m_listBox1, 1, wx.ALL|wx.EXPAND, 5 )
self.m_panel1.SetSizer( bSizer2 )
self.m_panel1.Layout()
bSizer2.Fit( self.m_panel1 )
bSizer1.Add( self.m_panel1, 1, wx.EXPAND |wx.ALL, 5 )
self.SetSizer( bSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_listBox1.Bind( wx.EVT_KEY_DOWN, self.test )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def test( self, event ):
print("ddd")
event.Skip()
###########################################################################
## Class MyPanel1
###########################################################################
class MyPanel1 ( wx.Panel ):
def __init__( self, parent, id = wx.ID_ANY, pos =
wx.DefaultPosition, size = wx.Size( 500,300 ), style = wx.TAB_TRAVERSAL,
name = wx.EmptyString ):
wx.Panel.__init__ ( self, parent, id = id, pos = pos, size =
size, style = style, name = name )
def __del__( self ):
pass
if __name__ == '__main__':
app = wx.PySimpleApp()
view = MyFrame1(None)
view.Show()
app.MainLoop()
Dani
O 24/08/21 ás 17:47, Antonio Cascales escribiu:
Hola a todos:
Me presento. Mi nombre es Antonio Cascales, y aunque llevo tiempo
siguiendo la actividad de la lista, es la primera vez que escribo a
ella.
Os escribo porque quisiera haceros una consulta relativa a Python y
la librería WXPython.
Estoy aprendiendo a utilizarla y a diseñar interfaces gráficas con
ella, y me estoy enfrentando a un problema que no entiendo, ni sé
porqué ocurre.
Estoy intentando añadir un evento EVT_KEY_DOWN a un ListBox, pero
me encuentro con que pese a que lo vinculo bien con el método, no
consigo que reaccione a la tecla RETURN cuando la pulso.
Sin embargo, si capturo la tecla espacio, sí funciona.
Sé que los ListBox tienen sus propios eventos, y que el más
adecuado a lo que yo necesito es el EVT_LIST_BOX_DCLICK, pero necesito
obligatoriamente que sea mediante teclado. De ahí intentarlo con
EVT_KEY_DOWN.
¿Alguno podríais indicarme porqué ocurre esto y si hay forma de
solucionarlo?
Estoy tratando de hacer un pequeño programa que contenga una lista
de elementos, y que escriba en un área de texto una cadena al pulsar
INTRO encima de uno de sus elementos.
Si alguien puede arrojar luz sobre esta cuestión, se lo
agradecería. Y disculpad si algo no queda claro, pero mis
conocimientos son limitados en la materia.
Un saludo,
_______________________________________________
Python-es mailing list
Python-es@python.org
https://mail.python.org/mailman/listinfo/python-es
_______________________________________________
Python-es mailing list
Python-es@python.org
https://mail.python.org/mailman/listinfo/python-es