0

I'm trying to use one of Python's gui modules to retrieve a user's Twitter username and password:

Here is my first attempt (using easygui):

import easygui
username = easygui.enterbox("Enter your Twitter username")
password = easygui.passwordbox("Enter your Twitter password")

Here is my second attempt (using 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()

Currently, the neither gui appears automatically. Rather, a Python icon appears on my Windows taskbar and I have to click the icon to make the gui appear. Is there any programmatic way to make the gui automatically pop up? Or perhaps there's another module I can use to achieve this?

ElsaInSpirit
  • 341
  • 6
  • 16

2 Answers2

3

The simpledialog module makes a new Toplevel window, which is the one you want to bring forward, not the root window.

simpledialog works best when you already have a tkinter GUI running. I think you would be a lot better off with easygui, since it actually uses a tkinter Tk instance:

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)

If that does not work, easygui has the hook to use the topmost trick:

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)

or just make your own dialog.

Novel
  • 13,406
  • 2
  • 25
  • 41
  • thank you for this. unfortunately, I'm having the same problem with easygui. instead of popping up automatically, a Python logo appears on the task bar and I have to click on it to open the dialog. – ElsaInSpirit Jun 27 '18 at 16:52
1

You can do it quite easily using PySimpleGUI. There are a couple of ways. One is the builtin function PopupGetText. This will create 2 windows, one for username, one for password. Or, you can create your own custom layout that asks for both in a single 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()

Using 2 input windows

Using 2 input windows

Using a custom GUI

Mike from PSG
  • 5,312
  • 21
  • 39