简体   繁体   English

将焦点转移到下一个条目

[英]move focus to the next entry

on wxPYTHON , It is default setting to move focus from an TEXTctrl to another by hitting 'TAB key'.. I want to do with hitting ' Enter key ' ..I tried style 'wx.TE_PROCESS_ENTER' but it does not work ..Im using windows 8 and here is my code: 在wxPYTHON上,默认设置是通过点击'TAB键'将焦点从TEXTctrl移动到另一个。我想点击'输入键'..我试过样式'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()  

Update: well ..I have solution idea but still missing the syntax.. on tkinter this can be accomplished by binding the entry widget with the event 'key' or 'keyRelease' (for example) to a handler and then check for key symbol like this : 更新:好..我有解决方案的想法,但仍然缺少语法..在tkinter这可以通过将条目小部件与事件'key'或'keyRelease'(例如)绑定到处理程序然后检查关键符号来实现像这样 :

            if event.keysym=='Return' :
              event.widget.event_generate('<Tab>')

so how to do that on wxPYTHON ? 那么如何在wxPYTHON上做到这一点?

To solve this problem, one solution is this: 要解决这个问题,一个解决方案就是:

First, use accelerator table to cache the enter key, when TextCtrl is on focus. 首先,当TextCtrl处于焦点时,使用加速器表来缓存回车键。

return_id = wx.NewId()
acc_table = wx.AcceleratorTable([(wx.ACCEL_NORMAL, wx.WXK_RETURN, return_id)])
self.SetAcceleratorTable(acc_table)

Second, in event handler of the enter key, navigate to next ctrl so that move focus to next ctrl after TextCtrl. 其次,在回车键的事件处理程序中,导航到下一个ctrl,以便在TextCtrl之后将焦点移动到下一个ctrl。

ctl = wx.Window_FindFocus()
ctl.Navigate()

Third, if you want to disable the enter-to-tab trick, you can use wx.NullAcceleratorTable to disable this. 第三,如果要禁用enter-to-tab技巧,可以使用wx.NullAcceleratorTable来禁用它。

self.SetAcceleratorTable(wx.NullAcceleratorTable)

The full example shows as the following: 完整示例如下所示:

# -*- 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM