【发布时间】:2015-12-02 08:58:28
【问题描述】:
第一次使用 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,您会看到一个带有两个按钮的窗口,食物和库存。
点击食物,
点击股票,
点击退出,
点击股票,
点击退出。
您应该会看到问题所在。我希望能够关闭“在我身后”的窗口,这样它们就不会堆积起来,因为这是一个需要修复的错误,但我不知道该怎么做。
正如我所说,我在网上查过,但一无所获。
【问题讨论】:
-
您的代码中没有缩进。您需要先修复它,然后它才能运行。
-
您也应该只运行一次
mainloop()。我建议查看有关如何制作 Tkinter 应用程序的教程。
标签: python user-interface tkinter