Code:
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")
The Error occurs after trying to call one of the 3 functions below:
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))
I removed the functions that will be called by showcolor1/2/3
, because they worked. There is just a problem with calling these functions with the Buttons command:
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()
Here is the whole code: It should show 3 Buttons that are black. ANd if you press BUtton 1 it should change his color. When you press 2 Buttons it checks if they have the same color or not. ::
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()
You have not defined the function in a class. The function must be called on an instance if there is 'self' parameter. e.g if x is an instance then you can call it as
x.showcolor3()
.