auser1
(auser1)
October 8, 2023, 3:01pm
1
Here is an example showing how a window lost its focus after some codes. Is it a bug or normal phenomenon?
Thanks anyone’s help.
Plus, my environment: win10, tkinter8.6.12 & tkinter 8.6.13
import tkinter
from tkinter import ttk
root = tkinter.Tk()
entry = ttk.Entry(root)
entry.grid()
entry.focus_set()
root.state('withdrawn')
root.update()
root.resizable(False, False)
root.resizable(True, True)
root.state('normal')
root.mainloop()
I ran it in IDLE, the window created didn’t get focus.
MRAB
(Matthew Barnett)
October 8, 2023, 3:55pm
2
I don’t know whether it’s a bug, but I don’t understand why you’re doing this:
root.state('withdrawn')
root.update()
root.resizable(False, False)
root.resizable(True, True)
root.state('normal')
kyle
(Michael Mmbaya)
October 8, 2023, 4:45pm
3
it’s not a bug and why would you do that?.. what @MRAB is curious about. There’s alot going on here you are reducing the window size, resizing then not resizing and then returning to normal … what was your aim?
tjreedy
(Terry Jan Reedy)
October 8, 2023, 5:15pm
4
I am pretty sure that withdrawing root and making it invisible necessarily gives up focus.
auser1
(auser1)
October 9, 2023, 7:01am
5
It is a condensed code to concentrate on problem. Of course the actual codes doesn’t look like this, but they are the same essentially.
auser1
(auser1)
October 9, 2023, 7:04am
6
Thank you for letting me know that withdrawing and making it visible gives up focus, then how do I make it get focus again?
tjreedy
(Terry Jan Reedy)
October 9, 2023, 11:56am
7
Get rid of the unneeded root calls or more entry.focus_set()
.
auser1
(auser1)
October 10, 2023, 10:04am
9
More ‘entry.focus_set()’ doesn’t work. Now I use ‘iconic’ instead of ‘withdrawn’ cooperating with ‘entry.focus_force’, it works well.