现在我正在显示一个窗口,其中包含 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 />'
或使用%
operator :
>>> "Gender: %s<br /> Age: %s< br />" % (gender, age)
'Gender: M<br /> Age: 33< br />'
关于python - pyqt : Variable values in QMessageBox output?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22139023/