self.date = QtCore.QDate.currentDate()
self.time = QtCore.QTime.currentTime()
self.updateTime = QtCore.QString(self.time.toString("hh:mm:ss AP"))
if ((self.time.second() % 2) == 0):
self.updateTime[2]= ' '
self.label.setText(self.updateTime)
Error: TypeError: file line 54: 'QString' object does not support item assignment
我在if循环中遇到这个错误,有什么解决办法吗?我是Python新手,如有任何帮助,表示赞赏....
谢谢
最佳答案
您的问题是您无法通过项目分配更改QString
(a[2] = ' '
);每当您尝试对对象执行不允许的操作时,您都会收到 TypeError
。您必须创建一个新字符串并将其分配给变量。因此,替换该行
self.updateTime[2]= ' '
具有以下内容
self.updateTime = self.updateTime[:2] + ' ' +self.updateTime[3:]
关于python - ‘QString’对象不支持项目分配python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23299437/