我正在尝试使用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都不自动显示。而是,Python图标出现在Windows任务栏上,我必须单击该图标以使gui出现。有什么编程方法可以使gui自动弹出?也许还有另一个模块可以用来实现这一目标?
答案 0 :(得分:3)
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)
或只创建自己的对话框。
答案 1 :(得分:0)
您可以使用PySimpleGUI轻松完成此操作。有两种方法。一种是内置函数PopupGetText。这将创建2个窗口,一个用于用户名,一个用于密码。或者,您可以创建自己的自定义布局,在单个窗口中同时要求两者。
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()