[Tkinter-discuss] Transient, grab and focus problems...
Vasilis Vlachoudis
Vasilis.Vlachoudis at cern.ch
Tue May 20 15:20:48 CEST 2014
Dear all
in my application I have some toplevels that appear as InPlaceEdit for Listboxes when the user
clicks a field in a listbox.
This time I've tried to create a 2 nested level toplevel, but I am completely losing the focus on the
return and I didn't find any way to restore it.
Bellow there is a small example just to demonstrate the problem
By clicking the button it opens a toplevel, transient, overrideredirect(1) with a Text() which
has the grab and the focus.
Once you press a certain key (now the +) it opens a second level, another toplevel of a listbox with override and grab
On return from the second toplevel the focus is completly lost.
I cannot restore the focus on the text, I've tried to use focus_set(), grab_set etc.. but with no success.
Any ideas? Where did the focus go and how why the focus_set() doesn't work
Thanks in advance
Vasilis
from Tkinter import *
# ========================================================
class TopList(Toplevel):
def __init__(self, master):
Toplevel.__init__(self, master)
self.overrideredirect(1)
self.transient(master)
self.bind("<Escape>", self.close)
self.l = Listbox(self, bg="Yellow")
self.l.pack()
self.l.bind("<ButtonRelease-1>", self.close)
for item in ["one","two","three","four"]: self.l.insert(END,item)
self.result = ""
# ------------------------------------------------
def show(self):
self.deiconify()
self.wait_visibility()
self.grab_set()
self.l.focus_set()
self.wait_window()
self.grab_release()
return self.result
# ------------------------------------------------
def close(self, event=None):
if event:
self.result = self.l.get(self.l.nearest(event.y))
else:
self.result = self.l.get(ACTIVE)
self.destroy()
# ========================================================
class TopText(Toplevel):
def __init__(self, master):
Toplevel.__init__(self, master)
self.overrideredirect(1)
self.transient(master)
self.bind("<Escape>", self.close)
self.t = Text(self, bg="White")
self.t.pack()
self.t.bind("<Key-plus>", self.openList)
self.result = ""
# ------------------------------------------------
def show(self):
self.deiconify()
self.wait_visibility()
self.grab_set()
self.t.focus_set()
self.wait_window()
return self.result
# ------------------------------------------------
def close(self, event=None):
self.result = self.t.get(1.0,END)
self.destroy()
# ------------------------------------------------
def openList(self, event=None):
self.grab_release()
result = TopList(self).show()
self.t.focus_set()
self.grab_set()
self.t.insert("%s + 1 chars"%(INSERT),result)
# ========================================================
def openText():
print TopText(tk).show()
tk = Tk()
e = Entry(tk)
e.pack()
e.focus_set()
b = Button(tk, text="Text", command=openText)
b.pack()
tk.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140520/eaca95bc/attachment.html>
More information about the Tkinter-discuss
mailing list