Problem with TextCtrl focus since Windows 10

Hallo,
i’m using wxpython now since a few years, mostly with Ubuntu. Now, i tried my code again under Windows and since Windows 10 i do have a problem the focus of the TextCtrl objects.
If i select a TextCrtl with the mouse, it gets framed (on Win 10), or blue underlined (Win 11), and it stay this way, even when i select an other TextCtrl. This also happens for inactive TextCtrl windows. AND i’m not able to return the focus to one of those windows with the cursor. I can use the TAB-key to get the keyboard input back to those TextCtrl windows, then the cursor still stays at his last position, whereas the letters appear in the TAB-selected window.

I’ve searched the web for those issues, but really got no results. From this search i got the brief idea it may depend on the ‘voice output’-focus of windows, but turning off the voice output didn’t solve the problem.

So it may be rather a windows-problem then a wx-problem, but if anyone has an idea, he is welcome!

Thanks a lot in advance,
Andreas K.

I wonder if you have the controls in panel? Best of my knowledge for the tabs/returns/mouse to act normal they have to be in a panel.

Johnf

Hallo,
i’ve changed my code to sizers and therefore i do not use a panel at the moment (at least not expicitly). But for some dialogs, i still have my hardcodet panel code. I just tested this old code and yes, it has the same behaviour.

Thanks for your answer.
Andreas K.

well, you haven’t got a runnable code, have you? but if you have please put it into </> icon and some fog may lift :face_with_peeking_eye:

Thanks, you’re right, i’ve to strip it down to get a clear view on the problem. Hopefully i’ll get a small executable example of the problem.
Think that might even be the best way to track the problem, as i now noticed, the i have smaller dialogs, where the problem does not occur, the keyboard focus and the cursur focus are synchronised correctly.
The whole time i assumed it to be a principal windows or wx problem. But the problem really seems to lie in my code.
I’ll come back to you, when i’ve stripprd down the problem.

Andreas K.

Hallo,
now i have two examples extracted from my code, one where the problem occurs (TestDialog_Ko) and a second, where everything work OK (TestDialog_OK):
REMARK: you need to replace the two toolbar icons (Bus_AfpX.png) with local icons on your machine

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx

class TestDialog_Ko(wx.Dialog):
    def __init__(self, *args, **kw):
        print ("TestDialog_Ko Input:", args, kw)
        self.textmap = {}
        self.vtextmap = {}
        self.choicemap = {}
        self.no_readonly = True
        self.close_dialog = True
        super(TestDialog_Ko, self).__init__(*args, **kw) 
        self.left_weight = 1
        self.right_weight = 7
        #self.right_weight = 2
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        
        self.statictext = wx.StaticText(self, -1, label="Test with asynchron focus", name="Text")

        self.line2_sizer = wx.BoxSizer(wx.HORIZONTAL)        
        self.label_T_Vorname_Adresse = wx.StaticText(self, -1, label="&Vorname:", name="T_Vorname_Adresse")
        self.text_Vorname_Adresse = wx.TextCtrl(self, -1, value="",  style=wx.TE_MULTILINE|wx.TE_BESTWRAP, name="Vorname_Adresse")
        self.text_Vorname_Adresse.Bind(wx.EVT_KILL_FOCUS, self.On_KillFocus)
        self.textmap["Vorname_Adresse"] = "Vorname.ADRESSE"
        self.line2_sizer.AddSpacer(10)
        self.line2_sizer.Add(self.label_T_Vorname_Adresse,self.left_weight,wx.EXPAND)
        self.line2_sizer.AddSpacer(10) 
        self.line2_sizer.Add(self.text_Vorname_Adresse,self.right_weight,wx.EXPAND)
        self.line2_sizer.AddSpacer(10) 

        self.line3_sizer = wx.BoxSizer(wx.HORIZONTAL)        
        self.label_T_Name_Adresse = wx.StaticText(self, -1, label="&Name:", name="T_Name_Adresse")
        self.text_Name_Adresse = wx.TextCtrl(self, -1, value="", style=wx.TE_MULTILINE|wx.TE_BESTWRAP, name="Name_Adresse")
        self.text_Name_Adresse.Bind(wx.EVT_KILL_FOCUS, self.On_KillFocus)
        self.textmap["Name_Adresse"] = "Name.ADRESSE"
        self.line3_sizer.AddSpacer(10)
        self.line3_sizer.Add(self.label_T_Name_Adresse,self.left_weight,wx.EXPAND)
        self.line3_sizer.AddSpacer(10) 
        self.line3_sizer.Add(self.text_Name_Adresse,self.right_weight,wx.EXPAND)
        self.line3_sizer.AddSpacer(10) 
        
        self.line4_sizer = wx.BoxSizer(wx.HORIZONTAL)        
        self.label_T_Strasse_Adresse = wx.StaticText(self, -1, label="Stra&sse:",  name="T_Strasse_Adresse")
        self.text_Strasse_Adresse = wx.TextCtrl(self, -1, value="", style=0, name="Strasse_Adresse")
        self.text_Strasse_Adresse.Bind(wx.EVT_KILL_FOCUS, self.On_KillFocus)
        self.textmap["Strasse_Adresse"] = "Strasse.ADRESSE"
        self.line4_sizer.AddSpacer(10)
        self.line4_sizer.Add(self.label_T_Strasse_Adresse,self.left_weight,wx.EXPAND)
        self.line4_sizer.AddSpacer(10) 
        self.line4_sizer.Add(self.text_Strasse_Adresse,self.right_weight,wx.EXPAND)
        self.line4_sizer.AddSpacer(10) 
       
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.statictext, 1, wx.EXPAND)
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.line2_sizer,1,wx.EXPAND)
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.line3_sizer,2,wx.EXPAND)
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.line4_sizer,1,wx.EXPAND)
        self.sizer.AddSpacer(10)
        
        self.SetSize((560,315))
        self.SetTitle("Test Dialog Ko")
        self.SetSizer(self.sizer)
        self.SetSizerAndFit(self.sizer)
        self.SetAutoLayout(1)
        #self.sizer.Fit(self)
         
    def On_KillFocus(self, event):
        print ("TestDialog.On_KillFocus")

class TestDialog_Ok(wx.Dialog):
    ## constructor
    def __init__(self, *args, **kw):
        super(TestDialog_Ok, self).__init__(*args, **kw) 
        width = 250
        height = 70
        self.Ok = False
        self.typ = None
        self.result = []
        self.label = []
        self.texts = []
        self.check = []
        self.buttons = []
        self.button_pushed = None
        self.sizers = []
        self.statictext = wx.StaticText(self, -1, label="Test with synchron focus", name="Text")

        self.SetTitle("Test OK")
        self.lines = 3
        self.texts = [None]*self.lines
        self.sizers = [None]*self.lines
        for i in range(self.lines):
            self.label.append(wx.StaticText(self, -1, label="Label " + str(i), name="L" + str(i)))
            self.texts[i] = wx.TextCtrl(self, -1, value="", name="T" + str(i))
            self.sizers[i] = wx.BoxSizer(wx.HORIZONTAL)
            self.sizers[i].AddStretchSpacer(1)
            self.sizers[i].Add(self.label[-1],5,wx.EXPAND)
            self.sizers[i].Add(self.texts[i],9,wx.EXPAND) 
            self.sizers[i].AddStretchSpacer(1)
            height += 35  #30+5
        self.sizer=wx.BoxSizer(wx.VERTICAL)
        self.sizer.AddSpacer(10)
        self.sizer.Add(self.statictext, 1, wx.EXPAND)
        self.sizer.AddSpacer(5)
        for i in range(self.lines):
            self.sizer.AddSpacer(5)
            self.sizer.Add(self.sizers[i], 1, wx.EXPAND)
        self.sizer.AddSpacer(10)
        self.SetSizerAndFit(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
        self.SetSize((width, height))

class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw) 
        self.InitUI()
        
    def InitUI(self):    
        ID_LEFT = wx.NewIdRef()
        tb = self.CreateToolBar()
        tb.AddTool(toolId=ID_LEFT, label='Ko', 
            bitmap=wx.Bitmap('Bus_AfpX.png'), kind=wx.ITEM_NORMAL)
        ID_RIGHT = wx.NewIdRef()
        tb.AddTool(toolId=ID_RIGHT, label='OK', 
            bitmap=wx.Bitmap('Bus_AfpX.png'), kind=wx.ITEM_NORMAL)
        tb.Realize()
        self.Bind(wx.EVT_TOOL, self.button_push_left, id=ID_LEFT)
        self.Bind(wx.EVT_TOOL, self.button_push_right, id=ID_RIGHT)
        self.SetSize((50, 100))
        self.SetTitle('Frame for test dialog')
        self.Centre()
        self.Show(True)

    def button_push_left(self, event):
        dlg = TestDialog_Ko(parent=None)
        dlg.ShowModal()
        dlg.Destroy()
        
    def button_push_right(self, event):
        dlg = TestDialog_Ok(parent=None)
        dlg.ShowModal()
        dlg.Destroy()

      
 # Main   
if __name__ == "__main__":
    ex = wx.App()
    Example(None)
    ex.MainLoop()    

Your EVT_KILL_FOCUS handler is inhibiting some of the default behaviour of the control. Add event.Skip() to the handler.

1 Like

Thanks!!!
Sometimes the solutions are really simple! As i invented the ‘On_KillFocus’ method in the parent class of all my dialogs, the problem occured really widespread in my code!

By the way: in all other event triggered methods. i have an ‘event.Skip()’ in my code.
Thanks a lot for all, i carried this problem with me now for nerarly 1 1/2 years.

Cheers
Andreas K.