我有一个问题,我的输出产生了一个空画布。我的点被追加到列表中是否有问题?

问题描述 投票:0回答:1

我有一个二维点类,我试图用 tkinter 画布来绘制图形化的点。这些点需要是二维点类的单个实例,每个实例都有随机的x-和y-分量,这些分量在画布的宽度和高度范围内(设置为800x800).绘制的点应该有0的半径和随机颜色。点的半径和点的颜色是坐标系类的类变量。

import math
from fractions import Fraction
from random import randint
from Tkinter import *
# the 2D point class
class Point(object):
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y

    # Mutators and Accessors
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @property
    def y(self):
        return self._y

    @y.setter
    def y(self, value):
        self._y = value

    # String function
    def __str__(self):
        floatX = float(str(self.x))
        floatY = float(str(self.y))
        return "({},{})".format(floatX, floatY)

    # Distance function
    def dist(self, other):
        distance = math.sqrt(((self.x - other.x)**2)+((self.y - other.y)**2))
        return "{}".format(distance)

    # Midpoint function
    def midpt(self, other):
        x_midpoint = float(str(((self.x + other.x))))/2
        y_midpoint = float(str(((self.y + other.y)/2)))
        return Point(x_midpoint, y_midpoint)

# the coordinate system class: (0,0) is in the top-left corner
# inherits from the Canvas class of Tkinter
class CoordinateSystem(Canvas):
    point_colors = ["black", "red", "green", "blue", "cyan", "yellow", "magenta"]
    radius = 0

    def __init__(self, master):
        Canvas.__init__(self, master, bg = "white")
        self.pack(fill = BOTH, expand = 1)
        self.point = []

    def plotPoints(self, n):
        for i in range(NUM_POINTS):
            x = randint(0, WIDTH-1)
            y = randint(0, HEIGHT-1)
            self.point.append(Point(x, y))

    def plot(self, x, y):
        color = self.point_colors[randint(0, len(self.point_colors)-1)]
        self.create_rectangle(x, y, x+self.radius*2, y+self.radius*2, outline = color, fill = color)

##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# the default size of the canvas is 800x800
WIDTH = 800
HEIGHT = 800
# the number of points to plot
NUM_POINTS = 5000

# create the window
window = Tk()
window.geometry("{}x{}".format(WIDTH, HEIGHT))
window.title("2D Points...Plotted")
# create the coordinate system as a Tkinter canvas inside the window
s = CoordinateSystem(window)
# plot some random points
s.plotPoints(NUM_POINTS)
# wait for the window to close
window.mainloop()
python python-2.7 tkinter tkinter-canvas
1个回答
1
投票

画布是空白的,因为 CoordinateSystem.plot() 方法永远不会被调用。这里有一个方法可以解决这个问题。

    def plotPoints(self, n):
        for i in range(NUM_POINTS):
            x = randint(0, WIDTH-1)
            y = randint(0, HEIGHT-1)
            self.point.append(Point(x, y))

        for point in self.point:            # ADDED
            self.plot(point.x, point.y)     # ADDED

还需要注意的是,你并不需要通过 plotPoints()n 参数,因为它没有使用它。

© www.soinside.com 2019 - 2024. All rights reserved.