如何在pyqt MessageBox中使用变量
How to use variable in pyqt MessageBox
如果要将数据添加到数据库,我正在使用 MessageBox 获取用户输入,但我不知道如何将我的变量放入实际消息中。 MessageBox 函数如下所示:
def message(self, par_1, par_2):
odp = QMessageBox.question(self, 'Information', "No entry. Would you like to add it to DB?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if odp == QMessageBox.Yes:
return True
else:
return False
函数是这样调用的:
self.message(autor_firstname, autor_lastname)
我尝试添加:
odp.setDetailText(par_1, par_2)
但它没有按预期工作。此外,当用户单击 "No" 时,我遇到了问题。程序崩溃而不是返回主 window.
根据 the docs on QMessageBox::question, the return value is the button that was clicked. Using the static method QMessageBox::question
limits how you can customize the QMessageBox
. Instead, create an instance of QMessageBox
explicitly, call setText
, setInformativeText
, and setDetailedText
as needed. Note that your arguments also do not match what is needed for setDetailedText
. Those docs are here。我认为您的代码应该看起来像这样。
def message(self, par_1, par_2):
# Create the dialog without running it yet
msgBox = QMessageBox()
# Set the various texts
msgBox.setWindowTitle("Information")
msgBox.setText("No entry. Would you like to add it to the database")
detail = "%s\n%s" % (par_1, par_2) # formats the string with one par_ per line.
msgBox.setDetailedText(detail)
# Add buttons and set default
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
# Run the dialog, and check results
bttn = msgBox.exec_()
if bttn == QMessageBox.Yes:
return True
else:
return False
这也可以解决你的问题:
def message(self, par_1, par_2):
# Create the dialog without running it yet
msgBox = QMessageBox()
# Set the various texts
msgBox.setWindowTitle("Information")
msgBox.setText("No entry for '"+str(par_1)+" "+str(par_2)+"'.Would you like to add it to the database")
# Add buttons and set default
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
# Run the dialog, and check results
bttn = msgBox.exec_()
if bttn == QMessageBox.Yes:
return True
else:
return False
第 6 行没有使用字符串连接。
如果要将数据添加到数据库,我正在使用 MessageBox 获取用户输入,但我不知道如何将我的变量放入实际消息中。 MessageBox 函数如下所示:
def message(self, par_1, par_2):
odp = QMessageBox.question(self, 'Information', "No entry. Would you like to add it to DB?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if odp == QMessageBox.Yes:
return True
else:
return False
函数是这样调用的:
self.message(autor_firstname, autor_lastname)
我尝试添加:
odp.setDetailText(par_1, par_2)
但它没有按预期工作。此外,当用户单击 "No" 时,我遇到了问题。程序崩溃而不是返回主 window.
根据 the docs on QMessageBox::question, the return value is the button that was clicked. Using the static method QMessageBox::question
limits how you can customize the QMessageBox
. Instead, create an instance of QMessageBox
explicitly, call setText
, setInformativeText
, and setDetailedText
as needed. Note that your arguments also do not match what is needed for setDetailedText
. Those docs are here。我认为您的代码应该看起来像这样。
def message(self, par_1, par_2):
# Create the dialog without running it yet
msgBox = QMessageBox()
# Set the various texts
msgBox.setWindowTitle("Information")
msgBox.setText("No entry. Would you like to add it to the database")
detail = "%s\n%s" % (par_1, par_2) # formats the string with one par_ per line.
msgBox.setDetailedText(detail)
# Add buttons and set default
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
# Run the dialog, and check results
bttn = msgBox.exec_()
if bttn == QMessageBox.Yes:
return True
else:
return False
这也可以解决你的问题:
def message(self, par_1, par_2):
# Create the dialog without running it yet
msgBox = QMessageBox()
# Set the various texts
msgBox.setWindowTitle("Information")
msgBox.setText("No entry for '"+str(par_1)+" "+str(par_2)+"'.Would you like to add it to the database")
# Add buttons and set default
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
msgBox.setDefaultButton(QMessageBox.No)
# Run the dialog, and check results
bttn = msgBox.exec_()
if bttn == QMessageBox.Yes:
return True
else:
return False
第 6 行没有使用字符串连接。