def add_to_cart():
# Import globals as required
global item_images
global item_names
global item_prices
global count
global total_price
# Find required item details by using the active location of the listbox cursor
img_index = item_box.index(ACTIVE)
name_index = item_box.index(ACTIVE)
price_index = item_box.index(ACTIVE)
# Check listbox to make sure it has been populated and item is selected
#### Do i need this section ####
selection = item_box.curselection()
if selection:
item_box.delete('0', END)
item_box.insert(END, "Oops please select a category")
else:
# Append cart lists ready for html invoice
cart_imgs.append(item_images[img_index])
cart_names.append(item_names[name_index])
cart_prices.append(item_prices[price_index])
# Do i need this handle and if so how do i use it...
try:
item_box.index = int(item_box.curselection()[0])
except IndexError:
item_box.delete('0', END)
item_box.insert(END, "Oops please select a category")
total_price = total_price + float(item_prices[price_index])
# Increase item count per button press.
count = count + 1
ショッピングリストとして機能するリストボックスがあります。ユーザーは、リストボックスに入力するカテゴリボタン(コンピューター/キッチン)を押すと、後でHTMLに送信されるカートにアイテムを選択/追加できます。
cart_img.append(item_images[img_index])
というエラーが表示されますリストのインデックスが範囲外です。
これは、add_to_cart funcを押したときにリストボックスに何もないためです。 (これは、最初にカテゴリが選択されなかったためでもあります)これとtry/exceptエラーハンドルを渡すことを試みるif/elseステートメントを含めました。コールバック関数について話すいくつかの投稿を見つけましたが、この状況でそれを実装する方法を完全には理解していません。
どんな助けでも感謝します
元のコードは次のとおりで、選択したカテゴリがある場合は正常に機能します。
def add_to_cart():
# Import globals as required
global item_images
global item_names
global item_prices
global count
global total_price
# Find required item details by using the active location of the listbox cursor
img_index = item_box.index(ACTIVE)
name_index = item_box.index(ACTIVE)
price_index = item_box.index(ACTIVE)
# Append cart lists ready for html invoice
cart_imgs.append(item_images[img_index])
cart_names.append(item_names[name_index])
cart_prices.append(item_prices[price_index])
# Update total price for invoice
total_price = total_price + float(item_prices[price_index])
# Increase count per button press.
count = count + 1
回答 1 件
関連記事
- インポート後のPythonノートブックのキーエラーの解決
- beautifulsoupでスクレイピングしようとしたときにPythonループでエラーが発生しました
- Pythonでエラー日付値を0で埋める方法
- Webスクレイピング(Python 3)のコードに構文エラーがありますか?
- Python:単純なforループ構文エラー
- リスト内包表記のエラーを除いてPythonが使用されないのはなぜですか
- Pythonシグナル処理、複数のプロセスの終了
- Python @ attrsは、オプションの属性を持つオブジェクトを作成するときにエラーを引き起こします
- 温度センサーのエラー処理
- PythonでJSONファイルにデータを保存するときにエラーが発生しました
関連した質問
- TKINTER Treeview GUIで異なる列の複数の行をカウントして表示するにはどうすればよいですか?
- クリックするとツリービューデータを表示し、クリックすると非表示にします。Tkinter
- Pythonはコードにもかかわらずtypeerrorを発生させません
- tkinterのトップレベルウィジェットに入力された値を取得し、それをメインウィンドウで使用して表示するにはどうすればよいですか?
- 電話をかけたときにtkinterメッセージボックスが表示されなかったのはなぜですか?
- tkinterの1つのクラス関数から値を取得する方法
- キーバインドイベントの比較tkinter
- move()メソッドを使用した後に長方形オブジェクトが表示されない:Canvas- Tkinter
- Tkinterttkはカスタムテーマ設定を参照してください
- Comboboxes tkinterpythonの数値を合計します