0

私はTkinterを使用して2つのウィンドウを構築しています。主なものの1つであるボタンを押すと、2番目のウィンドウが作成されます。

この2番目のウィンドウは、作成時にすぐにはフォーカスされません。.focus_force()を呼び出すことで修正できます。ただし、tkFileDialogからaskdirectory()関数を呼び出すと、フォーカスが最初のウィンドウに戻ります。

どこでもfocus_force()を呼び出すだけで、フォーカスの切り替えが発生しないようにするにはどうすればよいですか?

問題を再現するには:

from Tkinter import *
from tkFileDialog import *

class app:
    def __init__(self, master):
        Button(master, command = make_new).grid()
    def make_new(self):
        root = Tk()
        new = new_win(root)
        root.mainloop() #here the focus is on the first window
class new_win:
    def __init__(self, master):
        f = askdirectory() #even after placing focus on second window,
                           #focus goes back to first window here

Python2.7.3を使用しています。ありがとう!

4

1 に答える 1

0

少し文書化されたwm_attributesメソッドが役立つかもしれません:

from Tkinter import *
import tkFileDialog

root = Tk()

top = Toplevel()
top.wm_attributes('-topmost', 1)
top.withdraw()
top.protocol('WM_DELETE_WINDOW', top.withdraw)

def do_dialog():
    oldFoc = top.focus_get()
    print tkFileDialog.askdirectory()    
    if oldFoc: oldFoc.focus_set()

b0 = Button(top, text='choose dir', command=do_dialog)
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()
于 2012-07-23T02:56:14.837 に答える