0
您好我對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)
你得到錯誤信息?總是添加有問題的FULL錯誤消息(Traceback)。 – furas
不,我沒有收到任何錯誤消息,因爲Item和Supplier Option菜單沒有任何命令 –
,您在'variable.get()'中有菜單選擇,您必須將其與'items'一起用於查找'date '和'價格'。如果你有字典而不是列表,這可能會更容易。 – furas