[Tkinter-discuss] Need help on gui focus l

Michael Lange klappnase at web.de
Wed Oct 14 21:03:16 CEST 2009


Hi,

On Tue, 13 Oct 2009 23:14:35 -0400
BlackKnight <wwwwar3com at gmail.com> wrote:

> Hi folks,
> 
> I am using Tkinter to do a simple GUI for my program.
> 
> It contains A MainGui and A ChildGui:
> A button on the MainGUI will popout the childgui.
> On the childgui there is a browse button which popout a dialog to
> allow user to browser and choose a file.
> 
> I have no problem at doing them.
> 
> The problem is after the uesr choose a file the focus will return to
> the MainGui intead of the childgui. The child gui just go hide behind
> the Main.
> 
> I've tried focus_set, focus_force but none of them seem work.
> 
> I did some research but found nothing working. It's not a big problem,
> but is really really annoying.
> (Actually I notice the examples from some python book I am reading has
> the exact same problem. The author even tried to spawn the child gui
> away from the main one, so when the focus return from the browse
> dialog , the maingui won't block the child one~~ Is that how tkinter
> should work?)
> 

I cannot reproduce this behavior here (linux), maybe it is a platform
specific problem ? I tried the follwing which works for me as expected:

### CODE ###
from Tkinter import *
import tkFileDialog

root = Tk()

top = Toplevel()
top.withdraw()
top.protocol('WM_DELETE_WINDOW', top.withdraw)

def filedialog():
    print tkFileDialog.askopenfilename()

b0 = Button(top, text='filedialog', command=filedialog)
b0.pack(padx=100, pady=100)

def popup():
    top.deiconify()
    b0.focus_set()

b1 = Button(root, text='popup', command=popup)
b1.pack(padx=100, pady=100)
root.mainloop()
### CODE ###

In case I just did not understand exactly your problem,
could you post a short code snippet here that shows
the problem and tell us which platform you are using, please ?

Michael



More information about the Tkinter-discuss mailing list