首页 > 解决方案 > 缺少争论

问题描述

weatherreport.Weather to self.weather我在负责我的基本主窗口的主文件中进行了初始化 。

查看图片 [![此处][1]][1]


    def __init__(self):
        self.root = tk.Tk()
        
        #Initialize Weather module
        self.weather = weatherreport.Weather()

    def show_weather(self):
        city_name = tk.Entry(self.root, text = "Enter city name : ", width = 15)
        self.weather(city_name)

    def __init__(self, city):
        self.base_url = "http://api.openweathermap.org/data/2.5/weather?"
        self.city = city```

U can clearly come to know what error I'm facing. I searched all over internet or atleast what i can. None explains this. I'd love to rectify this but i need some help here. It is a small side project for my internals.

Thanking you in anticipation


  [1]: https://i.stack.imgur.com/KLRqQ.png

标签: pythonpython-3.xooptkinter

解决方案


欢迎来到堆栈溢出。请记住粘贴代码而不是代码图像,以便更容易为您提供帮助。

您的错误是一个简单且不言自明的错误。错误是:TypeError: __init__() missing 1 required positional argument: 'city'。让我们分解一下。

__init__()是 Python 中类的特殊功能,有时称为“构造函数”。当您创建该类的“实例”或“对象”时,将调用此函数。

例如,如果我有以下虚拟类:

class Foo:
    def __init__(self):
        print("bar")

当我通过执行类似的操作来创建 Foo 的实例时x = Foo()__init__()会被调用,并且我应该在分配发生之前看到“bar”被打印出来。

错误告诉我们 Weather 类的这个特殊函数需要一个参数,但从未得到它。如果您查看__init__Weather 类的函数,您会发现它接受了一个名为“city”的参数。

__init__因此,要解决此问题,您必须在创建类时提供参数。就像是

self.weather = weatherreport.Weather("Detroit")

推荐阅读