嗨,我对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)
答案 0 :(得分:0)
您已选择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()