wxPython:面板一部分上的 EVT_BUTTON
你好 evryone :) 我想在 wx.Panel 内绘制的蓝色框中捕获单击事件
我已经知道如何对按钮单击做出反应:
myButton.Bind(wx.EVT_BUTTON, myHandler)
或者
myFrame.Bind(wx.EVT_BUTTON, myHandler, myFrame.myConcernedButton)
但是如果我想
- 在面板上绘制一个蓝色方块,我该怎么做,什么幸运的是,我已经可以应付了。
- 仅将 EVT_BUTTON 捕获到蓝色方块上,而不是整个面板上?
我想我应该为我的 SquareBox 创建一个新类,但是:
- 它应该从哪个类派生?
- 到目前为止,如何向该类添加事件处理?
非常感谢。
PS:对于很少的历史,我曾经使用 Java 和 Java 进行开发。 SWING
编辑:
正如 Mike Driscoll 所建议的那样,我尝试使用 PlateButton 来解决我的问题。但不幸的是,我没有设法为按钮提供所需的尺寸或所需的样式(单击时它会改变颜色,我不希望这样)。此外,它对 EVT_BUTTON 事件根本没有反应。
这是我的尝试,提前致谢:
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()
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
- Draw a blue square onto a Panel, what I can already manage, luckily.
- 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 :
- What class should it be derived from ?
- 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 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要创建一个自定义小部件。 wiki 上有一篇关于此主题的文章: http://wiki.wxpython.org/CreatingCustomControls
看看在 AquaButton 或 PlateButton 的源代码中,因为它们也是自定义控件。
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.