Вы должны отправить его как параметр
def get_hourly_forecast(cur_address):
geocode_url = "...".format(cur_address, API_KEY)
И затем передать функцию кнопки, которая запускается get_hourly_forecast
со строкой
class MainWeatherHub(MyWeatherApp):
def __init__(self, mainwindow):
self.address = StringVar() # use self.
ttk.Button(loc_entry, text="Get Forecast", command=run_it)
def run_it(self):
get_hourly_forecast(self.address.get())
или используя lambda
class MainWeatherHub(MyWeatherApp):
def __init__(self, mainwindow):
ttk.Button(loc_entry, text="Get Forecast", command=lambda:get_hourly_forecast(address.get()))
РЕДАКТИРОВАТЬ:
Я вижу, вы используете current_weather
(StringVar
из MainWeatherHub
) в get_hourly_forecast
, чтобы установить значение current_weather.set(out[0])
.
Вы можете отправить current_weather на get_hourly_forecast
в качестве параметра
def get_hourly_forecast(cur_address, current_weather):
geocode_url = "...".format(cur_address, API_KEY)
current_weather.set(out[0])
и
class MainWeatherHub(MyWeatherApp):
def __init__(self, mainwindow):
self.address = StringVar() # use self.
self.current_weather = StringVar() # use self.
ttk.Button(loc_entry, text="Get Forecast", command=run_it)
def run_it(self):
get_hourly_forecast(self.address.get(), self.current_weather)
, но может быть лучше вернуть значение из get_hourly_forecast
def get_hourly_forecast(cur_address):
geocode_url = "...".format(cur_address, API_KEY)
return out[0]
и получите его в run_it
def run_it(self):
result = get_hourly_forecast(self.address.get())
if result is not None:
self.current_weather.set(result)
Таким образом get_hourly_forecast
не работает с StringVar
, и вы можете использовать его в другой программе, которая не использует StringVar
.