首次使用tkinter在Python中编码;在线搜索和手册没有用(对我而言)。
以下是此问题的示例:
(我会放一张图片,但是我的声望很低,所以我不能这样做。)
我有一个定义,它加载名为Load_Main_Menu的系统的其他部分。您可以从这里进入食物菜单,饮料菜单等。为了便于使用,我对其进行了编辑,这样您就不必编写代码来使其工作。
我有一个显示主菜单的定义,用户可以用来进入系统其他部分的按钮。我已经减少了我已有的代码,因此您可以复制并粘贴它,看看我的意思:
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()
所以,如果你有C& P,你会看到一个窗口出现两个按钮,食物和股票。
点击食物,
点击股票,
单击退出,
点击股票,
单击“退出”。
你应该看到这个问题。我希望能够关闭窗户'在我身后'所以他们不会堆积,因为它是一种需要修复的错误,但我不知道如何。
我已经在网上看过了,正如我所说的那样,并且没有任何结果。
答案 0 :(得分:2)
我通过其他答案修复了缩进和root.mainloop()建议。如果您不再需要显示树,则可以使用tree.destroy()
。但是,我认为在这种情况下使用tree.pack_forget()
更合适,如此答案here所示。
我已将tree.pack_forget()
添加到您的Return_To_Main_Menu()
功能并删除了原来的Load_Main_Menu()
来电,因为它会创建两个新的食物和股票按钮..我删除了原始帖子中的评论为了更容易发布。
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()