python - 将 Python gui 窗口放在前面?
问题描述
我正在尝试使用 Python 的 gui 模块之一来检索用户的 Twitter 用户名和密码:
这是我的第一次尝试(使用easygui):
import easygui
username = easygui.enterbox("Enter your Twitter username")
password = easygui.passwordbox("Enter your Twitter password")
这是我的第二次尝试(使用 tkinter):
import tkinter as tk
from tkinter import simpledialog
application_window = tk.Tk()
application_window.attributes("-topmost", True)
username = simpledialog.askstring("Input", "What is your Twitter username?")
password = simpledialog.askstring("Input", "What is your Twitter password?", show="*")
application_window.destroy()
目前,这两个 gui 都不会自动出现。相反,我的 Windows 任务栏上会出现一个 Python 图标,我必须单击该图标才能显示 gui。是否有任何编程方式可以使 gui 自动弹出?或者也许我可以使用另一个模块来实现这一点?
解决方案
simpledialog 模块创建了一个新的 Toplevel 窗口,这是您想要提出的那个,而不是根窗口。
simpledialog
当您已经运行 tkinter GUI 时效果最佳。我认为你会更好easygui
,因为它实际上使用了一个 tkinter Tk 实例:
import easygui
fieldNames = ["Username", "Password"]
values = easygui.multpasswordbox("Enter Twitter information", "Input", fieldNames)
if values:
username, password = values
else:
# user pushed "Cancel", the esc key, or Xed out the window
username, password = None, None
print(username, password)
如果这不起作用,easygui 可以使用最上面的技巧:
import easygui
fieldNames = ["Username", "Password"]
mb = easygui.multpasswordbox("Enter Twitter information", "Input", fieldNames, run=False)
mb.ui.boxRoot.attributes("-topmost", True)
mb.run()
if mb.values:
username, password = mb.values
else:
# user pushed "Cancel", the esc key, or Xed out the window
username, password = None, None
print(username, password)
或者只是制作自己的对话。
推荐阅读
- python - 在 tkinter 中调整窗口大小时,如何使屏幕上的框架和按钮动态变化?
- python - 如何将灰度图像变成RGB?
- javascript - 图像不固定
- python - 标签文本未出现在 tkinter 框架中
- r - 有没有办法在同一个图中绘制不同的回归模型?
- nginx - Django - 如何添加/允许 url 参数?
- android - 清单合并失败:需要明确指定 android:exported
即使我的 AndroidManifest.xml 文件中没有服务 - reactjs - Redux : 两个非常相似的命令,一个进入 store,另一个不进入
- c# - 在 Unity 中通过按键淡化图像 UI
- wordpress - Wordpress 不返回某些管理页面(如 post.php)的内容