-1
每當我運行我的程序時,第二個(不需要的)tk實例打開,我不能在打開時使用.quit()
,.destroy()
或.withdraw()
關閉它。我很可能錯過了一些基本的東西,或者把代碼放在錯誤的地方。任何幫助表示讚賞。額外tk實例打開不需要
from Tkinter import *
import random
entry_values = []
population_values = []
subpage = Frame()
button0 = Button(subpage, text="Set Generation Zero Values")
button0.grid(row = 0, column = 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= lambda: close_main_page())
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(): #entry recieve point
save_page = Frame(root)
e0 = entry0.get()
entry_values.append(e0)
e1 = entry0.get()
entry_values.append(e1)
e2 = entry0.get()
entry_values.append(e2)
e3 = entry0.get()
entry_values.append(e3)
e4 = entry0.get()
entry_values.append(e4)
e5 = entry0.get()
entry_values.append(e5)
e6 = entry0.get()
entry_values.append(e6)
e7 = entry0.get()
entry_values.append(e7)
print entry_values
return save_page
def switch_page(page_name):
slaves = root.pack_slaves()
if slaves:
# this assumes there is only one slave in the master
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()
您的縮進被打破。請修復它;不可能知道哪些代碼屬於某個功能,哪些不屬於某個功能。 –
對不起,我發誓我已經這麼做了@BryanOakley我也認爲這個問題可能與代碼開頭的全局變量或變量有關。 – 2C00L4U