7

is there any way to have an input box inside of an message box opened with the ctypes library? so far I have:

import ctypes
messageBox = ctypes.windll.user32.MessageBoxA
title = 'Title'
text = 'Message box!'
returnValue = messageBox(None, text, title, 0x40 | 0x1)
print returnValue

and this gives a message box with an image icon and two buttons, both of which I know how to change, and it sets a variable "returnValue" to a number representing the button clicked. However, I also need a variable that will set to a string input in the message box. The reason I need this and I can't just do simple a = raw_input('prompt') is that I want the program itself to run in the background (it would launch itself on logon).

Davis Diercks
  • 629
  • 1
  • 10
  • 11

2 Answers2

2

If you want a simple solution, use the PyMsgBox module. It uses Python's built-in tkinter library to create message boxes, including ones that let the user type a response. Install it with pip install pymsgbox.

The documentation is here: https://pymsgbox.readthedocs.org/

The code you want is:

>>> import pymsgbox
>>> returnValue = pymsgbox.prompt('Message box!', 'Title')
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
0

Message box is for messages only. What you need is QDialog. You can create it in QtDesigner(I have login dialog created this way, with 2 QLineEdit for username and pass, 2 buttons in QDialogButtonBox and QCombobox for language choose). You'll get .ui file, which you'll need to convert into .py this way in cmd:

pyuic4 -x YourLoginDialogWindow.ui -o YourLoginDialogWindow.py

import created YourLoginDialogWindow.py and you can use it and implement any method you need:

import YourLoginDialogWindow

class YourLoginDialog(QtGui.QDialog):
    def __init__(self, parent = None):
        super(YourLoginDialog, self).__init__(parent)
        self.__ui = YourLoginDialogWindow.Ui_Dialog()
        self.__ui.setupUi(self)
        ...
        self.__ui.buttonBox.accepted.connect(self.CheckUserCredentials)
        self.__ui.buttonBox.rejected.connect(self.reject)

    def GetUsername(self):
        return self.__ui.usernameLineEdit.text()

    def GetUserPass(self):
        return self.__ui.passwordLineEdit.text()

    def CheckUserCredentials(self):
        #check if user and pass are ok here
        #you can use self.GetUsername() and self.GetUserPass() to get them
        if THEY_ARE_OK :
            self.accept()# this will close dialog and open YourMainProgram in main
        else:# message box to inform user that username or password are incorect
            QtGui.QMessageBox.about(self,'MESSAGE_APPLICATION_TITLE_STR', 'MESSAGE_WRONG_USERNAM_OR_PASSWORD_STR')

in your __main__ first create login dialog and then your main window...

if __name__ == "__main__":
    qtApp = QtGui.QApplication(sys.argv)

    loginDlg = YourLoginDialog.YourLoginDialog()
    if (not loginDlg.exec_()):
        sys.exit(-1)

    theApp = YourMainProgram.YourMainProgram( loginDlg.GetUsername(), loginDlg.GetPassword())
    qtApp.setActiveWindow(theApp)
    theApp.show()
    sys.exit(qtApp.exec_())
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • Wow. That's a lot more complicated than it seems like it should be. Thanks though, I'll try it – Davis Diercks Feb 06 '14 at 16:35
  • That's almost done.. you need to check if username and pass are ok. I've edited code, take a look. QtDesigner is very easy to use, just put two `QLineEdits`(name them `usernameLineEdit` and `passwordLineEdit`) and `QButtonBox` on `QDialog` and follow instructions in my answer.. If you need more details feel free to ask – Aleksandar Feb 07 '14 at 08:21