Чтобы создать экземпляр класса, назовите его так, как если бы это была функция, которая, как оказалось, возвращала объект GetWeather
.
Кроме того, аргумент tkinter.Button
'command
должно быть чем-то, что он может вызвать снова, то есть вы должны передать ему функцию, которая еще не была вызвана.Обратите внимание на пропущенные скобки после GetWeather('town','country').print_coords
.
Вот ваш фиксированный код:
import tkinter as tk
import requests
API_ID = '///foo///'
class GetWeather:
def __init__(self, city, country):
url_city = 'http://api.openweathermap.org/data/2.5/weather?q={},{}&appid={}'.format(city, country, API_ID)
# note: save the weather data in 'self'
self.weatherdata = requests.get(url_city).json()
def print_coords(self):
# get the stored weather data
print(self.weatherdata)
class MainApplication:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.button1 = tk.Button(
self.frame, text='Coords', width=25,
command=GetWeather('town', 'country').coords,
)
self.button1.pack()
self.frame.pack()
def main():
root = tk.Tk()
app = MainApplication(root)
root.mainloop()
if __name__ == "__main__":
main()