From msa01 at bitflipper.ca Wed Jan 10 07:44:07 2024 From: msa01 at bitflipper.ca (Cam Farnell) Date: Wed, 10 Jan 2024 08:44:07 -0400 Subject: [Tkinter-discuss] Focus issue In-Reply-To: <0BC70B5D93E054469872FFD0FE07220E033860E8C2@CERNXCHG53.cern.ch> References: <0BC70B5D93E054469872FFD0FE07220E033860D835@CERNXCHG53.cern.ch> <0BC70B5D93E054469872FFD0FE07220E033860E8C2@CERNXCHG53.cern.ch> Message-ID: In a rather large program I have this code: self.TextWidget.focus_set() print("type(self.TextWidget)=%s"%type(self.TextWidget)) print('Focus_get()=%s'%self.focus_get()) The goal is to have TextWidget get the focus. Usually it does, but there is one particular circumstance when it does not get the focus. The "print" lines I added for debugging. The output from the print functions is: type(self.TextWidget)= Focus_get()=None TextWidget is an instance of rpWidgets.ColorizedText which is created thus: class ColorizedText(tkinter.Text): which is to say, it's just a tkinter.Text to which I've added methods. I've tried creating a minimum example without success. My questions is: how is it possible for TextWidget to NOT have the focus immediately after a call to focus_set? Changing focus_set to focus_force makes no difference. Invoking update_idletasks() after the focus_set makes no difference. The TextWidget in question is visible on the screen, and pressing Tab a couple of times does get it the focus. Having spent some hours chasing this I'm a tad frustrated, but quite open to suggestions about what to look for. Cheers Cam Farnell From msa01 at bitflipper.ca Wed Jan 10 15:33:46 2024 From: msa01 at bitflipper.ca (Cam Farnell) Date: Wed, 10 Jan 2024 16:33:46 -0400 Subject: [Tkinter-discuss] Focus issue In-Reply-To: References: <0BC70B5D93E054469872FFD0FE07220E033860D835@CERNXCHG53.cern.ch> <0BC70B5D93E054469872FFD0FE07220E033860E8C2@CERNXCHG53.cern.ch> Message-ID: To answer my own question: the focus was being given to another widget after the code in question had executed. That said, I still don't understand why focus_set followed by update_idletasks followed by focus_get would return None, but tkinter something works in strange and mysterious ways. Cheers Cam On 2024-01-10 08:44, Cam Farnell wrote: > In a rather large program I have this code: > > self.TextWidget.focus_set() > print("type(self.TextWidget)=%s"%type(self.TextWidget)) > print('Focus_get()=%s'%self.focus_get()) > > The goal is to have TextWidget get the focus. Usually it does, but there is one particular circumstance when it does not get the focus. The "print" lines I added for debugging. > > The output from the print functions is: > > type(self.TextWidget)= > Focus_get()=None > > TextWidget is an instance of rpWidgets.ColorizedText which is created thus: > > class ColorizedText(tkinter.Text): > > which is to say, it's just a tkinter.Text to which I've added methods. > > I've tried creating a minimum example without success. > > My questions is: how is it possible for TextWidget to NOT have the focus immediately after a call to focus_set? > > Changing focus_set to focus_force makes no difference. > > Invoking update_idletasks() after the focus_set makes no difference. > > The TextWidget in question is visible on the screen, and pressing Tab a couple of times does get it the focus. > > Having spent some hours chasing this I'm a tad frustrated, but quite open to suggestions about what to look for. > > Cheers > > Cam Farnell > > > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss at python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss From klappnase at web.de Wed Jan 10 18:51:53 2024 From: klappnase at web.de (Michael Lange) Date: Thu, 11 Jan 2024 00:51:53 +0100 Subject: [Tkinter-discuss] Focus issue In-Reply-To: References: <0BC70B5D93E054469872FFD0FE07220E033860D835@CERNXCHG53.cern.ch> <0BC70B5D93E054469872FFD0FE07220E033860E8C2@CERNXCHG53.cern.ch> Message-ID: <20240111005153.6382076893c9204de13c6aef@web.de> Hi, On Wed, 10 Jan 2024 16:33:46 -0400 Cam Farnell wrote: > To answer my own question: the focus was being given to another widget > after the code in question had executed. > > That said, I still don't understand why focus_set followed by > update_idletasks followed by focus_get would return None, but tkinter > something works in strange and mysterious ways. > I can only guess here, have you tried calling update_idletasks() (or even update()) *before* calling focus_set() ? Best regards, Michael