gpt4 book ai didi

python - 我可以从标准选项卡遍历中排除特定 Controller 吗?

转载 作者:行者123 更新时间:2023-12-01 05:50:35 25 4
gpt4 key购买 nike

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

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

最佳答案

防止按钮通过键盘获得焦点的一个简单方法是从 wx.lib.buttons.GenButtonwx.lib.buttons.ThemedGenButton 派生,其中基于支持覆盖 AcceptsFocusFromKeyboard()wx.PyControl:

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() 。通过.index(mywindow)可以获取wx.WindowList中当前焦点窗口的索引。有了这些信息,只要用户按下“导航键”,您就可以在列表中导航,并将焦点设置到下一个适用的控件,跳过那些您不想关注的控件。

为了更轻松地浏览列表,您可以创建一个生成器:

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

上面的示例模拟了默认行为:它循环显示可聚焦控件(跳过不可聚焦控件,例如静态文本)。您可以扩展检查以排除特定控件,或创建一个自定义按钮类来实现返回 False 的 AcceptsFocusFromKeyboard

注意:虽然wx.PyWindowwx.PyPanelwx.PyControl实现了允许覆盖 AcceptsFocusFromKeyboard,标准 wxPython 控件。但是,在 python 端处理 wx.EVT_NAVIGATION_KEY 并检查 AcceptsFocusFromKeyboard 将访问实际的 python 对象,该对象将调用重写的方法。

关于python - 我可以从标准选项卡遍历中排除特定 Controller 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com