python - 缺少争论
问题描述
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
解决方案
欢迎来到堆栈溢出。请记住粘贴代码而不是代码图像,以便更容易为您提供帮助。
您的错误是一个简单且不言自明的错误。错误是: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")
推荐阅读
- typescript - POST 方法不向 mongodb 发送数据
- inno-setup - 如何使用 Inno Setup 中的附加下载大小更新 Windows 控制面板/设置应用程序中显示的应用程序大小
- security - 保存通过 Stripe 存储用户银行信息的方法
- typoscript - TYPO3 10.4.x:获取实际语言 Uid 的打字稿
- animation - 在三元运算符中使用时 AnimatePresence 和 motion.div 不起作用
- c# - 为什么会出现这样的错误统一货币化
- sql - 如何检测 SQL 重复值
- awk - 当 awk $n 参数以 z 结尾时是什么意思?
- ruby-on-rails - Docker - 调用后端不起作用 - 即使 app/api 在同一个网络上
- c++ - 析构函数真的会破坏内存还是只是在对象的生命周期结束之前运行?