wxPython的isChecked()响应不一致 - 为什么?

时间:2011-04-01 18:39:59

标签: python wxpython matplotlib popupmenu

我有一个图表,当用户右键单击时会显示一个弹出菜单。默认情况下会检查子选项A.我希望用户能够为不同的行为切换此复选标记。因此,我想知道A是否已经检查过,我希望在自己的函数中处理它。让我们调用创建菜单showPopupMenu()的函数和需要知道A commandA()的检查状态的函数。

现在,如果我默认选中A并打印出A的IsChecked()状态,则在showPopupMenu()中,该值为True,而在commandA()中,该值为false。如果默认情况下未选中A,则答案将被反转 - showPopupMenu()将检查状态显示为false,并且commandA()将其显示为true。这使得看起来commandA()中的IsChecked()状态与showPopupMenu()中的IsChecked()状态相反。为什么是这样?

import wxversion
wxversion.ensureMinimal('2.8')
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WX')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx


class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self,None,-1,
                     'Example Frame',size=(550,350))

        self.SetBackgroundColour(wx.NamedColor("WHITE"))

        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        t = arange(0.0,3.0,0.01)
        s = sin(2*pi*t)

        self.axes.plot(t,s)
        self.canvas = FigureCanvas(self, -1, self.figure)
        self.canvas.Bind(wx.EVT_RIGHT_DOWN,
                   self.showPopupMenu)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
        self.SetSizer(self.sizer)
        self.Fit()

    def showPopupMenu(self, event):
        if not hasattr(self, "popupTwo"):
            self.popupOne = wx.NewId()
            self.popupTwo = wx.NewId()
            self.popupThree = wx.NewId()
            self.popupA = wx.NewId()
            self.popupB = wx.NewId()

    self.popupOne = wx.Menu()
    my_a = self.popupOne.Append(self.popupA, "A", kind=wx.ITEM_CHECK)
    self.popupOne.Append(self.popupB, "B")
    self.popupOne.Check(self.popupA, True)
    #self.popupOne.Check(self.popupA, False)
    print "in showPopupMenu(): ",self.popupOne.IsChecked(self.popupA)
        self.Bind(wx.EVT_MENU, self.commandA, my_a) 

        menu = wx.Menu()
        menu.AppendMenu(-1, 'One', self.popupOne)
        menu.Append(self.popupTwo, "Two")
        menu.Append(self.popupThree, "Three")

        self.PopupMenu(menu)
        menu.Destroy()

    def commandA(self, event):
    print "in commandA(): ", self.popupOne.IsChecked(self.popupA)

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

此脚本的输出:

showPopupMenu()中的

:True

命令A()中的

:错误

1 个答案:

答案 0 :(得分:0)

单击A时,复选标记从True切换为False,这就是为什么在那里看到输出False的原因。但是,您每次都会创建并销毁窗口,这就是为什么您从未看到它改变检查状态的原因。

以下是如何设置菜单而不是每次都创建和销毁菜单的示例:

import wx
import gui

class MainFrame(wx.Frame):
    def __init__( self, parent ):
        wx.Frame.__init__ (self, parent)

        # Layout my frame
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = wx.Panel(self)
        self.panel.Layout()
        self.panel.Bind(wx.EVT_RIGHT_DOWN, self.showPopupMenu)
        sizer.Add( self.panel, 1, wx.EXPAND |wx.ALL, 0 )
        self.SetSizer( sizer )
        self.Layout()

        # Add the menu
        self.popupA = wx.NewId()
        self.popupOne = wx.Menu()

        my_a = self.popupOne.Append(self.popupA, "A", kind=wx.ITEM_CHECK)
        self.popupOne.Check(self.popupA, True)

        self.Bind(wx.EVT_MENU, self.commandA, my_a)

        self.menu = wx.Menu()
        self.menu.AppendMenu(-1, 'One', self.popupOne)

    def showPopupMenu(self, event):
        print "in showPopupMenu(): ",self.popupOne.IsChecked(self.popupA)
        self.PopupMenu(self.menu)

    def commandA(self, event):
        event.Skip()
        print "in commandA(): ", self.popupOne.IsChecked(self.popupA)

class app(wx.App):
    def OnInit(self):
        self.m_frame = MainFrame(None)
        self.m_frame.Show()
        self.SetTopWindow(self.m_frame)
        return True

app = app(0)
app.MainLoop()