Py学习  »  Python

Tkinter SimpleDialog Box无法在使用python3的Windows 10中获得焦点

mgroat • 5 年前 • 1455 次点击  

在下面的代码中,第一个对话框立即获得焦点,因此用户可以键入答案并按Enter。在第二个版本中,在windows中运行时似乎不会发生这种情况。运行Raspbian9,两个窗口打开时都会聚焦。

有没有什么方法可以让两个窗口在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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43593
 
1455 次点击  
文章 [ 1 ]  |  最新文章 5 年前
figbeam
Reply   •   1 楼
figbeam    6 年前

我已经关注这个问题好几天了,希望有人能对这个问题有所启发。我在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)