mgr*_*oat 4 python tkinter simpledialog
在下面的代码中,第一个对话框立即获得焦点,因此用户只需键入答案并按 Enter。在第二个中,在 Windows 中运行时似乎不会发生这种情况。运行 Raspbian 9,两个窗口在打开时都会获得焦点。
有什么办法可以让两个窗口在 Windows 中打开时都获得焦点?
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
root.withdraw()
answer1 = simpledialog.askstring("Test1","This one gets focus when it opens",parent=root)
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
Run Code Online (Sandbox Code Playgroud)
我已经看了这个问题几天了,希望有人能对这个问题有所了解。我在 Windows 10 下运行 Python 3.6.5 并遇到同样的问题。
我尝试了几种不同的方法,但似乎微软以自己的方式做事。我终于找到了一个有用的东西,但前提是你不隐藏根窗口:
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
#root.withdraw() # This does not work if you hide the root window
root.update_idletasks()
answer1 = simpledialog.askstring("Test1","This one gets focus",parent=root)
root.update_idletasks()
answer2 = simpledialog.askstring("Test2","This one doesn't",parent=root)
Run Code Online (Sandbox Code Playgroud)