首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError在运行程序时发生,如何修复?

NameError在运行程序时发生,如何修复?
EN

Stack Overflow用户
提问于 2022-10-31 21:56:10
回答 1查看 30关注 0票数 -1

基本上,我的代码创建了不同的分形并将它们显示在画布上。但是,当我去运行程序时,会弹出以下错误消息:(类SierpinskiTriangle(分形):NameError: name 'Fractal‘未定义)。我知道这个特定的错误发生在变量未定义或定义不正确时,但我找不到问题所在。有什么帮助吗?

代码语言:javascript
复制
from tkinter import *
from math import sqrt, sin, cos, pi as PI
from random import randint

# the 2D point class    
class Point(object):
    def __init__(self, x=0.0, y=0.0):
        self.x = float(x)
        self.y = float(y)

# Accessors and mutators
    @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

# Functions used to calculate the distance and the midpoint
    def dist(self, a):
        return sqrt ((self.x-a.x)**2+(self.y-a.y)**2)

    def midpt(self, a):
        return Point((self.x+a.x)/2, (self.y+a.y)/2)
    
# The magic function
    def magic(self, a, triangle):
        trianglex = triangle
        if (self.x > a.x):
            trianglex = 1.0 - triangle
        triangley = triangle
        if (self.y > a.y):
            triangley = 1.0 - triangle
        x = abs(self.x - a.x) * trianglex + min(self.x, a.x)
        y = abs(self.y - a.y) * triangley + min(self.y, a.y)
        return Point(x,y)

# Function used to define the string
    def __str__(self):
        return "({}, {})".format(self.x, self.y)
    
# the coordinate system class: (0,0) is in the top-left corner
# inherits from the Canvas class of Tkinter
class ChaosGame(Canvas):
    def __init__(self, master):
        Canvas.__init__(self, master, bg="white")
        self.pack(fill=BOTH, expand=1)
        
# sets the dimensions and the radius, as well as the color
        self.dimensions = {"MIN_X":2, "MAX_X":WIDTH-9, "MIN_Y":2, "MAX_Y":HEIGHT-9}
        self.dimensions["MID_Y"] = (self.dimensions["MIN_Y"]+self.dimensions["MAX_Y"])/2
        self.dimensions["MID_X"] = (self.dimensions["MIN_X"]+self.dimensions["MAX_X"])/2
        self.vertexRadius = 1
        self.vertexColor = "blue"
        self.pointRadius = 0
        self.pointColor = "black"
    

# Function used to plot the generated points for the SierpinskiTriangle
    def plot_Values(self, points):
        if(points == "SierpinskiTriangle"):
            p1 = SierpinskiTriangle(self.dimensions)
            for vertex in p1.vertices:
                self.plot_point(vertex, self.vertexColor, self.vertexRadius)
            p = p1.vertices[randint(0, len(p1.vertices)-1)]
            for i in range(p1.num_points):
                p = p.magic(p1.vertices[randint(0, len(p1.vertices)-1)], p1.triangle)
                self.plot_point(p, self.pointColor, self.pointRadius)
                        

# Creates the rest of the fractal classes
        if(points == "Pentagon"):
            p1 = Pentagon(self.dimensions)
            for vertex in p1.vertices:
                self.plot_point(vertex, self.vertexColor, self.vertexRadius)
            p = p1.vertices[randint(0, len(p1.vertices)-1)]
            for i in range(p1.num_points):
                p = p.interpt(p1.vertices[randint(0, len(p1.vertices)-1)], p1.triangle)
                self.plot_point(p, self.pointColor, self.pointRadius)

        if(points == "Hexagon"):
            p1 = Hexagon(self.dimensions)
            for vertex in p1.vertices:
                self.plot_point(vertex, self.vertexColor, self.vertexRadius)
            p = p1.vertices[randint(0, len(p1.vertices)-1)]
            for i in range(p1.num_points):
                p = p.interpt(p1.vertices[randint(0, len(p1.vertices)-1)], p1.triangle)
                self.plot_point(p, self.pointColor, self.pointRadius)                        
                        
        if(points == "Octagon"):
            p1 = Octagon(self.dimensions)
            for vertex in p1.vertices:
                self.plot_point(vertex, self.vertexColor, self.vertexRadius)
            p = p1.vertices[randint(0, len(p1.vertices)-1)]
            for i in range(p1.num_points):
                p = p.interpt(p1.vertices[randint(0, len(p1.vertices)-1)], p1.triangle)
                self.plot_point(p, self.pointColor, self.pointRadius)

        if(points == "SierpinskiCarpet"):
            p1 = SierpinskiCarpet(self.dimensions)
            for vertex in p1.vertices:
                self.plot_point(vertex, self.vertexColor, self.vertexRadius)
            p = p1.vertices[randint(0, len(p1.vertices)-1)]
            for i in range(p1.num_points):
                p = p.interpt(p1.vertices[randint(0, len(p1.vertices)-1)], p1.triangle)
                self.plot_point(p, self.pointColor, self.pointRadius)


# Function used to set the color of the points and used to find the radius
    def plot_point(self, point, color, radius):
        self.create_oval(point.x , point.y , point.x + 2*radius, point.y + 2*radius, outline= color , fill= color)

# Code for the fractals
class SierpinskiTriangle(Fractal):
    def __init__(self, canvas):
        Fractal.__init__(self, canvas)
        v1 = Point(self.dimensions["MID_X"], self.dimensions["MIN_Y"])
        v2 = Point(self.dimensions["MIN_X"], self.dimensions["MAX_Y"])
        v3 = Point(self.dimensions["MAX_X"], self.dimensions["MAX_Y"])
        self.vertices = [ v1, v2, v3 ]

class SierpinskiCarpet(Fractal):
    def __init__(self, canvas):
        Fractal.__init__(self, canvas)
        v1 = Point(self.dimensions["MIN_X"], self.dimensions["MIN_Y"])
        v2 = Point(self.dimensions["MID_X"], self.dimensions["MIN_Y"])
        v3 = Point(self.dimensions["MIN_X"], self.dimensions["MIN_Y"])
        v4 = Point(self.dimensions["MIN_X"], self.dimensions["MID_Y"])
        v5 = Point(self.dimensions["MIN_X"], self.dimensions["MID_Y"])
        v6 = Point(self.dimensions["MIN_X"], self.dimensions["MAX_Y"])
        v7 = Point(self.dimensions["MID_X"], self.dimensions["MAX_Y"])
        v8 = Point(self.dimensions["MIN_X"], self.dimensions["MAX_Y"])
        self.vertices = [ v1, v2, v3, v4, v5, v6, v7, v8 ]
        self.num_points = 100000
        self.traingle = 0.66

class Pentagon(Fractal):
    def __init__(self, canvas):
        Fractal.__init__(self, canvas)
        v1 = Point(self.dimensions["MID_X"] + self.dimensions["MID_X"] * cos(2 * PI / 5 + 60), (self.frac_y(0.5375) + self.dimensions["MID_Y"] * sin(2 * PI / 5 + 60)))
        v2 = Point(self.dimensions["MID_X"] + self.dimensions["MID_X"] * cos(4 * PI / 5 + 60), (self.frac_y(0.5375) + self.dimensions["MID_Y"] * sin(4 * PI / 5 + 60)))
        v3 = Point(self.dimensions["MID_X"] + self.dimensions["MID_X"] * cos(6 * PI / 5 + 60), (self.frac_y(0.5375) + self.dimensions["MID_Y"] * sin(6 * PI / 5 + 60)))
        v4 = Point(self.dimensions["MID_X"] + self.dimensions["MID_X"] * cos(8 * PI / 5 + 60), (self.frac_y(0.5375) + self.dimensions["MID_Y"] * sin(8 * PI / 5 + 60)))
        v5 = Point(self.dimensions["MID_X"] + self.dimensions["MID_X"] * cos(10 * PI / 5 + 60), (self.frac_y(0.5375) + self.dimensions["MID_Y"] * sin(10 * PI / 5 + 60)))
        self.vertices = [ v1, v2, v3, v4, v5 ]
        self.triangle = 0.618

class Hexagon(Fractal):
    def __init__(self, canvas):
        Fractal.__init__(self, canvas)
        v1 = Point(self.dimensions["MID_X"], self.dimensions["MIN_Y"])
        v2 = Point(self.dimensions["MIN_X"], self.frac_y(0.25))
        v3 = Point(self.dimensions["MAX_X"], self.frac_y(0.25))
        v4 = Point(self.dimensions["MIN_X"], self.frac_y(0.75))
        v5 = Point(self.dimensions["MAX_X"], self.frac_y(0.75))
        v6 = Point(self.dimensions["MID_X"], self.dimensions["MAX_Y"])
        self.vertices = [ v1, v2, v3, v4, v5, v6 ]
        self.triangle = 0.665
        
class Octagon(Fractal):
    def __init__(self, canvas):
        Fractal.__init__(self, canvas)
        v1 = Point(self.frac_x(0.2925), self.dimensions["MIN_Y"])
        v2 = Point(self.frac_x(0.7075), self.dimensions["MIN_Y"])
        v3 = Point(self.dimensions["MIN_X"], self.frac_y(0.2925))
        v4 = Point(self.dimensions["MAX_X"], self.frac_y(0.2925))
        v5 = Point(self.dimensions["MIN_X"], self.frac_y(0.7075))
        v6 = Point(self.dimensions["MAX_X"], self.frac_y(0.7075))
        v7 = Point(self.frac_x(0.2925), self.dimensions["MAX_Y"])
        v8 = Point(self.frac_x(0.7075), self.dimensions["MAX_Y"])
        self.vertices = [ v1, v2, v3, v4, v5, v6, v7, v8 ]
        self.num_points = 75000
        self.triangle = 0.705
        
##############################
######## MAIN CODE############      
# sets the size of the canvas 
WIDTH = 700
HEIGHT = 550
# the implemented fractals
Fractals = ["SierpinskiTriangle", "SierpinskiCarpet", "Pentagon", "Hexagon", "Octagon"]

# create the fractals in individual (sequential) windows
for i in Fractals:  
    window = Tk()
    window.geometry("{}x{}".format(WIDTH, HEIGHT))
    window.title("The Chaos Game")
# create the chaos game as a Tkinter canvas inside the window
    s = ChaosGame(window)
# plot some random points
    s.plot_Values(i)
# wait for the window to close
    window.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2022-10-31 22:06:53

关于您的代码:

代码语言:javascript
复制
class SierpinskiTriangle(Fractal):

这是试图将SierpinskiTriangle类定义为来自Fractal的派生类。

您不会在代码中的任何一点定义(或导入) Fractal,这就是您获得错误的原因。

因此,解决方案是在尝试对Fractal类进行子类分类之前定义或导入它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74269507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档