pyqt:QMessageBox输出中的变量值?

时间:2014-03-03 05:04:23

标签: python string 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)

我想在Gender&amp; amp;之后加入某些变量的值。年龄,但我很好奇包含变量值的语法。我首先将它们转换为字符串吗?如何包含它们,因为.about框最多只能包含三个参数?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用str.format

>>> gender = 'M'
>>> age = 33

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

或使用% operator

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