繁体   English   中英

在Tkinter中创建一组数据时如何添加选项菜单

[英]How to add option menus when creating a set of data in tkinter

嗨,我是Tkinter和这个网站的新手。

在tkinter中,我希望能够添加一个选项菜单,显示用户可以选择下订单的物品和供应商的列表,但是我不知道如何使选项菜单遵循与日期价格相同的命令和数量

这是到目前为止针对该框架的代码:(如果需要,我可以提供其他数据)

 # Frame 5 - Add Order - window that allows the user to make an order using several entry widgets
    def create_Order_Var():
        f5.tkraise()
        # Data headings
        Label(f5, text="Date 'dd/mm/yy'",bg="#E5E5E5", anchor="w").grid(row=1, column=0, sticky=E+W)
        Label(f5, text="Price",bg="#E5E5E5", anchor="w").grid(row=2, column=0, sticky=E+W)
        Label(f5, text="Quantity",bg="#E5E5E5", anchor="w").grid(row=3, column=0, sticky=E+W)
        Label(f5, text="Item ID",bg="#E5E5E5", anchor="w").grid(row=4, column=0, sticky=E+W)
        Label(f5, text="Supplier ID",bg="#E5E5E5", anchor="w").grid(row=5, column=0, sticky=E+W)

        # Setting variables to approriate types
        newDate = StringVar()
        newPrice = DoubleVar() 
        newQuantity = DoubleVar()
        newItemID = IntVar() 
        newSupplierID = IntVar()

        #Item Option Menu
        variable = StringVar(f5)
        variable.set("Select Item") # default value

        items = get_all_inventory()
        items_formatted = []
        for item in items:
            items_formatted.append(item[0])
        print(items_formatted)
        # Establishing option menu widget
        optionbox = OptionMenu(f5, variable, *items_formatted)

        #Supplier Option Menu
        variable2 = StringVar(f5)
        variable2.set("Select Supplier") # default value

        suppliers = get_all_suppliers()
        suppliers_formatted = []
        for supplier in suppliers:
            suppliers_formatted.append(supplier[0])
        print(suppliers_formatted)
        # Establishing option menu widget
        optionbox2 = OptionMenu(f5, variable2, *suppliers_formatted)

        # Establishing entry widgets
        entry_Date = Entry(f5,textvariable=newDate).grid(row=1,column=1)
        entry_Price = Entry(f5,textvariable=newPrice).grid(row=2,column=1)
        entry_Quantity = Entry(f5,textvariable=newQuantity).grid(row=3,column=1)
        entry_ItemID = optionbox.grid(row=4,column=1)
        entry_SupplierID = optionbox2.grid(row=5,column=1)

        def add_Order():              

            try:
                date = newDate.get()
                price = newPrice.get()
                quantity = newQuantity.get()
                itemID = newItemID.get()
                supplierID = newSupplierID.get()                    
                # Stops invalid data by disallowing fields with the wrong data type
                float(price) 
                int(quantity)
                int(itemID)
                int(supplierID)
                # Calling of create order query
                create_order(date,price,quantity,itemID,supplierID)
                print("You have added: {0},{1},{2},{3},{4}".format(date,price,quantity,itemID,supplierID))
                # After an order has been place the window switches to the check order frame for the user to check that their order was made
                check_Order()
            except:
                # Error message when invalid data is entered
                print("Invalid Data. Price must be a number above zero. Quantity must be an integer above zero")

        Button(f5,text = "Create Order",command = add_Order).grid(row = 6, column = 2, padx = 10)

您已经在variable.get()选择了选项,现在必须使用它在items列表中查找其他信息。 如果您有字典而不是列表,可能会更容易。

清单范例

#!/usr/bin/env python3

import tkinter as tk

# --- functions ---

def on_button():

    # - list -

    # selected option
    sel_list = select_list.get()
    print('list - key:', sel_list)

    # find on list
    keys_list = [x[0] for x in items_list]

    if sel_list in keys_list:
        idx = keys_list.index(sel_list)
        item_list = items_list[idx]
    else:
        item_list =  None

    print('list - item:', item_list)

    print('---')

# --- data ---

# - list -

# list with items
items_list = [
    ['Item A', 'Today', 'Hello'],
    ['Item B', 'Tomorrow', 'World']
]

# --- main ---

root = tk.Tk()

# - list -

# variable for selected item
select_list = tk.StringVar()
select_list.set("Select Item")

# use list first column as options
keys_list = [x[0] for x in items_list]

op_list = tk.OptionMenu(root, select_list, *keys_list)
op_list.pack()

# -

b = tk.Button(root, text='OK', command=on_button)
b.pack()

root.mainloop()

字典范例

#!/usr/bin/env python3

import tkinter as tk

# --- functions ---

def on_button():

    # - dictionary -

    # selected option
    sel_dict = select_dict.get()
    print('dict - key:', sel_dict)

    # find in dictionary
    if sel_dict in items_dict:
        item_dict = items_dict[sel_dict]
    else:
        item_dict = None

    print('dict - item:', item_dict)

    print('---')

# --- data ---

# - dictionary -

# dictionary with items
items_dict = {
    'Item A': ['Today', 'Hello'],
    'Item B': ['Tomorrow', 'World']
}

# --- main ---

root = tk.Tk()

# - dictionary -

# variable for selected item
select_dict = tk.StringVar()
select_dict.set("Select Item")

# use dictionary keys as options
# (dictionary doesn't have to keep order so I sort it)
keys_dict = sorted(items_dict)

op_dict = tk.OptionMenu(root, select_dict, *keys_dict)
op_dict.pack()

# -

b = tk.Button(root, text='OK', command=on_button)
b.pack()

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM