我在生成空 canvas 的输出时遇到问题。我的积分附加到列表中有问题吗?
I'm having a problem with my output producing an empty canvas. Is there a problem with my points being appended to the list?
我有这个 2d 点 class,我正在尝试使用 tkinter canvas 以图形方式绘制这些点。这些点需要是 2d 点 class 的单独实例,每个点都具有随机的 x 和 y 分量,这些分量在 canvas(设置为 800x800)的宽度和高度内。绘制的点应该半径为 0,颜色随机。点半径和点颜色是坐标系 class 的 class 变量。
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()
canvas 是空白的,因为从未调用过 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
参数,因为它没有使用它。
我有这个 2d 点 class,我正在尝试使用 tkinter canvas 以图形方式绘制这些点。这些点需要是 2d 点 class 的单独实例,每个点都具有随机的 x 和 y 分量,这些分量在 canvas(设置为 800x800)的宽度和高度内。绘制的点应该半径为 0,颜色随机。点半径和点颜色是坐标系 class 的 class 变量。
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()
canvas 是空白的,因为从未调用过 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
参数,因为它没有使用它。