pyqt: Variable values in QMessageBox output?

有些话、适合烂在心里 提交于 2019-12-24 12:15:36

问题


Right now I'm displaying a window with text in a QMessageBox. It works and displays the text accurately.

 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)

I would like to include the value of certain variables to put after Gender & Age but I am curious about the syntax for including variable values. Do I convert them to strings first? How do I include them since an .about box can only take a maximum of three arguments?

Thank you!


回答1:


Use str.format:

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

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

or use % operator:

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


来源:https://stackoverflow.com/questions/22139023/pyqt-variable-values-in-qmessagebox-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!