gpt4 book ai didi

python - wxPython 中加速器的编程绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 01:32:26 24 4
gpt4 key购买 nike

我正在尝试以编程方式在循环中的 wxPython 中创建和绑定(bind)加速器表,这样我就不必担心为每个加速器获取和分配新的 ID(以及为了从某些外部资源,而不是硬编码)。我还通过 lambda 将一些参数传递给处理程序,因为我的很多处理程序都是相同的,但参数不同(移动、缩放等)。

该类是 wx.Frame 的子类,setup_accelerators() 在初始化期间被调用。

def setup_accelerators(self):

bindings = [
(wx.ACCEL_CTRL, wx.WXK_UP, self.on_move, 'up'),
(wx.ACCEL_CTRL, wx.WXK_DOWN, self.on_move, 'down'),
(wx.ACCEL_CTRL, wx.WXK_LEFT, self.on_move, 'left'),
(wx.ACCEL_CTRL, wx.WXK_RIGHT, self.on_move, 'right'),
]


accelEntries = []

for binding in bindings:
eventId = wx.NewId()
accelEntries.append( (binding[0], binding[1], eventId) )

self.Bind(wx.EVT_MENU, lambda event: binding[2](event, binding[3]), id=eventId)


accelTable = wx.AcceleratorTable(accelEntries)
self.SetAcceleratorTable(accelTable)

def on_move(self, e, direction):
print direction

然而,这似乎将所有加速器绑定(bind)到最后一个条目,因此 Ctrl+Up 打印 "right",就像所有其他三个。如何以这种方式正确绑定(bind)多个处理程序?

最佳答案

明白了。问题是你没有将你的方向作为你的 lambda 的参数传递(它只查看事件参数(和 self ),因为你的 lambda 被定义为接受)This页面为我提供了使它正常工作所需的帮助(这里是 gist ):

import wx

class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Programmatic binding of accelerators in wxPython", size=(450,150))

# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
bindings = [
(wx.ACCEL_CTRL, wx.WXK_UP, 'up'),
(wx.ACCEL_CTRL, wx.WXK_DOWN, 'down'),
(wx.ACCEL_CTRL, wx.WXK_LEFT, 'left'),
(wx.ACCEL_CTRL, wx.WXK_RIGHT, 'right'),
]


accelEntries = []

for binding in bindings:
eventId = wx.NewId()
accelEntries.append( (binding[0], binding[1], eventId) )

self.Bind(wx.EVT_MENU, lambda evt, temp=binding[2]: self.on_move(evt, temp), id=eventId)

accelTable = wx.AcceleratorTable(accelEntries)
self.SetAcceleratorTable(accelTable )
#----------------------------------------------------------------------

def on_move(self, Event, direction):
print "You pressed CTRL+"+direction

# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

我原来的答案(这是一个可接受的替代解决方案)是:

import wx

class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))

# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
bindings = [
(wx.ACCEL_CTRL, wx.WXK_UP, self.on_move_up),
(wx.ACCEL_CTRL, wx.WXK_DOWN, self.on_move_down),
(wx.ACCEL_CTRL, wx.WXK_LEFT, self.on_move_left),
(wx.ACCEL_CTRL, wx.WXK_RIGHT, self.on_move_right),
]


accelEntries = []

for binding in bindings:
eventId = wx.NewId()
accelEntries.append( (binding[0], binding[1], eventId) )

self.Bind(wx.EVT_MENU, binding[2], id=eventId)

accelTable = wx.AcceleratorTable(accelEntries)
self.SetAcceleratorTable(accelTable )
#----------------------------------------------------------------------
def on_move_up(self, event):
print "You pressed CTRL+up"
def on_move_down(self, event):
print "You pressed CTRL+down"
def on_move_left(self, event):
print "You pressed CTRL+left"
def on_move_right(self, event):
print "You pressed CTRL+right"

# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

关于python - wxPython 中加速器的编程绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com