我制作了一个包含一系列文本控件的自定义对话框。每个文本控件旁边都有几个按钮,可以更方便地添加特定值。我不希望这些按钮在用户选项卡遍历对话框时获得焦点,因为在大多数情况下,用户不需要使用按钮。
有没有方便的方法从标准选项卡遍历中排除特定控制器?
答案 0 :(得分:2)
防止按钮被键盘聚焦的一种简单方法是从wx.lib.buttons.GenButton
或wx.lib.buttons.ThemedGenButton
派生,这些wx.PyControl
或AcceptsFocusFromKeyboard()
基于class NoFocusButton(wx.lib.buttons.ThemedGenButton):
def __init__(self, parent, id=wx.ID_ANY, label=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.ButtonNameStr):
wx.lib.buttons.ThemedGenButton.__init__(self,parent,id,label,pos,size,style,validator,name)
def AcceptsFocusFromKeyboard(self):
return False # does not accept focus
支持覆盖wx.EVT_NAVIGATION_KEY
:
self.GetChildren()
对于更复杂的导航规则或控件,您可以自己处理wx.WindowList
并管理导航。要获取要导航的窗口列表,可以使用.index(mywindow)
。可以通过def CycleList(thelist,index,forward):
for unused in range(len(thelist)): # cycle through the list ONCE
if forward:
index = index+1 if index+1 < len(thelist) else 0
else:
index = index-1 if index-1 >= 0 else len(thelist)-1
yield thelist[index]
获取wx.EVT_NAVIGATION_KEY
中当前关注窗口的索引。
使用该信息,只要用户按下“导航键”并将焦点设置为下一个适用的控件,就可以浏览列表,跳过那些您不想聚焦的控件。
为了更轻松地浏览列表,您可以创建一个生成器:
self.Bind(wx.EVT_NAVIGATION_KEY, self.OnNavigationKey)
在对话框中,处理def OnNavigationKey(self,event):
children = self.GetChildren() # list of child windows
focused = self.FindFocus() # current focus
# avoid accessing elements that do not exist
if not focused or focused not in children:
event.Skip() # use default behavior
return
index = children.index(focused)
for child in CycleList(children,index,event.GetDirection()):
# default behavior:
if child.AcceptsFocusFromKeyboard():
child.SetFocus()
return
:
AcceptsFocusFromKeyboard
wx.PyWindow
上面的示例模拟了默认行为:它循环通过可聚焦控件(跳过不可聚焦的控件,如静态文本)。
您可以展开检查以排除特定控件或创建实现wx.PyPanel
返回False的自定义按钮类。
注意:虽然wx.PyControl
,AcceptsFocusFromKeyboard
和wx.EVT_NAVIGATION_KEY
实现了允许覆盖AcceptsFocusFromKeyboard
的机制,但标准的wxPython控件不要。
但是,在python端处理{{1}}和检查{{1}}将访问将调用重写方法的实际python对象。
答案 1 :(得分:0)
如果你使用C ++,这个问题有一个简单的解决方案,如本答案的其余部分所述。在wxPython中,似乎你不能专门化wxWidgets类 - 这在我看来是一个致命的障碍。
您可以通过重写AcceptsFocusFromKeyboard()返回FALSE来创建按钮控件的特化,该控件将不在Tab遍历中。
http://docs.wxwidgets.org/trunk/classwx_window.html#a2370bdd3ab08e7ef3c7555c6aa8301b8
以下C ++代码工作正常:当按下选项卡时,焦点从第一个按钮跳转到第三个按钮
class cNoTabButton : public wxButton
{
public:
cNoTabButton(wxWindow *parent,
wxWindowID id,
const wxString& label = wxEmptyString,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0 )
: wxButton(parent,id,label,pos,size,style)
{}
bool AcceptsFocusFromKeyboard() const {
return false;
}
};
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
// set the frame icon
SetIcon(wxICON(sample));
wxPanel * panel = new wxPanel(this,-1,wxPoint(0,0),wxSize(500,500));
new wxButton(panel,-1,"TabPlease",wxPoint(20,20));
new cNoTabButton(panel,-1,"NoTabThanks",wxPoint(100,20));
new wxButton(panel,-1,"OKWithMe",wxPoint(200,20));
}
答案 2 :(得分:0)
这不是一个完美的解决方案,但实现此目的的一种方法实际上是延迟你的控制让焦点恢复半秒。
您的主窗口将重新获得焦点,按钮仍可正常工作。我使用它是因为我希望我的主窗口能够处理所有按键操作,但仍然包含与鼠标一起使用的按钮。
因此,您将KILL_FOCUS事件绑定在应该保留焦点的控件中,并创建一个无法从中获取焦点的控件列表。
首先是帮助所有孩子的帮助函数:
def GetAllChildren(control):
children = []
for c in control.GetChildren():
children.append(c)
children += GetAllChildren(c)
return children
在我的情况下,我希望窗口的所有孩子都无法获得焦点
self.not_allowed_focus = GetAllChildren(self)
self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
在我的KILL FOCUS处理程序中,我要求半秒后重点关注
def OnKillFocus(self,evt):
print "OnKillFocus"
win = evt.GetWindow()
if win in self.not_allowed_focus:
wx.CallLater(500,self.SetFocus)