全局变量未在python中定义

时间:2017-12-24 20:04:33

标签: python python-2.7 spyder

我有以下文件,我已声明

GHG_emissions1, GHG_emissions2, GHG_emissions3, GHG_emissions4 as global 

变量但是当我运行它时,它说,

GHG_emissions1 is not defined.

代码:

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter
from Tkinter import *
import tkMessageBox
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
global GHG_emissions1
global GHG_emissions2
global GHG_emissions3
global GHG_emissions4

def process():
    import matplotlib
    import Tkinter as tk

    matplotlib.use('TkAgg')
    # from matplotlib import style
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
    from matplotlib.figure import Figure


    class GraphPage(tk.Frame):

        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            self.title_label = tk.Label(self, text="Graph Page Example")
            self.title_label.pack()
            self.pack()

        def add_mpl_figure(self, fig):
            self.mpl_canvas = FigureCanvasTkAgg(fig, self)
            self.mpl_canvas.show()
            self.mpl_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

            self.toolbar = NavigationToolbar2TkAgg(self.mpl_canvas, self)
            self.toolbar.update()
            self.mpl_canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


    class MPLGraph(Figure):

      def __init__(self):
            Figure.__init__(self, figsize=(5, 5), dpi=100)
            self.plot = self.add_subplot(111)
            self.plot.plot([1, 2, 3, 4], [GHG_emission1, GHG_emission2, GHG_emission3, GHG_emission4])


    fig = MPLGraph()

    root = tk.Tk()
    graph_page = GraphPage(root)
    graph_page.add_mpl_figure(fig)

    root.mainloop()

def proces():
    CO2_Sequestration_from_green_land=Entry.get(E1) 
    Well_Pad_area=Entry.get(E2)
    Number_of_wells=Entry.get(E3)
    well_lifetime=Entry.get(E4)
    Number_of_wells_per_wellpad=Entry.get(E5)
    Grub_stumps_and_remove=Entry.get(E8)
    Cut_and_Chip_heavy_trees=Entry.get(E9)
    Grading=Entry.get(E10)
    Geotextile_Soil_Stabilization=Entry.get(E11)
    Stone_Pavement=Entry.get(E12)
    Emission_per_1_Million_and_Construction=Entry.get(E13)
    Grub_stumps_and_remove_wpc=Entry.get(E16)
    Cut_and_Chip_heavy_trees_wpc=Entry.get(E17)
    Grading_wpc=Entry.get(E18)
    Slurry_Trench_wpc=Entry.get(E19)
    Pond_Liners_wpc=Entry.get(E20)
    Stone_Pavement_wpc=Entry.get(E21)
    Storage_Tanks_50000_gallons_wpc=Entry.get(E22)
    Road_Construction_Time_Period_per_well=Entry.get(E25)
    Well_Pad_Construction_Time_Period_per_well=Entry.get(E26)
    Average_Number_of_Vehicles_per_well=Entry.get(E27)
    Average_Vehicle_HP=Entry.get(E28)
    Nox_Emission_factor=Entry.get(E29)
    Sox_Emission_factor=Entry.get(E30)
    CO_Emission_factor=Entry.get(E31)
    CO2_Emission_factor=Entry.get(E32)
    CO2_Sequestration_from_green_land=float(CO2_Sequestration_from_green_land)
    Well_Pad_area=float(Well_Pad_area)
    Number_of_wells=float(Number_of_wells)
    well_lifetime=float(well_lifetime)
    Number_of_wells_per_wellpad=float(Number_of_wells_per_wellpad)
    Grub_stumps_and_remove=float(Grub_stumps_and_remove)
    Cut_and_Chip_heavy_trees=float(Cut_and_Chip_heavy_trees)
    Grading=float(Grading)
    Geotextile_Soil_Stabilization=float(Geotextile_Soil_Stabilization)
    Stone_Pavement=float(Stone_Pavement)
    Emission_per_1_Million_and_Construction=float(Emission_per_1_Million_and_Construction)
    Grub_stumps_and_remove_wpc=float(Grub_stumps_and_remove_wpc)
    Cut_and_Chip_heavy_trees_wpc=float(Cut_and_Chip_heavy_trees_wpc)
    Grading_wpc=float(Grading_wpc)
    Slurry_Trench_wpc=float(Slurry_Trench_wpc)
    Pond_Liners_wpc=float(Pond_Liners_wpc)
    Stone_Pavement_wpc=float(Stone_Pavement_wpc)
    Storage_Tanks_50000_gallons_wpc=float(Storage_Tanks_50000_gallons_wpc)
    Road_Construction_Time_Period_per_well=float(Road_Construction_Time_Period_per_well)
    Well_Pad_Construction_Time_Period_per_well=float(Well_Pad_Construction_Time_Period_per_well)
    Average_Number_of_Vehicles_per_well=float(Average_Number_of_Vehicles_per_well)
    Average_Vehicle_HP=float(Average_Vehicle_HP)
    Nox_Emission_factor=float(Nox_Emission_factor)
    Sox_Emission_factor=float(Sox_Emission_factor)
    CO_Emission_factor=float(CO_Emission_factor)
    CO2_Emission_factor=float(CO2_Emission_factor)
    total_no_of_well_pads=(Number_of_wells/Number_of_wells_per_wellpad)
    Entry.insert(E6,0,total_no_of_well_pads)
    print(total_no_of_well_pads)
    GHG_emissions1 = CO2_Sequestration_from_green_land*Well_Pad_area*well_lifetime*total_no_of_well_pads
    Entry.insert(E7,0,GHG_emissions1)
    print(GHG_emissions1)
    Total_Item_Cost=(Grub_stumps_and_remove+Cut_and_Chip_heavy_trees+Geotextile_Soil_Stabilization+Stone_Pavement+Grading)*total_no_of_well_pads
    Entry.insert(E14,0,Total_Item_Cost)
    print(Total_Item_Cost)
    GHG_emissions2 =Emission_per_1_Million_and_Construction*Total_Item_Cost*total_no_of_well_pads/1000000
    Entry.insert(E15,0,GHG_emissions2)
    print(GHG_emissions2)
    Total_Item_Cost_for_Total_wellpad=(Grub_stumps_and_remove_wpc+Cut_and_Chip_heavy_trees_wpc+Grading_wpc+Slurry_Trench_wpc+Pond_Liners_wpc+Stone_Pavement_wpc+Storage_Tanks_50000_gallons_wpc)
    Entry.insert(E23,0,Total_Item_Cost_for_Total_wellpad)
    print(Total_Item_Cost_for_Total_wellpad)
    GHG_emissions3=Emission_per_1_Million_and_Construction*Total_Item_Cost_for_Total_wellpad*total_no_of_well_pads/1000000
    Entry.insert(E24,0,GHG_emissions3)
    print(GHG_emissions3)
    Nox_Emission=Nox_Emission_factor*(Road_Construction_Time_Period_per_well+Well_Pad_Construction_Time_Period_per_well)*1000*total_no_of_well_pads
    Entry.insert(E33,0,Nox_Emission)
    print(Nox_Emission)
    Sox_Emission=Sox_Emission_factor*(Road_Construction_Time_Period_per_well+Well_Pad_Construction_Time_Period_per_well)*1000*total_no_of_well_pads
    Entry.insert(E34,0,Sox_Emission)
    print(Sox_Emission)
    CO_Emission=CO_Emission_factor*(Road_Construction_Time_Period_per_well+Well_Pad_Construction_Time_Period_per_well)*1000*total_no_of_well_pads
    Entry.insert(E35,0,CO_Emission)
    print(CO_Emission)
    CO2_Emission=CO2_Emission_factor*total_no_of_well_pads*Average_Number_of_Vehicles_per_well*Average_Vehicle_HP
    Entry.insert(E36,0,CO2_Emission)
    print(CO2_Emission)
    GHG_emissions4=CO2_Emission+(1.9*CO_Emission)
    Entry.insert(E37,0,GHG_emissions4)
    print(GHG_emissions4)
top = Tkinter.Tk()
L1 = Label(top, text="WELL CONSTRUCTION EMISSIONS",).grid(row=0,column=2)

L2 = Label(top, text="CO2 Sequestration from green land (kg/square foot/year)",).grid(row=3,column=0)
L3 = Label(top, text="Well Pad area (square feet/well pad)",).grid(row=4,column=0)
L4 = Label(top, text="Number of wells",).grid(row=5,column=0)
L4 = Label(top, text="well lifetime (years) ",).grid(row=6,column=0)
L5 = Label(top, text="Number of wells/wellpad",).grid(row=7,column=0)
L6 = Label(top, text="total no of well pads",).grid(row=8,column=0)
L7 = Label(top, text="Emissions from Land and vegetative disruption (Kg CO2-eq)",).grid(row=9,column=0)
L38 = Label(top, text="Emissions from Land and vegetative disruption",).grid(row=1,column=1)

L8 = Label(top, text="Grub stumps and remove (Acre)",).grid(row=3,column=2)
L9 = Label(top, text="Cut and Chip heavy trees (Acre)",).grid(row=4,column=2)
L10 = Label(top, text="Grading (Sq yards)",).grid(row=5,column=2)
L11 = Label(top, text="Geotextile Soil Stabilization (Sq yards)",).grid(row=6,column=2)
L12 = Label(top, text="Stone Pavement (Sq yards)",).grid(row=7,column=2)
L13 = Label(top, text="Emission per 1 Million and Construction (Kg CO2-eq)",).grid(row=8,column=2)
L14 = Label(top, text="Total Item Cost ($) ",).grid(row=9,column=2)
L15 = Label(top, text="Emissions from Access Road Construction (Kg CO2-eq) ",).grid(row=10,column=2)
L39 = Label(top, text="Emissions from Access Road Construction",).grid(row=1,column=3)

L16 = Label(top, text="Grub stumps and remove wpc (Acre) ",).grid(row=12,column=2)
L17 = Label(top, text="Cut and Chip heavy trees wpc (Acre)",).grid(row=13,column=2)
L18 = Label(top, text="Grading wpc (Sq yards)",).grid(row=14,column=2)
L19 = Label(top, text="Slurry Trench wpc (Cub. Ft)",).grid(row=15,column=2)
L20 = Label(top, text="Pond Liners wpc(Sq ft)",).grid(row=16,column=2)
L21 = Label(top, text="Stone Pavement wpc (Sq yards)",).grid(row=17,column=2)
L22 = Label(top, text="Storage Tanks 50000 gallons wpc",).grid(row=18,column=2)
L23 = Label(top, text="Total Item Cost for Total wellpad ($) ",).grid(row=19,column=2)
L24 = Label(top, text=" Emissions from Well Pad Construction (Kg CO2-eq) ",).grid(row=20,column=2)
L40 = Label(top, text="Emissions from Well Pad Construction",).grid(row=11,column=3)

L41 = Label(top, text="Emissions from Construction Vehicles",).grid(row=10,column=1)
L25 = Label(top, text="Road Construction Time Period per well (days)",).grid(row=11,column=0)
L26 = Label(top, text="Well Pad Construction Time Period per well  (days)",).grid(row=12,column=0)
L27 = Label(top, text="Average Number of Vehicles per well",).grid(row=13,column=0)
L28 = Label(top, text="Average Vehicle HP",).grid(row=14,column=0)
L29 = Label(top, text="Nox Emission factor(Tonne/construction time)",).grid(row=15,column=0)
L30 = Label(top, text="Sox Emission factor(Tonne/construction time)",).grid(row=16,column=0)
L31 = Label(top, text="CO Emission factor(Tonne/construction time)",).grid(row=17,column=0)
L32 = Label(top, text="CO2 Emission factor(kg/HP)",).grid(row=18,column=0)
L33 = Label(top, text="Nox Emission(Kg)",).grid(row=19,column=0)
L34 = Label(top, text="Sox Emission(Kg)",).grid(row=20,column=0)
L35 = Label(top, text="CO Emission(Kg)",).grid(row=21,column=0)
L36 = Label(top, text="CO2 Emission(kg)",).grid(row=22,column=0)
L37 = Label(top, text="Emissions from Construction Vehicles (Kg CO2-eq)",).grid(row=23,column=0)
E1 = Entry(top, bd =5)
E1.grid(row=3,column=1)
E2 = Entry(top, bd =5)
E2.grid(row=4,column=1)
E3 = Entry(top, bd =5)
E3.grid(row=5,column=1)
E4 = Entry(top, bd =5)
E4.grid(row=6,column=1)
E5 = Entry(top, bd =5)
E5.grid(row=7,column=1)
E6 = Entry(top, bd =5)
E6.grid(row=8,column=1)
E7 = Entry(top, bd =5)
E7.grid(row=9,column=1)
E8 = Entry(top, bd =5)
E8.grid(row=3,column=3)
E9 = Entry(top, bd =5)
E9.grid(row=4,column=3)
E10 = Entry(top, bd =5)
E10.grid(row=5,column=3)
E11 = Entry(top, bd =5)
E11.grid(row=6,column=3)
E12 = Entry(top, bd =5)
E12.grid(row=7,column=3)
E13 = Entry(top, bd =5)
E13.grid(row=8,column=3)
E14 = Entry(top, bd =5)
E14.grid(row=9,column=3)
E15 = Entry(top, bd =5)
E15.grid(row=10,column=3)
E16 = Entry(top, bd =5)
E16.grid(row=12,column=3)
E17 = Entry(top, bd =5)
E17.grid(row=13,column=3)
E18 = Entry(top, bd =5)
E18.grid(row=14,column=3)
E19 = Entry(top, bd =5)
E19.grid(row=15,column=3)
E20 = Entry(top, bd =5)
E20.grid(row=16,column=3)
E21 = Entry(top, bd =5)
E21.grid(row=17,column=3)
E22 = Entry(top, bd =5)
E22.grid(row=18,column=3)
E23 = Entry(top, bd =5)
E23.grid(row=19,column=3)
E24 = Entry(top, bd =5)
E24.grid(row=20,column=3)
E25 = Entry(top, bd =5)
E25.grid(row=11,column=1)
E26 = Entry(top, bd =5)
E26.grid(row=12,column=1)
E27 = Entry(top, bd =5)
E27.grid(row=13,column=1)
E28 = Entry(top, bd =5)
E28.grid(row=14,column=1)
E29 = Entry(top, bd =5)
E29.grid(row=15,column=1)
E30 = Entry(top, bd =5)
E30.grid(row=16,column=1)
E31 = Entry(top, bd =5)
E31.grid(row=17,column=1)
E32 = Entry(top, bd =5)
E32.grid(row=18,column=1)
E33 = Entry(top, bd =5)
E33.grid(row=19,column=1)
E34 = Entry(top, bd =5)
E34.grid(row=20,column=1)
E35 = Entry(top, bd =5)
E35.grid(row=21,column=1)
E36 = Entry(top, bd =5)
E36.grid(row=22,column=1)
E37 = Entry(top, bd =5)
E37.grid(row=23,column=1)
B=Button(top, text ="Calculate",command = process).grid(row=23,column=3,)





top.mainloop()

1 个答案:

答案 0 :(得分:2)

你似乎认为

global GHG_emissions1

在模块级别对您定义的函数有影响。它没有。 global语句告诉解释器不要在本地函数或类命名空间中查找名称。

因此,在模块级别,执行

GHG_emissions1 = 0 # or whatever initialization you want

并且在引用GHG_emissions1的每个函数内部执行

global GHG_emissions1