需要帮助来创建函数以及如何传递从文件中读取的参数

时间:2019-04-25 15:19:31

标签: python tkinter tkinter-canvas

我正在尝试创建一个包函数,在这种情况下,它将具有两个参数allRect和canvasSize。第一个参数是我从文件读取后在主函数中创建的Rectangle对象的列表。第二个是包含高度和宽度的元组。然后,我的函数将获取列表,并为每个要放置在画布上而不相互重叠的矩形对象确定一个位置。我的问题是如何从我的主要功能中为我的功能提供矩形列表和画布大小?

如果您有任何需要我回答的问题,我会尽力而为,以便您进一步理解。

from tkinter import *
import sys
import os

root = Tk()

class CustomCanvas:

    def createcanvas(self, height, width): #constructor, 2 arguments height and width of type int
        self.height = height
        self.width = width
        self.canvas = Canvas(width=width, height=height)
        return (self.canvas)


class Rectangle:

    def __init__(self, height, width, x = 0, y = 0): #constructor, takes in 4 arguments height, width, x, and y where x and y have default parameters of 0
        self.height = height
        self.width = width
        self.x = x
        self.y = y

def pack(allRect, canvasSize):


def main():

    fp = open("25PrecentFill.txt","r+")
    canvas_size = fp.readline()
    dimensions = [x.strip() for x in canvas_size.split(',')]
    canvas = CustomCanvas
    canvas = canvas.createcanvas(root, dimensions[0], dimensions[1])
    canvas.pack()
    rectangles = fp.readlines()
    sizes = [i.split(',',1) for i in rectangles]
    rList = []
    count = 0
    for y in sizes:
        y[1] = y[1].rstrip()
        rect = Rectangle(y[0], y[1])
        rList.append(rect)
        count = count + 1
    fp.close()
    arrayColors = ["blue", "green", "red", "purple", "yellow", "orange"]

if __name__ == '__main__':
    main()

0 个答案:

没有答案