'QString' object does not support item assignment python

谁都会走 提交于 2020-01-25 06:34:27

问题


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

iam getting this error in if loop, is there any solution? iam new to python any help appreciated....

thank you


回答1:


Your problem is that you cannot change the the QString by item assignment (a[2] = ' '); you get a TypeError whenever you try to do something to an object that isn't allowed. You have to create a new string and assign it to the variable. So, replace the line

self.updateTime[2]= ' '

with the following

self.updateTime = self.updateTime[:2] + ' ' +self.updateTime[3:]


来源:https://stackoverflow.com/questions/23299437/qstring-object-does-not-support-item-assignment-python

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