是否有任何方法可以在使用CTYPES库打开的消息框内将输入框?到目前为止,我有:
import ctypes
messageBox = ctypes.windll.user32.MessageBoxA
title = 'Title'
text = 'Message box!'
returnValue = messageBox(None, text, title, 0x40 | 0x1)
print returnValue
,这给出了一个带有图像图标和两个按钮的消息框,我都知道如何更改,并且它将可变的" returnValue"设置为代表单击按钮的数字。但是,我还需要一个将设置为消息框中的字符串输入的变量。我需要这个的原因,我不能仅仅做简单的a = raw_input('提示')是因为我希望程序本身在后台运行(它将在logon上启动)。
如果您想要一个简单的解决方案,请使用Pymsgbox模块。它使用Python的内置TKINTER库来创建消息框,包括让用户键入响应的消息框。使用pip install pymsgbox
。
文档在这里:https://pymsgbox.readthedocs.org/
您想要的代码是:
>>> import pymsgbox
>>> returnValue = pymsgbox.prompt('Message box!', 'Title')
消息框仅适用于消息。您需要的是QDialog
。您可以在qtdesigner中创建它(我有以用户名和通过的2个 QLineEdit
创建的登录对话框, QDialogButtonBox
中的2个按钮, QCombobox
用于语言选择)。您将获得.ui
文件,您需要在CMD中以这种方式转换为.py
:
pyuic4 -x YourLoginDialogWindow.ui -o YourLoginDialogWindow.py
导入创建的YourLoginDialogWindow.py
,您可以使用它并实现您需要的任何方法:
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')
在您的__main__
中,首先创建登录对话框,然后创建主窗口...
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_())