コード:
class App:
root = Tk()
button1 = Button()
button2 = Button()
button3 = Button()
img1 = PhotoImage(file="blueBox.png")
img2 = PhotoImage(file="redBox.png")
deckImage = PhotoImage(file="blackBox.png")
以下の 3 つの関数のいずれかを呼び出そうとすると、エラーが発生します。
def showcolor1(self):
if self.card1 != 0:
self.card2 = 1
else:
self.card1 = 1
self.button1.configure(image=self.getArrayValue(0))
self.button1.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(0))
によって呼び出される関数は機能したshowcolor1/2/3
ため、削除しました。Buttons コマンドでこれらの関数を呼び出すには問題があります。
def showcolor2(self):
if self.card1 != 0:
self.card2 = 2
else:
self.card1 = 2
self.button2.configure(image=self.getArrayValue(1))
self.button2.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(1))
def showcolor3(self):
if self.card1 != 0:
self.card2 = 3
else:
self.card1 = 3
self.button2.configure(image=self.getArrayValue(1))
self.button2.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(2))
button1 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor1)
button1.pack()
button2 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor2)
button2.pack()
button3 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor3)
button3.pack()
root.mainloop()
a = App()
コード全体は次のとおりです。黒いボタンが 3 つ表示されます。そして、ボタン 1 を押すと、彼の色が変わるはずです。2 つのボタンを押すと、同じ色かどうかをチェックします。::
from tkinter import *
class App:
root = Tk()
testArray = [1, 2, 2]
cAmount = 0
card1 = 0
card2 = 0
button1 = Button()
button2 = Button()
button3 = Button()
img1 = PhotoImage(file="blueBox.png")
img2 = PhotoImage(file="redBox.png")
deckImage = PhotoImage(file="blackBox.png")
def checkCards(self):
if self.testArray[self.card1] == self.testArray[self.card2]:
print("CORRECT")
elif self.testArray[self.card1] != self.testArray[self.card2]:
print("FALSE")
if self.card1 == 1 or self.card2 == 1:
self.button1.configure(image=self.deckImage)
self.button1.configure(state=NORMAL)
if self.card1 == 2 or self.card2 == 2:
self.button2.configure(image=self.deckImage)
self.button2.configure(state=NORMAL)
if self.card1 == 3 or self.card2 == 3:
self.button3.configure(image=self.deckImage)
self.button3.configure(state=NORMAL)
def setChoosenPicture(self):
self.cAmount = self.cAmount + 1
if self.cAmount == 2:
self.checkCards()
self.cAmount = 0
else:
return
def getArrayValue(self, buttonIndex):
if self.testArray[buttonIndex] == 1:
return self.img1
elif self.testArray[buttonIndex] == 2:
return self.img2
def showcolor1(self):
if self.card1 != 0:
self.card2 = 1
else:
self.card1 = 1
self.button1.configure(image=self.getArrayValue(0))
self.button1.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(0))
def showcolor2(self):
if self.card1 != 0:
self.card2 = 2
else:
self.card1 = 2
self.button2.configure(image=self.getArrayValue(1))
self.button2.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(1))
def showcolor3(self):
if self.card1 != 0:
self.card2 = 3
else:
self.card1 = 3
self.button2.configure(image=self.getArrayValue(1))
self.button2.configure(state=DISABLED)
self.setChoosenPicture(self.getArrayValue(2))
button1 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor1)
button1.pack()
button2 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor2)
button2.pack()
button3 = Button(master=root, text="", image=deckImage, state=NORMAL, command=showcolor3)
button3.pack()
root.mainloop()
a = App()