リストボックスを使ってリストからアイテムを選択するシステムを作成しようとしています。アイテムを押すと、そのアイテムを使用するかどうか尋ねられます。 「はい」を押すと、その項目がリストから削除されます。Python 3.4.2、在庫リストボックスを使用する
私の問題は、正しくプログラムする方法を理解できないため、アイテムがリストから選択されると、そのアイテムを使用するコマンドが実行されるということです。これはコードですが、彼らはただ、ユーザーが行うか、アイテムを使用することを望んでいないかどうかを尋ねるGUIを起動するために使用されているとして、最初の25行は無視することができます:私が試したもので
from tkinter import *
def YNquestion(questionString): #putting the code necessary for the GUI into a function allows it to be called every time the quesiton is asked
global answer #allows the vaiable to be used anywhere in the program
def yes_command(): #called by the GUI to change the variable when the button is clicked
answer.set('yes') #.set allows this to exit the GUI as it is a gloabal variable
window.destroy() #closes the GUI window so the program does no become stuck
def no_command(): #similar to the procedure above but sets the variable to 'no'
answer.set('no')
window.destroy()
window = Tk() #creates GUI window
yes_no_label = Label(window, text=questionString) #sets the label text within this GUI specified as 'window', set to the string specified when called so this can be used in multiple ways such as in tic tac toe or setting gender of character
yes_no_label.grid(row=0, column=1) #sets a place for the label to be displayed on the GUI
answer=StringVar() #needed when running Tkinter with multiple Tk instances to allow variable to be used outside of program (value cannot be assigned within tkinter).
YESbutton = Button(window, text="Yes", fg='green', command = lambda :yes_command()) #sets the button with test and colour deatils, as well as what happens when pressed.
YESbutton.grid(row=1, column=0) #sets button location
NObutton = Button(window, text = 'No', fg = 'red', command= lambda :no_command()) #same as for button above
NObutton.grid(row=1, column=2)
window.mainloop() #required to create the GUI which is essentially an infinite loop waiting for changes (button press)
#this is the seciton of code that handles how the inventory system works
inventory = ["Water", "Rabbit Meat"]
def water():
YNquestion('Water will restore 5 energy. Use it?') #calls GUI which sets variable and asks for label text as arguement
choice = answer.get()
if choice == 'yes':
print('You have restored 5 energy!')
else:
YNquestion.quit
def inventoryCommand():
window = Tk()
listboxInventory = Listbox(window)
listboxInventory.pack()
listboxInventory.insert(END, "Choose an item to use this turn!")
for item in inventory:
listboxInventory.insert(END, item)
turn = True
while turn == True:
listboxInventory.curselection()
mainloop()
inventoryCommand()
これまでのところ、私は「水」を押したときに起こることをプログラムしようとしました。 助けてくれてありがとう!
以下のコメントは非常に参考になりました。私はすっきり、それを作るために、コードを変更した、とメッセージボックスを使用する方法は、ある程度、作品:Tkinterのインポートメッセージボックス 在庫= [「水」、「ウサギの肉」]
からTkinterのインポート* から
#this is the section of code that handles how the inventory system works
def delete_item(listboxInventory, event = None):
answer = messagebox.askquestion("Delete", "Are You Sure?", icon='warning')
if answer == 'yes':
if listboxInventory.curselection() == "Water":
listboxInventory.delete(listboxInventory.curselection())
inventory.remove("Water")
print ("5 Energy has been restored!")
elif listboxInventory.curselection() == "Rabbit Meat":
listboxInventory.delete(listboxInventory.curselection())
inventory.remove("Rabbit Meat")
print ("3 Health has been restored!")
def inventoryCommand():
window = Tk()
listboxInventory = Listbox(window)
listboxInventory.pack()
listboxInventory.bind('<<ListboxSelect>>', lambda _: delete_item(listboxInventory))
listboxInventory.insert(END, "New item")
for item in inventory:
listboxInventory.insert(END, item)
mainloop()
inventoryCommand()
しかし、私が今問題にしているのは、リストとリストボックスの両方からアイテムを削除する方法を理解できないということです。明らかに、上記のコードでは、引数としてlistboxInventory.curselectionを使用して分岐することはできません。これは、基本在庫リストを破棄し、リストボックスに固有のものを使用するのが最善であるということですか?私の元々のアイデアは、アイテムを使用したときにリストを作成して追加または削除できるということでしたが、それが可能かどうかはもはや分かりません。
また、メッセージがアイテムに固有のものになるようにして、ユーザーが何をしているのか正確に知るようにしたいと思います。私は、アイテムが使用されると、これを出力するのが好きです。上記のとおりですが、分岐ステートメントが機能しないため、この方法で動作するかどうかはテストできません。
これをとても複雑にして申し訳ありませんが、私は本当に助けてくれてありがとう!
:このようにそれをバインドし、
そして最後に問題を再現するために必要な場合は、問題を再現するコードを作成しながらそれらを削除してください。 http://www.stackoverflow.com/help/mcveを参照してください。 –