2

我制作了一个包含一系列文本控件的自定义对话框。每个文本控件旁边都有几个按钮,用于更方便地添加特定值。我不希望这些按钮在用户使用它的选项卡遍历对话框时获得焦点,因为在大多数情况下,用户不需要使用这些按钮。

是否有任何方便的方法可以从标准选项卡遍历中排除特定控制器?

4

3 回答 3

3

防止按钮被键盘聚焦的一种简单方法是派生wx.lib.buttons.GenButtonwx.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返回 False 的自定义按钮类。

注意:虽然wx.PyWindow和实现允许覆盖的机制wx.PyPanel,但标准的 wxPython 控件却不允许。但是,在 python 端处理和检查将访问将调用重写方法的实际 python 对象。wx.PyControlAcceptsFocusFromKeyboardwx.EVT_NAVIGATION_KEYAcceptsFocusFromKeyboard

于 2013-01-28T12:29:15.587 回答
0

如果您使用的是 C++,则此问题有一个简单的解决方案,如本答案的其余部分所述。在 wxPython 中,您似乎不能专门化 wxWidgets 类——这在我看来是一个致命的障碍。

您可以通过重写 AcceptsFocusFromKeyboard() 以返回 FALSE 来创建按钮控件的特殊化,该控件将被排除在选项卡遍历之外。

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


}
于 2013-01-22T14:45:10.393 回答
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)
于 2014-08-06T08:55:23.263 回答