将"输入"键绑定到 WXPython "AquaButton" 带有焦点的按钮



我是这个网站的新手,对Python相当陌生。我正在开发一个使用 wxPython 用于 GUI 的程序。 这正在开发中,并将在Windows操作系统机器上使用。 GUI 使用许多按钮来触发脚本中的不同进程。我决定尝试使用AGW AquaButtons来获得视觉兴趣。 我发现具有焦点的"AquaButton"不会响应按"Enter"键,而标准"wx。按钮"确实如此。 以下是工作和非工作代码的示例(主要借用类似的问题,感谢"萨克森的罗尔夫"和"迈克德里斯科尔"(。

有没有办法让"AquaButton"(带焦点(的事件由"Enter"键触发?

这有效:

import wx
class Example(wx.Frame):
def __init__(self, parent, title):
frame = wx.Frame.__init__(self, parent, title=title, )
self.panel = wx.Panel(self, -1, size=(200,100))
self.btn1 = wx.Button(self.panel, label='Button 1', id=1)
self.btn1.SetForegroundColour("black")
self.btn2 = wx.Button(self.panel, label='Button 2', id=2)
self.btn2.SetForegroundColour("black")
self.sizer = wx.GridBagSizer(0, 0)
self.sizer.Add(self.btn1, pos=(0, 0), flag=wx.ALIGN_CENTER)
self.sizer.Add(self.btn2, pos=(1, 0), flag=wx.ALIGN_CENTER)
self.text_box = wx.StaticText(self.panel, style = wx.NO_BORDER)
self.text_box.SetFont(wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
self.text_box.SetForegroundColour((40,115,180))
self.sizer.Add(self.text_box, pos=(2,0), flag=wx.ALIGN_CENTER)
self.Bind(wx.EVT_BUTTON, self.button_press)
self.panel.SetSizer(self.sizer)
self.Show()
def button_press(self, event):
Id = event.GetId()
print ('Click Button',str(Id))
retVal = F"Click Button {Id}"
self.text_box.SetLabel(str(retVal))

class AppMenu(wx.App):
def OnInit(self):
'Create the main window and insert the custom frame'
frame = Example(None, 'Example')
frame.Show(True)
return True
app = AppMenu()
app.MainLoop()

这不会:

import wx
import wx.lib.agw.aquabutton as AB
class Example(wx.Frame):
def __init__(self, parent, title):
frame = wx.Frame.__init__(self, parent, title=title, )
self.panel = wx.Panel(self, -1, size=(200,100))
self.btn1 = AB.AquaButton(self.panel, label='Button 1', id=1)
self.btn1.SetForegroundColour("black")
self.btn2 = AB.AquaButton(self.panel, label='Button 2', id=2)
self.btn2.SetForegroundColour("black")
self.sizer = wx.GridBagSizer(0, 0)
self.sizer.Add(self.btn1, pos=(0, 0), flag=wx.ALIGN_CENTER)
self.sizer.Add(self.btn2, pos=(1, 0), flag=wx.ALIGN_CENTER)
self.text_box = wx.StaticText(self.panel, style = wx.NO_BORDER)
self.text_box.SetFont(wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
self.text_box.SetForegroundColour((40,115,180))
self.sizer.Add(self.text_box, pos=(2,0), flag=wx.ALIGN_CENTER)
self.Bind(wx.EVT_BUTTON, self.button_press)
self.panel.SetSizer(self.sizer)
self.Show()
def button_press(self, event):
Id = event.GetId()
print ('Click Button',str(Id))
retVal = F"Click Button {Id}"
self.text_box.SetLabel(str(retVal))

class AppMenu(wx.App):
def OnInit(self):
'Create the main window and insert the custom frame'
frame = Example(None, 'Example')
frame.Show(True)
return True
app = AppMenu()
app.MainLoop()

奇怪的是,AquaButton是使用空格键而不是Enter键按下的。
导航似乎是通过箭头键和/或 Tab 和 Shift Tab 键进行的。
如有疑问,源代码位于:
your_python_location/dist-packages/wx/lib/agw/aquabutton.py

我希望这能为你澄清这一点,如果不是为你的用户。 :)

最新更新