如何在两个函数之间定义全局变量

时间:2016-08-18 11:40:14

标签: python-2.7 function tkinter global-variables

from Tkinter import *
import random
entry_values = []
population_values = []

subpage = 0

entry0 = Entry(subpage)
entry1 = Entry(subpage)
entry2 = Entry(subpage)
entry3 = Entry(subpage)
entry4 = Entry(subpage)
entry5 = Entry(subpage)
entry6 = Entry(subpage)
entry7 = Entry(subpage)


def main_menu(root):
    menu = Frame(root)
    button0 = Button(menu, text="Set Generation Zero Values",
                 command=lambda: switch_page("sub"))
    button0.grid(row=0, column=0, sticky=W)

    button1 = Button(menu, text="Display Generation Zero Values",
                 command = lambda: switch_page("sub2"))
    button1.grid(row=1, column=0, sticky=W)

    button2 = Button(menu, text="Run Model",
                 command = lambda: switch_page("sub3"))
    button2.grid(row=2, column=0, sticky=W)

    button3 = Button(menu, text="Export Data")
    button3.grid(row=3, column=0, sticky=W)

    button4 = Button(menu, text="Exit Program", command=menu.destroy)
    button4.grid(row=4, column=0, sticky=W)

    return menu

def sub_menu(root):
    global subpage
    subpage = Frame(root)
    button5 = Button(subpage, text="Save Generation Data",
                 command = lambda: save_entries())
    button5.grid(row=1, column= 6, sticky=E)

    button6 = Button(subpage, text="Return To Main Page",
                 command = lambda: switch_page("main"))
    button6.grid(row=0, column= 6, sticky=W)

    juveniles_label0 = Label(subpage,text="Juveniles")
    adults_label1 = Label(subpage,text="Adults")
    seniles_label2 = Label(subpage,text="Seniles")
    population_label3 = Label(subpage,text="Population (Thousands)")
    survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)")
    birth_rate_label5 = Label(subpage,text="Birth Rate")
    number_of_gens_label6 = Label(subpage,text="Number of Generations")

    global entry0
    entry0 = Entry(subpage)
    global entry1
    entry1 = Entry(subpage)
    global entry2
    entry2 = Entry(subpage)
    global entry3
    entry3 = Entry(subpage)
    global entry4
    entry4 = Entry(subpage)
    global entry5
    entry5 = Entry(subpage)
    global entry6
    entry6 = Entry(subpage)
    global entry7
    entry7 = Entry(subpage)


    juveniles_label0.grid(row=0, column=1) 
    adults_label1.grid(row=0, column=2)
    seniles_label2.grid(row=0, column=3)
    population_label3.grid(row=1, column=0)
    survival_rate_label4.grid(row=2, column=0)
    birth_rate_label5.grid(row=3, column=0)
    number_of_gens_label6.grid(row=3, column=2)

    entry0.grid(row=1, column=1)
    entry1.grid(row=1, column=2)
    entry2.grid(row=1, column=3)
    entry3.grid(row=2, column=1)
    entry4.grid(row=2, column=2)
    entry5.grid(row=2, column=3)
    entry6.grid(row=3, column=2)
    entry7.grid(row=3, column=3)



    return subpage
def display_values(root):
    sub2 = Frame(root)
    label0 = Label(sub2, text = "")
    label1 = Label(sub2, text="")

    button7 = Button(sub2, text="Return To Main Page",
                 command = lambda: switch_page("main"))

    label0.grid(row=1, column=1)
    label1.grid(row=2, column=2)

    button7.grid(row=1, column=10)

    return sub2 

def run_model(root):
    sub3 = Frame(root)
"""
newjuveniles = entry_values[1] * entry_values[6] #new juveniles = adults * birthrate
newseniles = (entry_values[2]*entry_values[5]) + (entry_values[1] * entry_values[4]) #new seniles = adults + survivingseniles
newadults= entry_values[0] * entry_values[3]#juveniles to adults juveniles * juvenile survibal rate
"""
    button8 = Button(sub3, text="Return To Main Page",
                command = lambda: switch_page("main"))

    button8.grid(row=1, column=10)
    return sub3

def save_entries():
    save_page = Frame(root)
    ln0 = entry0.get
    entry_values.append(ln0)
    print entry_values

    return save_page


def switch_page(page_name):
    slaves = root.pack_slaves()
    if slaves:

        slaves[0].pack_forget()
    pages[page_name].pack(fill="both", expand=True)

root = Tk()
pages = {
    "main": main_menu(root),
    "sub": sub_menu(root),
    "sub2":display_values(root),
    "sub3":run_model(root),

}

switch_page("main")
root.mainloop()

我目前的问题是我正在尝试使保存条目有效,因为条目是在另一个函数中定义的。如果我试图将它们从功能中取出,那么它将说明子页面没有被定义,并且子页面不能被移出功能(据我所知)并仍然做它的工作所以我研究声明全局变量但是我所做的一切发现是这样的例子

myGlobal = 5
def func1():
    myGlobal = 42

def func2():
    print myGlobal

func1()
func2()

其中变量在函数之外,然后它们在具有全局范围的函数中使用它。

除了tk open的第二个实例

之外,该问题已得到解决

1 个答案:

答案 0 :(得分:0)

    myvariable = 5
def func():
    global myvariable
    myvariable = 6   #changes `myvariable` at the global scope
    print myvariable #prints 6

func()
print myvariable  #prints 6 now because we were able 
                  #to modify the reference in the function

检查this