pyqt:QMessageBox 输出中的变量值



现在我在QMessageBox中显示一个带有文本的窗口。它可以工作并准确显示文本。

 profBox = QMessageBox()
 QMessageBox.about(self,'Profile', "Gender: <br /> Age: < br />") #Ideal output is gender: F (stored in a variable) and Age: X (also stored in a variable)

我想在性别和年龄之后包含某些变量的值,但我对包含变量值的语法感到好奇。我先将它们转换为字符串吗?由于 .about 框最多只能接受三个参数,我如何包含它们?

谢谢!

使用 str.format

>>> gender = 'M'
>>> age = 33
>>> "Gender: {}<br /> Age: {}< br />".format(gender, age)
'Gender: M<br /> Age: 33< br />'

或使用%运算符:

>>> "Gender: %s<br /> Age: %s< br />" % (gender, age)
'Gender: M<br /> Age: 33< br />'

最新更新