1
from Tkinter import *
import random
menu = Tk()
subpage = Tk()
entry_values = []
population_values = []
startUpPage = Tk()

def main_menu(window):

    window.destroy()
    global menu 
    menu = Tk()
    frame1 = Frame(menu)
    menu.resizable(width=FALSE, height=FALSE)
    button0 = Button(menu, text="Set Generation Zero Values", command=sub_menu(menu))
    button1 = Button(menu, text="Display Generation Zero Values")
    button2 = Button(menu, text="Run Model")
    button3 = Button(menu, text="Export Data")
    button4 = Button(menu, text="Exit Program", command=menu.destroy)

    button0.grid(row=0, column=0, sticky=W)
    button1.grid(row=2, column=0, sticky=W)
    button2.grid(row=3, column=0, sticky=W)
    button3.grid(row=4, column=0, sticky=W)
    button4.grid(row=5, column=0, sticky=W)

    menu.mainloop()

def sub_menu(window):
    global subpage
    window.destroy()
    subpage = Tk()
    subpage.resizable(width=FALSE, height=FALSE)

    #defining sub page items
    button5 = Button(subpage, text="Save Generation Data",command = main_menu(subpage))



    juveniles_label0 = Label(subpage,text="Juveniles")
    adults_label1 = Label(subpage,text="Adults")
    seniles_label2 = Label(subpage,text="Seniles")
    population_label3 = Label(subpage,text="Popultation")
    survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)")

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

    button4.grid(row=1, column= 6, sticky=E)

    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)

    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)
    #add entry 6 7 8 
    subpage.mainloop()

main_menu(subpage)
main_menu(startUpPage)

I'm very new to coding and stackoverflow. I am trying to create a GUI that has a main page which will be opened first and a sub page which will be opened by clicking a button which will be stored in the main page. my issue is that I have no clue why it isn't opening my main page. my thought is that it is something to do with the .destroy() or something similar. any help would be much appreciated.

2C00L4U
  • 9
  • 1
  • 7

2 Answers2

1

heres some code that does what you want.. make a window, destroy it when button is clicked and then make a new window...

from Tkinter import *
import random


def main_menu():
    global root
    root = Tk()
    b = Button(root,text='our text button',command = next_page)
    b.pack()

def next_page():
    global root,parent
    parent = Tk()
    root.destroy()
    new_b = Button(parent,text = 'new Button',command=print_something)
    new_b.pack()

def print_something():
    print('clicked')


main_menu()
root.mainloop()
parent.mainloop()

ps. ive done this in python3 so keep that in mind though it wouldnt be a problem in my opinion

Cid-El
  • 500
  • 7
  • 30
  • 1
    Is it good practice to use more than one Tkinter-object? Isn't it better to use *root* for all functions instead? – Ted Klein Bergman Jul 12 '16 at 10:46
  • you could use a Toplevel, but im not sure whether it will serve our purpose here since a Toplevel is a child of the root methinks it will be destroyed with the root, il check it out and post the result – Cid-El Jul 12 '16 at 10:52
  • 1
    You don't need either of those calls to `root.update()`. – Bryan Oakley Jul 12 '16 at 10:53
  • File "/home/vandeventer/x.py", line 16, in next_page new_b = Button(parent,text = 'new Button',command=print_something) File "/usr/lib/python3.4/tkinter/__init__.py", line 2195, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "/usr/lib/python3.4/tkinter/__init__.py", line 2125, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: can't invoke "button" command: application has been destroyed – Cid-El Jul 12 '16 at 10:53
  • so in normal surcumstances you would just use a Toplevel writing: parent = Toplevel(root)... but in this case it wont work... – Cid-El Jul 12 '16 at 10:54
1

As a general rule, you should create exactly one instance of Tk for the life of your program. That is how Tkinter is designed to be used. You can break this rule when you understand the reasoning behind it, though there are very few good reasons to break the rule.

The simplest solution is to implement your main menu and your sub menu as frames, which you've already done. To switch between them you can simply destroy one and (re)create the other, or create them all ahead of time and then remove one and show the other.

For example, the following example shows how you would create them ahead of time and simply swap them out. The key is that each function needs to return the frame, which is saved in a dictionary. The dictionary is used to map symbolic names (eg: "main", "sub", etc) to the actual frames.

def main_menu(root):
    menu = Frame(root)
    button0 = Button(menu, text="Set Generation Zero Values",
                     command=lambda: switch_page("sub"))
    ...
    return menu

def sub_menu(root):
    subpage = Frame(root)
    button5 = Button(subpage, text="Save Generation Data",
                     command = lambda: switch_page("main"))
    ...
    return subpage

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),
    ...
}

switch_page("main")
root.mainloop()

For a more complex object-oriented approach see Switch between two frames in tkinter

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • there are some bits of code there which I don't really understand. I'm guessing the slaves are sub pages/child windows but i still don't fully understand if slaves: # this assumes there is only one slave in the master slaves[0].pack_forget() pages[page_name].pack(fill="both", expand=True) – 2C00L4U Jul 17 '16 at 08:41