【问题标题】:TypeError: setText(self, str): argument 1 has unexpected type 'tuple'TypeError:setText(self,str):参数 1 具有意外类型“元组”
【发布时间】:2018-08-02 09:01:12
【问题描述】:

我创建了一个代码,当点击“加载”时,用户将上传一个 .png 图像,opencv 将执行 houghcircle 并计算圈数。

和计数,将显示在文本标签上。 circles.shape 会导致 (1, 99, 3),我想在 textlabel 上显示 99,甚至是整个 (1, 99, 3)。

问题是,上传图片后出现此错误

Traceback(最近一次调用最后一次): 文件“try.py”,第 58 行,在浏览中 self.label_2.setText(circles.shape) TypeError: setText(self, str): argument 1 has unexpected type 'tuple'

这是我的代码:

def Browse(self):
    filter = "Images (*.png)"
    fname, _ = QFileDialog.getOpenFileName(self, "Open Image", "Desktop", filter)
    print(fname)
    self.scene = QGraphicsScene()
    self.scene.addPixmap(QPixmap(fname))
    self.graphicsView_2.setScene(self.scene)

    img = cv2.imread(fname,0)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

    circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,5,
                        param1=200,param2=8,minRadius=0,maxRadius=7)

    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),1)
    numberofcells = print(circles.shape)
    self.label_2.setText(circles.shape) 

任何帮助将不胜感激。非常感谢!

【问题讨论】:

  • shape 返回一个元组(高度、宽度、通道),而不是 str。您不能使用元组作为参数调用 setText

标签: python python-3.x opencv pyqt pyqt5


【解决方案1】:

setText() 需要一个字符串,但您传递的是一个元组,一个可能的解决方案是:

self.label_2.setText(str(circles.shape)) 

【讨论】:

  • circles.shape 是元组,正如@eibersji 所说,setText() 需要字符串。如果您想将其显示为此元组的字符串表示形式,请使用str(circles.shape),并且如果您只想要该元组的第二个数字,请使用str(circles.shape[1]),但将其转换为字符串是必要的,因为函数只需要字符串,并且thos两种方法返回元组和整数。
猜你喜欢
  • 2012-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2021-03-18
  • 2021-05-23
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多