python - pyqt : Variable values in QMessageBox output?

标签 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)

我想在性别和年龄之后包含某些变量的值,但我对包含变量值的语法感到好奇。我要先将它们转换为字符串吗?由于 .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/

相关文章:

python - Pandas :按每组中的复杂条件选择子数据集

string - R - 仅绘制文本

string - 创建字符串元胞数组 matlab

javascript - 字符串数组中的制表符补全

c++ - 如何更改 QMessageBox 图标和标题

python - QtGui.QMessageBox.information 和自定义

qt - 如何在 Qt 中更改 QMessageBox 的字体?

python - 如何使用 Django 管理面板实现 Semantic-UI

python - PyQt QVBoxLayout 和缺少的小部件?

python - 如何将具有自定义 keras 层(.h5)的 keras 模型卡住为 tensorflow 图(.pb)?