如何使用square的递归?

时间:2016-03-13 04:58:12

标签: python class recursion tkinter

我有一个问题,你如何使用小数坐标递归绘制一个矩形?我一直试图让正方形/矩形每次变小10%,直到它占据整个画布(意味着它不能超出板的宽度和高度,或者它只是得到0意味着它不会画任何东西,或者它很小,你不会看到最后的广场)。或者也许还有另一种方法,我可以以递归方式绘制正方形,并持续减少一定百分比?

到目前为止,我已经知道了这一点,但我似乎无法想到让它继续递归直到它再也无法做到这一点。

import tkinter

class draw_squares:
    def __init__(self, squares: [(float, float, float, float)]):
        self.squares = squares

        self.root_window = tkinter.Tk()

        self.canvas = tkinter.Canvas(master = self.root_window,
                                     width = 500, height = 500,
                                     background = 'blue')
        self.canvas.grid(row=0, column = 0, padx=0, pady = 0,
                         sticky = tkinter.E + tkinter.W + tkinter.S + tkinter.N)

        self.canvas.bind('<Configure>', self._resized)

        self.root_window.rowconfigure(0, weight = 1)
        self.root_window.columnconfigure(0, weight = 1)

    def start(self):
        self.root_window.mainloop()

    def _resized(self, event: tkinter.Event):
        self.draw_squares_recursively()

    def draw_squares_recursively(self):
        self.canvas.delete(tkinter.ALL)

        for x, y, x2, y2 in self.squares:
            if x != 0.5 and y != 0.5 and x2 != 0.5 and y2 != 0.5:
                self._draw_square(self._draw_square(x-.1,y-.1,x2+.1,y2+.1))

    def _draw_square(self, x: float, y:float, x2:float, y2:float):
        width = self.canvas.winfo_width()
        height = self.canvas.winfo_height()

        self.canvas.create_rectangle(
            x * width, y * height,
            x2 * width, y2 *height, outline = 'grey')

if __name__ == '__main__':
    squares = [(0.9, 0.9, 0.1, 0.1), (0.8,0.8,0.2,0.2), (0.7,0.7,0.3,0.3),
               (0.6,0.6,0.4,0.4), (0.5,0.5,0.5,0.5)]

    app = draw_squares(squares)
    app.start()

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

首先,你使用浮点数,所以(不)相等不是一个好主意http://www.lahey.com/float.htm draw_squares_recursively()可以调用自己,直到达到限制。您可以计算算术以获得所需的大小和位置。

class DrawSquares:
    def __init__(self, square):
##        self.squares = squares

        self.root_window = tkinter.Tk()

        self.canvas = tkinter.Canvas(master = self.root_window,
                                 width = 500, height = 500,
                                 background = 'blue')
        self.canvas.grid(row=0, column = 0, padx=0, pady = 0,
                     sticky = "nsew")

##        self.canvas.bind('<Configure>', self._resized)

        self.root_window.rowconfigure(0, weight = 1)
        self.root_window.columnconfigure(0, weight = 1)

        self.draw_squares_recursively(square)
        self.root_window.mainloop()


    def draw_squares_recursively(self, square):
        ##self.canvas.delete(tkinter.ALL)

        for value in square:
            if value < 0.05 or value > 100:
                return

        self.canvas.create_rectangle(square[0], square[1],
                                     square[2], square[3],
                                     outline = 'grey', width=2)
        square[2] += 20
        square[3] += 20
        self.draw_squares_recursively(square)

if __name__ == '__main__':
    squares = [0.9, 0.9, 10.1, 10.1]
    app = DrawSquares(squares)