额外的tk实例不必要地打开

时间:2016-08-23 11:11:40

标签: python-2.7 tkinter

每当我运行程序时,tk的第二个(不需要的)实例都会打开,我无法在使用.quit().destroy().withdraw()打开时关闭它。我很可能错过了一些基本的东西或将代码放在错误的地方。任何帮助表示赞赏。

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()

1 个答案:

答案 0 :(得分:0)

问题是您在创建Tk实例之前正在创建其他小部件。执行此操作时,将隐式创建根窗口。稍后,您将创建一个Tk实例,为您提供第二个根窗口。