gpt4 book ai didi

python - 如何在 Python 3.4.3 的 tkinter 中关闭窗口?

转载 作者:行者123 更新时间:2023-12-01 04:30:42 25 4
gpt4 key购买 nike

第一次使用 tkinter 在 Python 中编码;在线搜索和手册对我来说不起作用。

以下是该问题的示例:

(我会放一张图片,但我的声誉低于 10,所以我不能)。

我有一个加载系统其他部分的定义,名为 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,您会看到一个窗口,其中有两个按钮:食物和库存。

点击食物,

点击股票,

点击退出,

点击股票,

单击“退出”。

您应该会看到这个问题。我希望能够关闭“我身后”的窗口,这样它们就不会堆积起来,因为这是一个需要修复的错误,但我不知道如何修复。

正如我所说,我在网上查过,但一无所获。

最佳答案

我修复了其他答案的缩进和 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()

关于python - 如何在 Python 3.4.3 的 tkinter 中关闭窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32427902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com