对象没有属性?

时间:2014-12-01 02:28:36

标签: python python-3.x tkinter

所以我有这个代码" Rock Paper Scissors"我收到类myApp对象的属性错误没有属性' myContainer1'。我现在很无能为力。此外,如果有任何其他方式可以改善,这对我也是一个很大的帮助。

 from tkinter import * 
 import random
 def main():
    options = ["rock", "paper", "scissors"]
    class myApp:
        def __init__(self, parent):
            self.myParent = parent
            self.myParent = Frame(parent)
            self.myContainer1.pack()

            self.button1 = Button(self.myContainer1, commmand = self.rockMove)
            self.button2.configure(text = "rock")
            self.button1.pack()
            self.button1.bind("<Button-1>", self.rockMove)

            self.button2 = Button(self.myContainer1, command = self.paperMove)
            self.button2.configure(text = "paper")
            self.button2.pack()
            self.button2.bind("<Button-2>", self.paperMove)

            self.button3 = Button(self.myContainer1, command = self.scissorsMove)
            self.button3.configure (text = "scissors")
            self.button3.pack()
            self.button3.bind("<Button-3>", self.scissorsMove)

            self.CompButton1 = Button(root)
            self.compButton1.configure(text = "rock", state = "disabled")
            self.compButton1.pack() 

            self.compButton2 = Button(root)
            self.compButton2.configure(text = "paper", state = "disabled")
            self.compButton2.pack()

            self.compButton3 = Button(root)
            self.compButton3.configure(text = "scissors", state = "disabled")
            self.compButton3.pack()

        def compRandom(self,event):
            self.compRandom["text"] = random.choice(options)
        def button1Click(self, event):
            if self.button1["text"] == "rock" and self.compButton1["text"] == "paper":
                return "You lose!"
            elif self.button1["text"] == "rock" and self.compbutton1["text"] == "rock":
                return "Draw!"
            else: "you win!"
        def rockMove(self, event):
            self.compRandom()
            self.button1Click()
        def button2Clck(self, event):
            if self.button2["text"] == "paper" and self.compButton2["text"] == "scissors":
                return "You lose!"
            elif self.button2["text"] == "paper" and self.compbutton2["text"] == "paper":
                return "Draw!"
            else: "you win!"
        def paperMove(self, event):
            self.compRandom()
            self.button2Click()
        def button3Click(self, event):
            if self.button3["text"] == "scissors" and self.compbutton3["text"] == "rock":
                return "you lose!"
            elif self.button3["text"] == "scissors" and self.compButton3["text"] == "scissors":
                return "Draw!"
            else: "you win!"
        def scissorsMove(slef, event):
            self.compRandom()
            self.button3Click()
    root = Tk()
    myapp = myApp(root)
    root.mainloop()

以下是我遇到的错误我不确定myContainer或myApp类发生了什么。也许我在做错了班级?

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    main()
  File "<pyshell#3>", line 66, in main
    myapp = myApp(root)
  File "<pyshell#3>", line 7, in __init__
    self.myContainer1.pack()
AttributeError: 'myApp' object has no attribute 'myContainer1'

1 个答案:

答案 0 :(得分:1)

我认为myParentmyContainer1的拼写错误。

除此之外,还有很多错别字。

    一行中的button2
  • button1self.button2.configure(text = "rock")
  • {li> CompButton1 compButton1 {li> commmand command {li> slef self

并且,回调函数应该只有一个参数:self


compRandom正在尝试将项目分配给该方法。您可能希望更改其他窗口小部件而不是compRandom

def compRandom(self):
    self.compRandom["text"] = random.choice(options)