创建适用于具有类似功能的多个按钮的功能

时间:2018-05-25 01:34:02

标签: python methods tkinter frame

第一季度:11年级计算机科学项目。我被要求创建一个有用的桌面应用程序。我愚蠢的想法是餐厅菜单。该菜单包含约150个标签和100个按钮。对于100个按钮,其中50个具有类似的功能:添加一个菜,其中50个具有相反的功能:取出一个菜。所以我想尝试只使用2个函数来完成100个函数的工作,因为我真的不想创建100个函数。 Q2:有没有办法找到用户在笔记本上的当前帧?

from tkinter import *
from tkinter import ttk
from Tkinter import Widget
def add():
    pass
def subtra():
    pass
root = Tk()

style = ttk.Style(root)
style.configure('lefttab.TNotebook', tabposition='wn')

# Menu
menu = ttk.Notebook(root, style='lefttab.TNotebook')
catename=["     Appetizer     ", "        Soup        ", "         Rice        ", "       Noodle       ", "        Duck        ", "        Pork        ", "        Beef        ", "       Chicken       ", "     KingPrawn      ", "Scallop and Sea food", "     Vegetables     ", "      Dessert       ", "      Beverage      "]
cate=[]

#Creat
for i in range(0, len(catename)):
    cate.append(Frame(menu, bg="white", width=1000, height=1000))

#Grid
for i in range(0, len(catename)):
    menu.add(cate[i], text=catename[i])


#End of Menu

# Dish names
DishName=[
    ["Egg Rol","Mini Spring Rolls","Vegetarian Spring Rolls","Thai Rolls"],
    ["Clear Consume Soup","Chicken Egg Drop Soup","Wonton & B.B.Q. Pork Soup","Chicken & Sweet Corn Soup"],
    ["Steamed Rice","Vegetable Fried Rice","Mushroom Fried Rice","Chicken Fried Rice"],
    ["Singapore Vermicelli Noodle","Shanghai Thick Noodle","Beef Flat Noodle","Chicken Flat Noodle"],
    ["Old Fashion Braised Duck","B.B.Q. Roasted Duck (Half)","Duck Lettuce Wrap","Shredded Duck Meat in Szechuan Sauce"],
    ["B.B.Q Pork Chop Suey","B.B.Q. Pork Chow Mein","B.B.Q. Pork Egg Foo Young","B.B.Q. Pork with Seasonal Vegetables"],
    ["Beef Chop Suey","Beef Chow Mein","Beef Egg Foo Young","Beef with Seasonal Vegetables"],
    ["Chicken Chop Suey","Chicken Chow Mein","Chicken Egg Foo Young","Chicken with Seasonal Vegetables"],
    ["King Prawn with Vegetables","King Prawn in Curry Sauce","King Prawn in Black Bean Sauce","King Prawn in Satay Sauce"],
    ["Scallop with Vegetables","Scallop in Black Bean Sauce","Sea Salt Scallop","Live Lobster"],
    ["Mixed Vegetables","Spicy Egg Plant","Spicy Bean Stalk","Buddha Delight"],
    ["Home Made Almond Cookie","Cake of the Day","Fresh Lychee Fruit","Mandarin Cheese Cake"],
    ["Coke","Sprite","Diet Coke","Gingerale"]
]
Dishlables=[]
#Creat
for i in range(0, len(catename)):
    singlepage=[]
    for j in range(0, 4):
        singlepage.append(Label(cate[i],text=DishName[i][j]))
    Dishlables.append(singlepage)

#Grid
for i in range(0, len(catename)):
    for j in range(0, 4):
        Dishlables[i][j].grid(column=1,row=j+1,sticky=W)

#Dish Prices
DishPrice=[[2.25,5.25,5.25,7.50],
    [4.50,5.50,5.50,5.50],
    [3.25,7.95,8.50,8,50],
    [13.25,13.95,13.95,13.95],
    [19.95,20.95,55.00,55.00],
    [9.95,10.50,11.95,12.50],
    [10.95,11.50,12.50,13.50],
    [9.95,10.50,11.95,13.50],
    [18.95,18.95,18.95,18.95],
    [19.95,19.95,20.95,12.95],
    [9.95,10.95,11.25,12.95],
    [3.50,5.95,5.95,6.50],
    [1.75,1.75,1.75,1.75],
]
Pricelables=[]
#Creat
for i in range(0, len(catename)):
    singlepage=[]
    for j in range(0, 4):
        singlepage.append(Label(cate[i],text=f'{DishPrice[i][j]:.2f}'))
    Pricelables.append(singlepage)

#Grid
for i in range(0, len(catename)):
    for j in range(0, 4):
        Pricelables[i][j].grid(column=2,row=j+1,sticky=W)

#Number of servings ordered
Count=[]
single=[0,0,0,0]
for i in range(0, len(catename)):
    for j in range(0,4):
        Count.append(single)
countlables=[]
#Creat
for i in range(0, len(catename)):
    single=[]
    for j in range(0,4):
        single.append( Label(cate[i], text=f'{Count[i][j]}'))
    countlables.append(single)


print(Count[0][0])

#Grid
for i in range(0, len(catename)):
    for j in range(0,4):
        countlables[i][j].grid(column=4, row=j+1,sticky=E)

#Add Button
Buttonsadd=[]

#Creat
for i in range(0, len(catename)):
    single=[]
    for j in range(0, 4):
        single.append(Button(cate[i], text="+",command=add))
    Buttonsadd.append(single)
#Grid
for i in range (0, len(catename)):
    for j in range(0, 4):
        Buttonsadd[i][j].grid(column=5,row=j+1)
#Minus Button
Buttonsminus=[]

#Creat
for i in range(0, len(catename)):
    single=[]
    for j in range(0, 4):
        single.append(Button(cate[i], text="-",command=subtra))
    Buttonsminus.append(single)
#Grid
for i in range (0, len(catename)):
    for j in range(0, 4):
        Buttonsminus[i][j].grid(column=3,row=j+1)



menu.pack()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

这是一种简化的方法,展示了如何实现您的要求:

import tkinter as tk


def add_dish_to_order(dish):
    print(dish)
    order[dish].set(order[dish].get() + 1)


if __name__ == '__main__':

    root = tk.Tk()

    dishes = {'carrots': 12.5, 'salad': 16}  # what is available on the menu & price
    order = {}  # What the client orders

    for dish in dishes:
        quantity = tk.IntVar(root, value=0)
        order[dish] = quantity

    add_buttons = {}  # container for the buttons that add a unit to the order
    # you will need to have a container for buttons that remove a unit
    price_labels = {}
    order_labels = {}

    for idx, dish_data in enumerate(dishes.items()):
        dish, price = dish_data
        b = tk.Button(root, text=dish,
                      command=lambda d=dish: add_dish_to_order(d))
        add_buttons[dish] = b
        add_buttons[dish].grid(row=idx, column=0)
        price_labels[dish] = tk.Label(root, text=price)
        price_labels[dish].grid(row=idx, column=1)
        order_labels[dish] = tk.Label(root, textvariable=order[dish])
        order_labels[dish].grid(row=idx, column=2)

    root.mainloop()