Skip to main content
 首页 » 编程设计

python之* : 'NoneType' and 'int' in Python 3 不支持的操作数类型

2024年10月01日8bhlsheji

我是 qt5 设计一个应用程序,这是我正在使用的功能序列。

self.u = 0 
self.u = self.listWidget_3.itemClicked.connect(self.ClickRW1) 
 
self.pushButton_2.clicked.connect(self.push4) 

下面是ClickRW1函数

def ClickRW1(self, item): 
    self.y = item.text() 
    if self.y is "June": 
        return 0 
    elif self.y is "July": 
        return 1 
    elif self.y is "August": 
        return 2 
    else: 
        return 3 

当 pushButton_2 被点击时,这个函数被调用

def push4(self): 
    plot_rain(self.z, self.u) 

积雨函数-

def plot_rain(year, mont): 
    if year>2013: 
        year = year-1 
    n = year-2000 
    xc = np.zeros(4) 
    for i in range(0,4): 
        xc[i] = i 
    plt.plot(xc, predicted[((n*16)+(mont*4)):((n*16) + 4 + (mont*4))]) 
    plt.show() 

当我点击 pushButton_2 时。以下是错误-

plt.plot(xc, predicted[((n*16)+(mont*4)):((n*16) + 4 + (mont*4))]) 
 
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int' 

请您参考如下方法:

self.u 为None;您在此处将其设置为 None:

self.u = self.listWidget_3.itemClicked.connect(self.ClickRW1) 

connect() 调用返回了None。

然后将 self.u 传递给 plot_rain() :

def push4(self): 
    plot_rain(self.z, self.u) 

它将该值用作 mont,因此 mont*4 失败。

解决方案是不要将 .connect() 结果赋值给任何东西,它不需要赋值:

self.u = 0 
self.listWidget_3.itemClicked.connect(self.ClickRW1)