把 Python gui window 放到前面?
Bring Python gui window to front?
我正在尝试使用 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()
目前,这两个界面都不会自动出现。相反,Python 图标出现在我的 Windows 任务栏上,我必须单击该图标才能显示 gui。有什么编程方式可以让 gui 自动弹出吗?或者我可以使用另一个模块来实现此目的?
simpledialog 模块创建了一个新的 Toplevel window,这是您要提出的那个,而不是根 window。
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)
或者制作您自己的对话框。
您可以使用 PySimpleGUI 轻松完成。有几种方法。一个是内置函数 PopupGetText。这将创建 2 windows,一个用于用户名,一个用于密码。或者,您可以创建自己的自定义布局,在单个 window.
中请求两者
import PySimpleGUI as sg
username = sg.PopupGetText('Enter your username')
password = sg.PopupGetText('Entery your password', password_char='*')
layout = [[sg.Text('Username'), sg.Input()],
[sg.Text('Password'), sg.Input(password_char='*')],
[sg.OK()]]
button, (username, password) = sg.Window('Login').Layout(layout).Read()
我正在尝试使用 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()
目前,这两个界面都不会自动出现。相反,Python 图标出现在我的 Windows 任务栏上,我必须单击该图标才能显示 gui。有什么编程方式可以让 gui 自动弹出吗?或者我可以使用另一个模块来实现此目的?
simpledialog 模块创建了一个新的 Toplevel window,这是您要提出的那个,而不是根 window。
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)
或者制作您自己的对话框。
您可以使用 PySimpleGUI 轻松完成。有几种方法。一个是内置函数 PopupGetText。这将创建 2 windows,一个用于用户名,一个用于密码。或者,您可以创建自己的自定义布局,在单个 window.
中请求两者import PySimpleGUI as sg
username = sg.PopupGetText('Enter your username')
password = sg.PopupGetText('Entery your password', password_char='*')
layout = [[sg.Text('Username'), sg.Input()],
[sg.Text('Password'), sg.Input(password_char='*')],
[sg.OK()]]
button, (username, password) = sg.Window('Login').Layout(layout).Read()