我有这个代码:
import wx
class Plugin(wx.Panel):
def __init__(self, parent, *args, **kwargs):
panel = wx.Panel.__init__(self, parent, *args, **kwargs)
self.colorOver = ((89,89,89))
self.colorLeave = ((110,110,110))
self.colorFont = ((131,131,131))
self.SetBackgroundColour(self.colorLeave)
self.SetForegroundColour(self.colorLeave)
self.name = "Plugin"
self.overPanel = 0
self.overLabel = 0
sizer = wx.BoxSizer(wx.VERTICAL)
name = wx.StaticText(self, -1, ' ' + self.getName())
close = wx.StaticText(self, -1, ' X ')
gs = wx.GridSizer(2, 2, 0, 0)
gs.AddMany([(name, 0, wx.ALIGN_LEFT), (close, 0, wx.ALIGN_RIGHT)])
sizer.Add(gs, 1, wx.EXPAND)
self.SetSizer(sizer)
.... ....
是否可以左键单击 StaticText 关闭并隐藏面板本身?
我不知道是否可以将 wx.EVT_LEFT_DOWN 绑定到 StaticText 小部件。您可以使用按钮来调用 self.Hide()。如果您想要自定义外观,也许可以使用 BitmapButton。
class myPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
wx.Panel.__init__(self, parent, *args, **kwargs)
bitmap = wx.EmptyBitmap(15,15)
self.button = wx.BitmapButton(self, -1, bitmap=bitmap, size=(15,15), style=wx.NO_BORDER)
self.Bind(wx.EVT_BUTTON, self.onClick, self.button)
def onClick(self, event):
self.Hide()
我似乎记得必须将静态文本放入框架或某些可以接收事件的对象内,但使其不可见。然后,文本上的单击事件将传递给父级。我曾经有一个用于可点击标签的特殊派生类。
您可以使用 EVT_LEFT_DOWN 或 EVT_LEFT_UP 来检测 StaticText 小部件上的鼠标操作
close.Bind(wx.EVT_LEFT_DOWN, self.hide_frame)
经过一天的努力,我找到了解决方案。 不可能从框架捕获鼠标事件,这些事件在 wxWidgets 中被故意丢弃。
您需要继承wxStaticText并在子对象中捕获鼠标事件。
/// Extend wxStaticText with a new capability of propagating mouse clicks.
class mouseStaticText: public wxStaticText
{
public:
mouseStaticText(wxWindow *parent,
wxWindowID id,
const wxString& label,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxStaticTextNameStr): wxStaticText(parent, id, label, pos, size, style, name)
{}
void OnMouse(wxMouseEvent& event);
private:
DECLARE_EVENT_TABLE()
};
//declare external event notification when a camera resolution is changed.
BEGIN_DECLARE_EVENT_TYPES()
DECLARE_EVENT_TYPE(wxEVT_STATIC_TEXT_CLICKED, -1)
END_DECLARE_EVENT_TYPES()
BEGIN_EVENT_TABLE(mouseStaticText, wxStaticText)
EVT_SYS_COLOUR_CHANGED(wxWindowMSW::OnSysColourChanged)
EVT_ERASE_BACKGROUND(wxWindowMSW::OnEraseBackground)
EVT_LEFT_DOWN( mouseStaticText::OnMouse)
#ifdef __WXWINCE__
EVT_INIT_DIALOG(wxWindowMSW::OnInitDialog)
#endif
END_EVENT_TABLE()
/// Mouse click event needs to be propagated.
void mouseStaticText::OnMouse(wxMouseEvent& event)
{
/* Emit notification about camera change. */
wxCommandEvent NewEvent(wxEVT_STATIC_TEXT_CLICKED,GetId());
NewEvent.SetEventObject(this);
//event.SetText( wxT("Camera") ); // Give it some contents (I hope that this is not necessary)
GetEventHandler()->ProcessEvent(NewEvent);
}
然后你就可以正常捕获来自该对象的事件了
BEGIN_EVENT_TABLE(AboutDialog, wxDialog)
EVT_PAINT( AboutDialog::OnPaint)
EVT_BUTTON(ID_BTN_OK, AboutDialog::OnOk)
EVT_COMMAND(ID_URL_TEXT, wxEVT_STATIC_TEXT_CLICKED, AboutDialog::OnClickURL)
EVT_COMMAND(ID_ALT_URL_TEXT, wxEVT_STATIC_TEXT_CLICKED, AboutDialog::OnClickAltURL)
END_EVENT_TABLE()
此静态文本用于我的[此处的相机查看器][1]]的启动屏幕中