wxpython Choice key down事件将不运行

2024-04-20 10:10:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是wxpython和wx.选择. 我试图绑定它,但它没有达到功能,也不工作,为什么呢? 另外,当我关注这个选择时(他已经绑定了),它只运行了两次函数。 为什么?我怎么能改变它?你知道吗

 Select=wx.Choice(parent, choices=SectorList,pos=pos,size=(100,25))
 Select.Bind(wx.EVT_KEY_DOWN,self.OnInputCharPressSelect)

Tags: key函数pos功能sizebindwxpythonselect
1条回答
网友
1楼 · 发布于 2024-04-20 10:10:47

我不知道你在做什么,因为你没有一个小的可运行的样品。举个例子:

import wx


class MyPanel(wx.Panel):

    def __init__(self, parent): 
        wx.Panel.__init__(self, parent)

        txt = wx.TextCtrl(self)
        self.choice_widget = wx.Choice(self, choices=['a', 'b', 'c'])
        self.choice_widget.Bind(wx.EVT_KEY_DOWN, self.OnInputCharPressSelect)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(txt, 0, wx.ALL, 5)
        main_sizer.Add(self.choice_widget, 0, wx.ALL, 5)

        self.SetSizer(main_sizer)

    def OnInputCharPressSelect(self, event):
        print('OnInputCharPressSelect fired')

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Choices')

        panel = MyPanel(self)

        self.Show()


if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

当我从TextCtrl小部件进入Choice小部件时,绑定事件处理程序不会触发,这是正确的。如果我用鼠标选择一个项目,它也不会启动。为此,需要将Choice小部件绑定到EVT_CHOICE。你知道吗

要启动OnInputCharPressSelect,必须突出显示Choice小部件(即选中),然后按键盘上的键。这将导致处理程序在每次按键时触发一次。你知道吗

我用wxpython4.0.0b2和python3.6在window7上测试了这段代码。你知道吗

相关问题 更多 >