Python askdirectory()将焦点更改为不同的窗口
我正在使用Tkinter构建两个窗口。一个是主窗口,按下按钮可创建第二个窗口 第二个窗口在创建时不会立即获得焦点。我可以通过调用.focus\u force()来修复。但是,当我从tkFileDialog调用askdirectory()函数时,焦点会变回第一个窗口 我如何防止焦点切换发生,而不只是到处调用focus_force() 要复制问题,请执行以下操作:Python askdirectory()将焦点更改为不同的窗口,python,focus,tkinter,Python,Focus,Tkinter,我正在使用Tkinter构建两个窗口。一个是主窗口,按下按钮可创建第二个窗口 第二个窗口在创建时不会立即获得焦点。我可以通过调用.focus\u force()来修复。但是,当我从tkFileDialog调用askdirectory()函数时,焦点会变回第一个窗口 我如何防止焦点切换发生,而不只是到处调用focus_force() 要复制问题,请执行以下操作: from Tkinter import * from tkFileDialog import * class app: def
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
我正在使用Python 2.7.3。谢谢 很少有文档记录的wm_属性方法可能有助于:
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()