获取标签中的字符串值,然后解析为整数,pyqt4

2024-10-01 11:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从我创建并解析的标签中获取一个字符串值,或者将它转换成一个整数,这样我就可以在标签的值上加上+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)

Tags: 字符串textselfmovedef整数标签按钮
1条回答
网友
1楼 · 发布于 2024-10-01 11:29:46

TypeError: QLabel.setText(QString): argument 1 has unexpected type 'int'

明确声明传递的参数是unexpected type。所以接下来您应该考虑的是,什么类型对QString重要

A Python string or unicode object, a QLatin1String or a QChar may be used whenever a QString is expected.

来自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))

相关问题 更多 >