我正在尝试从我创建和解析的标签中获取字符串值,或者将其转换为整数,以便我可以将+ 1添加到标签的值,然后将其设置回标签。例如:有一个标签上写着“4”按一个按钮增加它然后会说“5”。谢谢!
self.lblCam1Focus = QtGui.QLabel("4",self)
self.lblCam1Focus.move(50,60)
def cam1IncreaseFocus(self):
text = self.lblCam1Focus.text
n = text
n = n + 1
self.lblCam1Focus.setText(n)
答案 0 :(得分:1)
TypeError: QLabel.setText(QString): argument 1 has unexpected type 'int'
清楚地说明传递的参数是unexpected type
。接下来你应该考虑的是,QString
Python字符串或unicode对象,QLatin1String或QChar可能是 在预期的QString时使用。
来自http://pyqt.sourceforge.net/Docs/PyQt4/qstring.html
所以,
def cam1IncreaseFocus(self):
num_text = self.lblCam1Focus.text()
num = int(num_text) + 1
self.lblCam1Focus.setText(str(num))