简体   繁体   中英

How to bind an event to a ribbon button click in wxpython-phoenix

I want to know how to use bind method with ribbon button in wxpython for python 3.4 (Phoenix version 3.0.3) because I tried all possible ways used with menus and buttons but all the time I have an error looks like:

File "C:\\Anaconda3\\lib\\site-packages\\wx\\core.py", line 1200, in _EvtHandler_Bind assert source is None or hasattr(source, 'GetId') AssertionError

please help with simple example if possible. Thanks in advance.

I found a solution to my problem by using

import wx.ribbon as RB

instead of:

import wx.lib.agw.ribbon as RB

and bind with:

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()

I will leave my answer which worked for me perfectly without accepting it for a while to give a chance for better idea, if not I will accept mine.

You can bind a specific event handler to each button if you assign an ID to them instead of using wx.ID_ANY. It's cleaner than having a large and growing if/elif/else block in a single event handler.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM