목록이 있습니다.
StoreItems = random.sample(set(['sword','pickaxe','toothpick','hammer','torch','saw']), 5)
선택한 5 개의 문자열을 캔버스에 추가하고 바인딩을 제공하는 추가 라인.
XBASE, YBASE, DISTANCE = 300, 320, 50
for i, word in enumerate(StoreItems):
canvas.create_text(
(XBASE, YBASE + i * DISTANCE),
text=word, activefill="Medium Turquoise", anchor=W, fill="White", font=('Anarchistic',40))
found = canvas.find_closest(XBASE, YBASE)
if found:
canvas.itemconfig(found[0])
canvas.bind('<1>', Buy)
문제는 각 단어에 다른 태그 바인딩을 할당해야하고 현재 모든 단어에 동일한 바인딩을 지정해야한다는 것입니다. 그래서 클릭 saw
하는 것보다 다른 결과를 얻을 수 없습니다 toothpick
.
내 솔루션은 다음과 같습니다.
StoreItems = random.sample(set(['sword','pickaxe','toothpick','hammer','torch','saw']), 5)
#Selects 5 Random strings from the list. ^
XBASE, YBASE, DISTANCE = 300, 340, 50
for i, word in enumerate(StoreItems):
canvas.create_text(
(XBASE, YBASE + i * DISTANCE),
text=word, activefill="Medium Turquoise", anchor=W, fill="White", font=('Anarchistic',40), tags=word)
canvas.tag_bind('sword', '<ButtonPress-1>', BuySword)
canvas.tag_bind('pickaxe', '<ButtonPress-1>', BuyPick)
canvas.tag_bind('toothpick', '<ButtonPress-1>', BuyTooth)
canvas.tag_bind('hammer', '<ButtonPress-1>', BuyHammer)
canvas.tag_bind('torch', '<ButtonPress-1>', BuyTorch)
canvas.tag_bind('saw', '<ButtonPress-1>', BuySaw)
(tags = word)를 설정하고 tag.bind를 해당 단어와 동일하게 만들어 해당 태그를 해당 단어에만 할당합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다