函数无法从其他函数识别全局变量

问题描述 投票:0回答:1

我正在编写一个显示天气数据的python GUI应用程序。我有三个函数,我想在我的displaydata()函数中使用在getdata()函数中声明的变量。我将变量“值”声明为全局变量。但是,当我运行程序时,它说在displaydata()函数中未定义“值”。因为值应该在我的displaydata()函数中被识别,因为它在我的getdata()函数中被声明为全局变量,因此这怎么可能。

def getdata():
    apikey = '2GTELFA4SCTBGTNMNIN6P8F6K'
    url = 'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/weatherdata/history'
    place = city.get() + ',' + state.get() + ',US'
    months = dict(January = 1, February = 2, March = 3, April = 4, May = 5, June = 6, July = 7, August = 8, September = 9, October = 10, November = 11, December = 12)
    monthvalue = '0' + str(months[month.get()])#Convert month name to month number
    endmonthvalue = '0' + str(months[endmonth.get()]) 
    #Gather data from api
    if radiovalue.get() == 1:
       params = {'aggregateHours': '24' , 'startDateTime': year.get() + '-' + monthvalue + '-' + day.get() + 'T00:00:00', 'endDateTime': year.get() +
              '-' + monthvalue + '-' + day.get()  + 'T00:00:00',
              'unitGroup': 'us', 'contentType': 'json', 'dayStartTime': '0:0:00', 'dayEndTime': '0:0:00',
              'location': place, 'key': apikey }
    if radiovalue.get() == 2:
       params = {'aggregateHours': '24' , 'startDateTime': year.get() + '-' + monthvalue + '-' + day.get() + 'T00:00:00', 'endDateTime': endyear.get() +
              '-' + endmonthvalue + '-' + endday.get()  + 'T00:00:00',
              'unitGroup': 'us', 'contentType': 'json', 'dayStartTime': '0:0:00', 'dayEndTime': '0:0:00',
              'location': place, 'key': apikey }
    if radiovalue.get() == 3:
        if monthvalue == '01' or '03' or '05' or '07' or '08' or '010' or '012':
           params = {'aggregateHours': '24' , 'startDateTime': year.get() + '-' + monthvalue + '-' + '01' + 'T00:00:00', 'endDateTime': year.get() +
              '-' + monthvalue + '-' + '31'  + 'T00:00:00',
              'unitGroup': 'us', 'contentType': 'json', 'dayStartTime': '0:0:00', 'dayEndTime': '0:0:00',
              'location': place, 'key': apikey }
        elif monthvalue == '04' or '06' or '09' or '011':
            params = {'aggregateHours': '24' , 'startDateTime': year.get() + '-' + monthvalue + '-' + '01' + 'T00:00:00', 'endDateTime': year.get() +
              '-' + monthvalue + '-' + '30'  + 'T00:00:00',
              'unitGroup': 'us', 'contentType': 'json', 'dayStartTime': '0:0:00', 'dayEndTime': '0:0:00',
              'location': place, 'key': apikey }
        else:
            params = {'aggregateHours': '24' , 'startDateTime': year.get() + '-' + monthvalue + '-' + '01' + 'T00:00:00', 'endDateTime': year.get() +
              '-' + monthvalue + '-' + '28'  + 'T00:00:00',
              'unitGroup': 'us', 'contentType': 'json', 'dayStartTime': '0:0:00', 'dayEndTime': '0:0:00',
              'location': place, 'key': apikey } 


    response = requests.get(url, params = params)
    weather = response.json()
    global value
    value = weather['locations'][place]['values']

def displaydata():
    for i in range(0,len(value)):
        maxtemp = tk.Label(text = weather['locations'][place]['values'][i]['maxt'])
        mintemp = tk.Label(text = weather['locations'][place]['values'][i]['mint'])
        precipitation = tk.Label(text = weather['locations'][place]['values'][i]['precip'])
        condition = tk.Label(text = weather['locations'][place]['values'][i]['conditions'])

        if(radiovalue.get() == 1):
          highlabel.place(relx = 0.05, rely = 0.60)
          maxtemp.place(relx = 0.30, rely = 0.60)
          lowlabel.place(relx = 0.05, rely = 0.65)
          mintemp.place(relx = 0.30, rely = 0.65)
          preciplabel.place(relx = 0.05, rely = 0.70)
          precipitation.place(relx = 0.30, rely = 0.70)
          conditionslabel.place(relx = 0.05, rely = 0.75)
          condition.place(relx = 0.30, rely = 0.75)
python global
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.