Tkinter Focus_set And Focus_force Not Working As Expected
I am attempting to have the Entry field to have focus when a new page opens up: import tkinter as tk from tkinter import * from tkinter import ttk class DIS(tk.Tk): def __ini
Solution 1:
It seems like tkraise()
messes up the focus. So you need to invoke it after you've raised the page into view. I'd update you framework to always call some method after tkraise like so:
import tkinter as tk
from tkinter import *
from tkinter import ttk
classDIS(tk.Tk):
def__init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="")
tk.Tk.wm_title(self, "program")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (startPage, contactQues):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(startPage)
defshow_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.postupdate()
classstartPage(tk.Frame):
def__init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button2 = ttk.Button(self, text = "Here's a Button",
command=lambda: controller.show_frame(contactQues))
button2.pack()
defpostupdate(self):
passclasscontactQues(tk.Frame):
def__init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.entry = Entry(self)
self.entry.pack()
defpostupdate(self):
self.entry.focus()
app = DIS()
app.mainloop()
If you want to avoid having the postupdate()
method where it's not needed, you could check to see if it exists in the class before trying to run it. Like so:
defshow_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
try:
frame.postupdate()
except AttributeError:
pass
Solution 2:
So after a lot of searching I got the solution to this issue.
Just add the following line to the end of your show_frame
function and remove the postupdate
calls:
return'break'
It disables the event propagation to the other frames under the raised one. The solution posted by anderswb didn't work for me.
Post a Comment for "Tkinter Focus_set And Focus_force Not Working As Expected"