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()
My problem at the moment is that I am trying to make the save entries work and can't because the entries are defined in another function. If I try to take them out of the function it will then say subpage is not defined and subpage cannot be moved out of the function(to my knowledge) and still do it's job so I researched declaring global variables but everything I found was an example like this
myGlobal = 5
def func1():
myGlobal = 42
def func2():
print myGlobal
func1()
func2()
where the variable is outside of the function and then they use it in the function with a global scope.
The issue is resolved except for a second instance of tk opening