未知错误 : argument 1 has unexpected type ‘numpy.float64‘ 解决办法

        如题,今天在用pyqt5写项目,到最后一步输出结果的时候发现QLabel组件setText()失败,既然print能打出来为什么QLabel打不出来呢??


具体报错为:

未知错误 setText(self, str): argument 1 has unexpected type 'numpy.float64'

 

搞了半天才知道是float64写不进去,数据格式有问题。

解决办法:直接把float64强制转为str型的即可解决。

代码示例:

报错代码:

    def display_event(self):
        #结果打印区
        try:
            print(self.lists[0])
            print(self.lists[1])
            print(self.lists[2])
            self.label_value1.setText(self.lists[0])#平均值
            self.label_value1_2.setText(self.lists[1])#标准不确定度
            self.label_value1_3.setText(self.lists[2])#扩展不确定度
            self.label_value1_4.setText(0)#置信区间上端点
            self.label_value1_5.setText(0)#置信区间下断点
        except Exception as e:
            print("未知错误 %s "% e)

正确代码: 

    def display_event(self):
        #结果打印区
        try:
            print(self.lists[0])
            print(self.lists[1])
            print(self.lists[2])
            self.label_value1.setText(str(self.lists[0]))#平均值
            self.label_value1_2.setText(str(self.lists[1]))#标准不确定度
            self.label_value1_3.setText(str(self.lists[2]))#扩展不确定度
            self.label_value1_4.setText(0)#置信区间上端点
            self.label_value1_5.setText(0)#置信区间下断点
        except Exception as e:
            print("未知错误 %s "% e)


 

反思总结:

 

1.要善用try-catch:之前一直没有try-catch,每运行到那个地方就直接闪退,没有给报错信息,导致我像个无头苍蝇一样乱搜,结果也没搜到什么有用的信息。

2.写数据的时候要注意数据格式的转换。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值