on wxPYTHON,这是默认设置,通过点击'TAB键'将焦点从TEXTctrl移动到另一个。 我想点击'Enter键'..我试过'wx.TE_PROCESS_ENTER'样式但是它不起作用..我使用的是Windows 8,这里是我的代码:
from wx import *
class test(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent;
self.basicsizer = wx.BoxSizer(wx.HORIZONTAL)
self.box=wx.Panel(self,size=(1150,600))
self.box.SetBackgroundColour("yellow")
self.basicsizer.Add(self.box, )
self.SetSizer(self.basicsizer)
self.MASTER_frames_sizer=wx.BoxSizer(wx.VERTICAL)
self.box.SetSizer(self.MASTER_frames_sizer)
self.frame1=wx.Panel(self.box,size=(1000,31))
self.indx=wx.TextCtrl(self.frame1,size=(40,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER);
self.a=wx.TextCtrl(self.frame1, size=(100,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER);
self.b=wx.TextCtrl(self.frame1, size=(100,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER)
self.c=wx.TextCtrl(self.frame1, size=(150,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER)
self.d=wx.TextCtrl(self.frame1, size=(150,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER)
self.e=wx.TextCtrl(self.frame1,size=(180,30),style=wx.TE_CENTRE|wx.TE_PROCESS_ENTER)
self.frame1_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.MASTER_frames_sizer.Add(self.frame1,flag=wx.ALIGN_RIGHT) # add frame 1
self.frame1_sizer.Add(self.e,flag=wx.ALIGN_TOP)
self.frame1_sizer.Add(self.d,flag=wx.ALIGN_TOP )
self.frame1_sizer.Add(self.c,flag=wx.ALIGN_TOP)
self.frame1_sizer.Add(self.b,flag=wx.ALIGN_TOP)
self.frame1_sizer.Add(self.a,flag=wx.ALIGN_TOP)
self.frame1_sizer.Add(self.indx,flag=wx.ALIGN_TOP)
self.frame1.SetSizer(self.frame1_sizer)
self.Layout()
app = wx.App(False)
frame_ = wx.Frame(None, title="test",size=(1350,800))
gui=test(frame_)
frame_.Show()
app.MainLoop()
更新: 好吧..我有解决方案的想法,但仍然缺少语法..在tkinter这可以通过将条目小部件与事件'key'或'keyRelease'(例如)绑定到处理程序然后检查这样的关键符号来实现:
if event.keysym=='Return' :
event.widget.event_generate('<Tab>')
那么如何在wxPYTHON上做到这一点?
答案 0 :(得分:2)
要解决这个问题,一个解决方案就是:
首先,当TextCtrl处于焦点时,使用加速表来缓存回车键。
return_id = wx.NewId()
acc_table = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_RETURN, return_id)])
self.SetAcceleratorTable(acc_table)
其次,在回车键的事件处理程序中,导航到下一个ctrl,以便在TextCtrl之后将焦点移动到下一个ctrl。
ctl = wx.Window_FindFocus()
ctl.Navigate()
第三,如果你想禁用enter-to-tab技巧,你可以使用wx.NullAcceleratorTable来禁用它。
self.SetAcceleratorTable(wx.NullAcceleratorTable)
完整示例如下所示:
# -*- coding: utf-8 -*-
""" Simulate Enter Key to Tab.
"""
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title=u'Test Enter to Tab Key', size=(800, 600))
self.panel = wx.Panel(self)
base_sizer = wx.BoxSizer(wx.VERTICAL)
text = wx.TextCtrl(self.panel, -1, '', size=(160, -1))
base_sizer.Add(text, border=5, flag=wx.EXPAND | wx.ALL)
for i in range(5):
bttn = wx.Button(self.panel, -1, 'Button %d' % i, size=(160, -1))
base_sizer.Add(bttn, border=5, flag=wx.EXPAND | wx.ALL)
return_id = wx.NewId()
acc_table = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_RETURN, return_id)])
self.SetAcceleratorTable(acc_table)
wx.EVT_MENU(self, return_id, self.on_return)
self.panel.SetAutoLayout(True)
self.panel.SetSizerAndFit(base_sizer)
self.Fit()
def on_return(self, event):
ctl = wx.Window_FindFocus()
ctl.Navigate()
# self.SetAcceleratorTable(wx.NullAcceleratorTable)
if __name__ == '__main__':
app = wx.App(redirect=False)
win = MainFrame()
win.Show()
app.MainLoop()