绑定和wxpython在列表中的位置 [英] position in a list with bind and wxpython

查看:62
本文介绍了绑定和wxpython在列表中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定列表中的位置?我有以下源代码:

How can I determine the position in a list? I have the following source code:

   def __init__(self, parent):
      self.tickets      = []
      for i in list(range(15)):
         self.intRandomNum=self.getRandomNum(i)
         self.tickets.append(wx.StaticBitmap(self.panels2[i], -1, wx.Bitmap(self.ticketImages[self.intRandomNum])))
         self.tickets[i].Bind(wx.EVT_LEFT_DOWN, self.cardChoice)


   def cardChoice(self, event):    
      alert = wx.MessageDialog(self, _("Do you want to choice this card?"),
         _("Cardchoice"), wx.YES_NO|wx.ICON_EXCLAMATION)
      if alert.ShowModal() == wx.ID_YES:
         ret = wx.MessageDialog(self, _("This card:"+repr(self.intRandomNum)),
         _("Cardchoice"), wx.YES_NO|wx.ICON_EXCLAMATION)
         ret.ShowModal()
   

使用intRandomNum的输出,我仅获得最后一个随机值.但是,如何输出我单击的卡?使用Bind,我认为我无法传递其他参数:

With the output of intRandomNum I only get the last random value. But how can I output which card I clicked on? With Bind, I don't think I can pass an additional parameter:

推荐答案

附加参数可以使用事件 lambda 函数在 Bind中传递命令.
以这种方式使用,它允许您在 event 之后追加参数.
请注意,回调函数必须允许其他参数.
这里的事件 callback 函数为未发送的可能参数提供了默认值(例如, flag3 ).

Additional parameters can be passed using a lambda function for the event, in the Bind command.
Used in this way, it allows you to append parameters after the event.
Note that the callback function, has to allow for additional parameters.
Here the event callback function, provides default values for the possible parameters, in case they aren't sent (case in point, flag3).

import wx

class TestFrame(wx.Frame):
    def __init__(self, *args):
        wx.Frame.__init__(self, *args)
        panel = wx.Panel(self, -1)
        button = wx.Button(panel, -1, "Click me", pos=(10,10), size=(100,40))
        button.Bind(wx.EVT_BUTTON, lambda event: self.OnButton(event, flag1="1", flag2="2"))
        self.Show()

    def OnButton(self, event, flag1=None, flag2=None, flag3=None):
        print ("The Button was pressed")
        print ("Flag1 was set to ", flag1)
        print ("Flag2 was set to ", flag2)
        print ("Flag3 was set to ", flag3)

if __name__ == "__main__":
    app = wx.App()
    myframe = TestFrame(None, -1, "Button lambda event Test")
    app.MainLoop()

编辑上面的方法适用于文字分配,但是在循环中使用变量,循环计数的值不断变化,并保留为最终值.
问题是,据我所知,lambda函数在执行时对表达式进行求值,因此它将获取变量的最终值/当前值.
在这里,我调整了 lambda 的结构以实现此目的,并使用了"y"的即时值.

Edit The above works for literal assignment but using a variable within a loop, the value of the loop count keeps changing and is retained as the final value.
The problem being, that as far as I can tell, the lambda function evaluates the expression on execution, so it picks up the final/current value of the variable.
Here I adjust the construction of the lambda to allow for this, with the on-the-fly value of ´y´.

import wx

class TestFrame(wx.Frame):
    def __init__(self, *args):
        wx.Frame.__init__(self, *args)
        panel = wx.Panel(self, -1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        buttons = []
        for i in range(10):
            buttons.append(wx.Button(panel, id = i, label=str(i), size=(100,40)))
            sizer.Add(buttons[i])
            lmda = lambda event, y = i: self.OnButton(event, flag1=int(y), flag2="z")
            buttons[i].Bind(wx.EVT_BUTTON, lmda )
        panel.SetSizer(sizer)
        self.Show()

    def OnButton(self, event, flag1=None, flag2=None, flag3=None):
        print ("The Button was pressed", flag1)

if __name__ == "__main__":
    app = wx.App()
    myframe = TestFrame(None, -1, "Button lambda event Test")
    app.MainLoop()

请注意, lambda 是单独创建的,纯粹是为了方便和清晰起见.

Note the lambda is created separately purely for convenience and clarity.

这篇关于绑定和wxpython在列表中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆