【问题标题】:Why does my <a> and <b> if conditional not execture when a and b are both True? [closed]为什么当 a 和 b 都为真时,如果条件不执行我的 <a> 和 <b>? [关闭]
【发布时间】:2012-02-28 08:20:06
【问题描述】:

编辑:这是我的错字。由于某种原因,我在 GetEventObject 之后看不到缺少括号。

代码:

   def onKeyDown(self, event):
        ESC_list = [self.topic_control,self.search_control]
        print event.GetEventObject() in ESC_list
        keycode = event.GetKeyCode()
        print keycode == wx.WXK_ESCAPE
        if keycode == wx.WXK_ESCAPE and event.GetEventObject in ESC_list:
            print "fire"
            self.onExit(event)
        event.Skip()

当我运行两个打印语句都给我“True”的测试时,if 条件不会执行。什么给了?

【问题讨论】:

    标签: python if-statement wxpython wxwidgets


    【解决方案1】:

    您缺少一对括号:

    if keycode == wx.WXK_ESCAPE and event.GetEventObject() in ESC_list:
    #                                                   ^^ HERE
    

    您当前的代码在语法上是有效的,但具有不同的语义。它检查函数对象本身——而不是调用函数的结果——是否存在于ESC_list 中。

    【讨论】:

    • 谢谢。在我来检查答案之前,我自己发现了这一点。
    猜你喜欢
    • 1970-01-01
    • 2015-05-01
    • 2020-09-20
    • 2014-03-29
    • 2015-11-15
    • 2013-05-29
    • 2021-10-06
    • 2011-05-30
    • 1970-01-01
    相关资源
    最近更新 更多