wxPython 的 isChecked() 响应不一致 - 为什么?
我有一个带有弹出菜单的图表,当用户右键单击时会出现该菜单。默认情况下,子选项 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()
该脚本的输出:
in showPopupMenu(): True
in commandA(): False
I have a graph with a popup menu that appears when the user right-clicks. Sub-option A is checked by default. I would like the user to be able to toggle this check mark for different behaviors. Thus, I would like to know if A is already checked or not, and I would like this to be handled in its own function. Let's call the function creating the menu showPopupMenu() and the function that needs to know the check status of A commandA().
Right now if I check A by default and print out the IsChecked() status of A, in showPopupMenu() the value is True and in commandA() the value is false. If A is unchecked by default the answers are inverted- showPopupMenu() shows the check status as false and commandA() shows it as true. This makes it seem like the IsChecked() status in commandA() is simply the reverse of the IsChecked() status in showPopupMenu(). Why is this?
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()
Output of this script:
in showPopupMenu(): True
in commandA(): False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您单击 A 时,复选标记从 True 切换为 False,这就是您看到它输出 False 的原因。但是,您每次都会创建和销毁窗口,这就是为什么您从未看到它更改检查状态的原因。
以下是如何设置菜单而无需每次都创建和销毁菜单的示例:
When you click on A, the check mark switches from True to False, which is why you see it output False there. However, you create and destroy your window every time which is why you never see it change the check state.
Here is an example of how to set up the menu without creating and destroying it every time: