开发者

wxPython : EVT_BUTTON on a portion of a panel

开发者 https://www.devze.com 2023-04-11 08:26 出处:网络
Hello evryone :) I would like to capture click event in a blue box drawn inside a wx.Panel I already know how to reacts to button click :

Hello evryone :) I would like to capture click event in a blue box drawn inside a wx.Panel

I already know how to reacts to button click :

myButton.Bind(wx.EVT_BUTTON, myHandler)

or

myFrame.Bind(wx.EVT_BUTTON, myHandler, myFrame.myConcernedButton)

But how must I do if I want to

  1. Draw a blue square onto a Panel, what I can already manage, luckily.
  2. Capture a EVT_BUTTON onto the blue square only, and not the whole panel ?

I think I should make a new class for my SquareBox, but :

  1. What class should it be derived from ?
  2. So far then, how to add event handling to that class ?

Thank you very much.

P.S : For the little history, I used to develop in Java & SWING


Edit :

As Mike Driscoll advised me, I tried to solve my problem with a PlateButton. But unfortunately, I did not managed to give the button the wanted dimension nor the wanted style (it changes its color when it is clicked, and I don't wan't this). Furthermore, it doesn't react at all to开发者_StackOverflow中文版 EVT_BUTTON event.

This is my attempt, thanks in advance :

import wx
from wx.lib.platebtn import PlateButton

class Square(PlateButton):

    def __init__(self, parent, size, pos):
        PlateButton.__init__(self, parent, size = size, pos = pos)
        self.SetBackgroundColour(wx.Colour(0,0,255))

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Reactive square application",
            size = (300,200))
        panel = wx.Panel(self, wx.ID_ANY)
        square1 = Square(panel, size=(60,60), pos=(80,50))
        square2 = Square(panel, size=(60,60), pos=(80,120))
        square1.Bind(wx.EVT_BUTTON, self.OnSquareClick)

    def OnSquareClick(self, event):
        dialog = wx.MessageDialog(self, "You clicked on square !!!",
            "Hit has been done", wx.OK)
        dialog.Show(True)


if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MainFrame()
    frame.Show(True)
    app.MainLoop()


You'll probably need to create a custom widget. There's an article on the wiki about this topic here: http://wiki.wxpython.org/CreatingCustomControls

Look at the source for AquaButton or PlateButton as those are custom controls too.


Just bind to the panel then check the coordinates of the event.

0

精彩评论

暂无评论...
验证码 换一张
取 消