2016-11-26 8 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

とリスト

#!/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() 

例と

例を得るのですか?常に問題のFULLエラーメッセージ(トレースバック)を追加してください。 – furas

+0

いいえアイテムとサプライヤオプションメニューにはコマンドがないのでエラーメッセージが表示されません –

+0

'variable.get()'にメニューがあり、 'items'を使って' date 'と' '価格。あなたがリストの代わりに辞書を持っていれば、もっと簡単になるかもしれません。 – furas

答えて

0

あなたはvariable.get()でオプションを選択している今、あなたはそれを使用する必要がありますitemsのリストをご覧ください。あなたがリストの代わりに辞書を持っていれば、もっと簡単になるかもしれません。あなたは、エラーメッセージを辞書

#!/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() 
関連する問題