尝试使用stack()添加形状

时间:2016-04-22 07:40:33

标签: python python-3.x tkinter-canvas

尝试使用堆栈()添加和删除圆圈。我得到一个我不确定的错误。试图在随机位置和随机大小中添加一个圆圈。这是我的堆栈类

class Stack:
def __init__(self):
    self.items = []

def is_empty(self):
    return self.items == []

def push(self, item):
    self.items.append(item)

def pop(self):
    return self.items.pop()

def peek(self):
    return self.items[len(self.items) - 1]

def size(self):
    return len(self.items)

这是我的另一堂课。我收到此错误>>> AttributeError:' Recta'对象没有属性' stack'

from tkinter import *
import random
root = Tk()
from Stack import Stack

class Recta:

def __init__(self, height=60, width=80 ):
    self.height = height
    self.width = width
    self.create_buttons()
    self.canvas = Canvas(root)
    self.canvas.pack()
    stack = Stack()



def create_buttons(self):
        self.frame = Frame(root, bg='grey', width=400, height=40)
        self.frame.pack(fill='x')
        self.button1 = Button(self.frame, text='Oval', command=randomCircle)
        self.button1.pack(side='left', padx=10)
        self.button2 = Button(self.frame, text = "Remove Oval")
        self.button2.pack(side = 'left')

def randomCircle(self):
    w = random.randrange(300)
    h = random.randrange(200)
    self.stack.push(self.canvas.create_oval(0,0,w,h,fill= random.choice(self.colours)))

tes = Recta() root.mainloop()

2 个答案:

答案 0 :(得分:0)

在init中

它应该是self.stack = Stack()而不是stack = Stack()

答案 1 :(得分:0)

这是有效的。

你真的需要学习the code proposed earlier,以便能够针对各种形状和情况重复它。 :)

from tkinter import *
import random

class Stack:
    def __init__(self):
        self.items = []

    def is_empty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items) - 1]

    def size(self):
        return len(self.items)



class Recta:

    def __init__(self, height=60, width=80 ):
        self.height = height
        self.width = width
        self.create_buttons()
        self.canvas = Canvas(root)
        self.canvas.pack()
        self.stack = Stack()        # <-- must be self.stack

    def create_buttons(self):
        self.frame = Frame(root, bg='grey', width=400, height=40)
        self.frame.pack(fill='x')
        self.button1 = Button(self.frame, text='Oval', command=self.randomCircle)    # <- must be self.randomCircle
        self.button1.pack(side='left', padx=10)
        self.button2 = Button(self.frame, text = "Remove Oval", command=self.remove_oval)     # <-- must have a command method to be able to do anything
        self.button2.pack(side = 'left')

    def randomCircle(self):
        w = random.randrange(300)
        h = random.randrange(200)
        self.stack.push(self.canvas.create_oval(0, 0, w, h, fill='blue'))

    def remove_oval(self):    # <-- must have a method to correspond to the command in button2
        try:
            a = self.stack.pop()
            self.canvas.delete(a)
        except:
            pass

root = Tk()
tes = Recta()
root.mainloop()

remove_oval也可以按如下方式编写,以利用class Stack()方法:

def remove_oval(self):
    if not self.stack.is_empty():
        self.canvas.delete(self.stack.pop())