2016-11-26 30 views
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) 
+0

你得到錯誤信息?總是添加有問題的FULL錯誤消息(Traceback)。 – furas

+0

不,我沒有收到任何錯誤消息,因爲Item和Supplier Option菜單沒有任何命令 –

+0

,您在'variable.get()'中有菜單選擇,您必須將其與'items'一起用於查找'date '和'價格'。如果你有字典而不是列表,這可能會更容易。 – furas

回答

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() 
相關問題