我想知道如何在wxpython for python 3.4(Phoenix 3.0.3版)中使用带有功能区按钮的绑定方法,因为我尝试了所有可能用于菜单和按钮的方法,但我总是遇到错误:< / p>
文件&#34; C:\ Anaconda3 \ lib \ site-packages \ wx \ core.py&#34;,第1200行,_EvtHandler_Bind 断言源是None或hasattr(source,&#39; GetId&#39;) 的AssertionError
如果可能的话,请帮助简单的例子。提前谢谢。答案 0 :(得分:0)
我使用
找到了解决问题的方法import wx.ribbon as RB
而不是:
import wx.lib.agw.ribbon as RB
并绑定:
import wx
import wx.ribbon as RB
# Class code goes here...
self.ribbon = RB.RibbonBar(self,wx.NewId())
self.page_home = RB.RibbonPage(self.ribbon, wx.NewId(), "Home")
self.panel1 = RB.RibbonPanel(self.page_home, wx.ID_ANY, "Panel#1")
self.button_bar1 = RB.RibbonButtonBar(self.panel1)
bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_OTHER, wx.Size(32, 32))
self.button_bar1_Exit = self.button_bar1.AddButton(wx.ID_ANY, "Exit", bmp, 'Close Window')
self.button_bar1.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.on_button_bar1)
def on_button_bar1(self, event):
button = event.GetButton()
if button == self.button_bar1_Exit:
self.Close()
如果不是我接受我的话,我会留下我的答案,这些答案对我来说是完美的,但却不接受它一段时间给予机会以获得更好的想法。
答案 1 :(得分:0)
如果为其分配ID而不是使用wx.ID_ANY,则可以将特定事件处理程序绑定到每个按钮。它比在单个事件处理程序中拥有一个大而且不断增长的if / elif / else块更清晰。
import wx
import wx.ribbon as RB
BUTTON_EXIT_ID = wx.NewId()
# Class code goes here...
self.ribbon = RB.RibbonBar(self,wx.NewId())
self.page_home = RB.RibbonPage(self.ribbon, wx.NewId(), "Home")
self.panel1 = RB.RibbonPanel(self.page_home, wx.ID_ANY, "Panel#1")
self.button_bar1 = RB.RibbonButtonBar(self.panel1)
bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_OTHER, wx.Size(32, 32))
self.button_bar1_Exit = self.button_bar1.AddButton(BUTTON_EXIT_ID, "Exit", bmp, 'Close Window')
self.button_bar1.Bind(RB.EVT_RIBBONBUTTONBAR_CLICKED, self.on_exit, id=BUTTON_EXIT_ID)
def on_exit(self, event):
self.Close()