1

First time coding in Python with tkinter; online searches and the manual have not worked (for me).

Below is an example of the issue:

(I would put an image, but I'm sub 10 reputation so I cannot).

I have a definition that loads the other parts of the system named Load_Main_Menu. You can access the food menu, drink menu, etc from here. For ease of use I have edited it so that you don't have to write out code to get it to work.

I have a definition that displays the main menu, buttons the user can use to get into other parts of the system. I've reduced the code I already have so you can copy and paste it and see what I mean:

import tkinter#Imports Tkinter. Used for the user interface.
from tkinter import *#Imports the package tkinter for the user interface.
from tkinter import ttk#Imports the ttk module, used in the Food/Drink menus.

root = Tk()

tree = ttk.Treeview(root)

def Load_Main_Menu():#Loads the main menu (Food, Drink, Exit, etc.)
#Positions the buttons by setting topFrame as the top (re:First), bottomFrame as
#bottom (re: Last) and then the other buttons fill the space between
Top_Frame = Frame(root)
Top_Frame.pack()
Bottom_Frame = Frame(root)
Bottom_Frame.pack(side = BOTTOM)

#This chunk of code creates the buttons, sets the text, links it to the
#function and also decides it's colour.
Menu_Food_Button = Button(Top_Frame, text = "Food", command = Food_Button, fg = 'Blue')
Menu_Stock_Button = Button(Top_Frame, text = "Stock Control", command = Stock_Control_Button, fg = 'Green')

#The code below determines which side of the parent widget packs against.
Menu_Food_Button.pack(side=LEFT)
Menu_Stock_Button.pack(side=LEFT)

root.mainloop()

def Food_Menu():  
tree['columns'] = ('Price', 'Qty', 'Print Receipt')#additional columns after the default '#0'

tree.column('Price', stretch = 0, width = 100, anchor = E)#Price Column, tkinter.E aligns contents to the "east"
tree.column('Qty', stretch = 0, width = 100, anchor = E)#Quantity Column
tree.column('Print Receipt', stretch = 0, width = 100, anchor = E)#Print Receipt Column
tree.heading('#0', text = "Item")# default column responsible for tree mechanics
tree.heading('Price', text = "£")
tree.heading('Qty', text = "Quantity")
tree.heading('Print Receipt', text = "Get Receipt")

tree.insert('', 0, 'Print_Receipt', text = "Get Receipt")#

tree.insert('', 0, '_Chips_', text = "Chips", values = (4.99, 133))#Parent, text goes to '#0', values go to tree['columns']
tree.insert('_Chips_', 0, text = "Add to Order")#Child

tree.pack()
root.mainloop()

def Stock_Menu():#Loads the main menu (Food, Drink, Exit, etc.)
#Positions the buttons by setting topFrame as the top (re:First), bottomFrame as
#bottom (re: Last) and then the other buttons fill the space between
Top_Frame = Frame(root)
Top_Frame.pack()
Bottom_Frame = Frame(root)
Bottom_Frame.pack(side = BOTTOM)

#This chunk of code creates the buttons, sets the text, links it to the
#function and also decides it's colour.
Stock_Exit_Button = Button(Top_Frame, text = "Exit", command = Return_To_Main_Menu, fg = 'Green')

#The code below determines which side of the parent widget packs against.
Stock_Exit_Button.pack(side=LEFT)

root.mainloop()

def Food_Button():#This displays the food menu.
Food_Menu()

def Stock_Control_Button():#This displays the stock options
Stock_Menu()

def Return_To_Main_Menu():#Loads the main menu
Load_Main_Menu()

Load_Main_Menu()

So, if you've C&P that you'll see a window come up with two buttons, Food and Stock.

Click Food,

Click Stock,

Click Exit,

Click Stock,

Click Exit.

You should see the issue. I want to be able to close the window 'behind me' so they don't pile up because it's a bug that kind of needs to be fixed, but I don't know how.

I've looked online, as I've said, and come up with nothing.

Mat R Yoshka
  • 19
  • 1
  • 1
  • 4

1 Answers1

2

I fixed the indentation and root.mainloop() suggestions by the other answers. If you don't need the tree to be shown anymore, you could use tree.destroy(). However, I think it's more appropriate to use tree.pack_forget() in this case as suggested by this answer here.

I added the tree.pack_forget() to your Return_To_Main_Menu() function and removed the original Load_Main_Menu() call since it would create two new Food and Stock buttons.. I removed the comments from the original post just for easier posting.

import tkinter
from tkinter import *
from tkinter import ttk

root = Tk()

tree = ttk.Treeview(root)

def Load_Main_Menu():
    Top_Frame = Frame(root)
    Top_Frame.pack()
    Bottom_Frame = Frame(root)
    Bottom_Frame.pack(side = BOTTOM)

    Menu_Food_Button = Button(Top_Frame, text = "Food", command = Food_Button, fg = 'Blue')
    Menu_Stock_Button = Button(Top_Frame, text = "Stock Control", command = Stock_Control_Button, fg = 'Green')

    Menu_Food_Button.pack(side=LEFT)
    Menu_Stock_Button.pack(side=LEFT)

    root.mainloop()

def Food_Menu():  
    tree['columns'] = ('Price', 'Qty', 'Print Receipt')

    tree.column('Price', stretch = 0, width = 100, anchor = E)
    tree.column('Qty', stretch = 0, width = 100, anchor = E)
    tree.column('Print Receipt', stretch = 0, width = 100, anchor = E)
    tree.heading('#0', text = "Item")
    tree.heading('Price', text = "£")
    tree.heading('Qty', text = "Quantity")
    tree.heading('Print Receipt', text = "Get Receipt")

    tree.insert('', 0, 'Print_Receipt', text = "Get Receipt")

    tree.insert('', 0, '_Chips_', text = "Chips", values = (4.99, 133))
    tree.insert('_Chips_', 0, text = "Add to Order")#Child

    tree.pack()

def Stock_Menu():
    Top_Frame = Frame(root)
    Top_Frame.pack()
    Bottom_Frame = Frame(root)
    Bottom_Frame.pack(side = BOTTOM)

    Stock_Exit_Button = Button(Top_Frame, text = "Exit", command = Return_To_Main_Menu, fg = 'Green')

    Stock_Exit_Button.pack(side=LEFT)


def Food_Button():
    Food_Menu()

def Stock_Control_Button():
    Stock_Menu()

def Return_To_Main_Menu():
    tree.pack_forget()

Load_Main_Menu()
Community
  • 1
  • 1
qwertyuip9
  • 1,522
  • 2
  • 16
  • 24