'QString'对象不支持项目分配python [英] 'QString' object does not support item assignment python

查看:134
本文介绍了'QString'对象不支持项目分配python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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循环中遇到此错误,有什么解决方法吗? iam是python的新手,不胜感激....

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

谢谢

推荐答案

您的问题是您不能通过项目分配(a[2] = ' ')更改QString;每当尝试对不允许的对象执行操作时,都会得到TypeError.您必须创建一个新字符串并将其分配给变量.因此,替换行

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]= ' '

带有以下内容

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

这篇关于'QString'对象不支持项目分配python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆