0

代码:

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()

错误信息

4

4 回答 4

0

只需写出所有功能的最后一行,一切都很完美,我运行它,我得到这个错误,只试一下。

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() //This needs to be out 
于 2021-05-29T18:17:35.473 回答
0

您没有向showcolor3()函数发送参数。

如果要向函数发送参数,可以使用lambda函数:

button3 = tk.Button(master=root,text="",command=lambda self = instance: showcolor3(self))

顺便说一句,您似乎没有在类中定义 showcolor 函数,但我不能肯定地说,因为我无法看到整个代码。如果是这种情况,如果您想通过使用实例来调用它,则必须在类中定义该函数。

编辑:我看到你编辑了你的问题。您必须先创建一个类,然后为其实例创建方法。您的问题可能是对 OOP 的了解不足。查看 OOP 概念可能是个好主意。

于 2020-09-29T05:39:40.377 回答
0

您尚未在类中定义函数。如果有“self”参数,则必须在实例上调用该函数。例如,如果 x 是一个实例,那么您可以将其称为x.showcolor3().

于 2020-09-29T07:09:54.877 回答
0

您的代码的问题是您在class语句中运行了太多东西,而您可能希望稍后在顶层运行它。当您想要使用您在类中定义的方法时,这尤其会导致问题。他们使用self, 应该引用该类的一个实例。但是您不能在类语句本身的主体内创建实例,因为该类尚未完成创建。

这是一个很多示例,它将引发与您的代码相同的错误:

class Foo:
    def bar(self):
        print("bar")

    bar()

    # this also won't work, though you won't get here due to the error above
    f = Foo()  # if you remove the line above, this will raise a NameError about Foo

要修复它,您需要将使用这些方法的代码移出类体。将它放在一个函数中可能是有意义的,但它也可以在顶层工作,如果这更有意义的话。在我非常简单的示例中,它可能会:

class Foo:
    def bar(self):
        print("bar")

# unindent here!
f = Foo()
f.bar()

在您的代码中,您可能希望更多按钮设置代码发生在__init__方法中。在创建每个实例后,Python 会自动调用它。您需要考虑一下您当前正在设计的哪些类变量应该由所有实例共享,哪些应该特定于每个实例。我不太了解你的课程设计,无法告诉你什么是最好的,尽管让所有东西都成为实例属性可能是一个安全的选择。该__init__方法会self自动传入。

这是您的类的一个版本,App它显示了 Tkinter 程序的更典型样式,所有数据都放在实例变量中,而不是类变量中:

class App:
    # get rid of all the class-variable code
    def __init__(self, root):
        self.root = root
        self.testArray = [1, 2, 2]
        self.cAmount = 0
        self.card1 = 0
        self.card2 = 0
    
        self.img1 = PhotoImage(file="blueBox.png")
        self.img2 = PhotoImage(file="redBox.png")
        self.deckImage = PhotoImage(file="blackBox.png")

        self.button1 = Button(master=root, text="", image=self.deckImage, state=NORMAL, command=self.showcolor1)
        self.button1.pack()
        self.button2 = Button(master=root, text="", image=self.deckImage, state=NORMAL, command=self.showcolor2)
        self.button2.pack()
        self.button3 = Button(master=root, text="", image=self.deckImage, state=NORMAL, command=self.showcolor3)
        self.button3.pack()

    # the rest of the methods are the same


# unindent for some more top level code to start the app running
root = Tk()
app = App(root)
root.mainloop()

请注意,Button对象的初始化变化非常轻微,它们现在使用command=self.showcolor1和相似,而不仅仅是使用方法名称。

于 2020-09-29T23:41:52.233 回答