Point is not defined even though I imported graphi

2019-09-14 23:53发布

问题:

So this is my class for a game called Zombie Dice.

I've imported graphics but I get this error and I have no idea why. Please explain some possibilities for me.

"File "<pyshell#0>", line 1, in <module>
    Game()
  File "C:\Users\Brandon\Desktop\FinalProject3.py", line 17, in Game
    Yellow = DieViewYellow(Gamewindow, Point(95,75),20)
  File "C:\Users\Brandon\Desktop\DieViewYellow.py", line 83, in __init__
    p1 = Point(x-25, y-25)
NameError: name 'Point' is not defined">

Here is my Die view class, which is giving me the issue even though graphics is imported

#Die View Yellow
from graphics import *

class DieViewYellow:

def __init__(self, win, center, value):
    """Create a view of a die, e.g.:
       d1 = GDie(myWin, Point(40,50), 20)
    creates a die centered at (40,50) having sides
    of length 20."""

    # first define some standard values
    self.win = win
    #self.background = Color # color of die face
    #self.foreground = Color2 # color of the pips

    # create a square for the face
    if value==0:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')


    if value == 1:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Brain=Text(Point(95,75),'B')
        self.Brain.draw(self.win)

    elif value == 2:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Brain=Text(Point(95,75),'B')
        self.Brain.draw(self.win)


    elif value == 3:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Shotgun=Text(Point(95,75),'S')
        self.Shotgun.draw(self.win)


    elif value == 4:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Foot=Text(Point(95,75),'F')
        self.Foot.draw(self.win)


    elif value == 5:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Foot=Text(Point(95,75),'F')
        self.Foot.draw(self.win)

    else:
        x, y = center.getX(), center.getY()
        p1 = Point(x-25, y-25)
        p2 = Point(x+25, y+25)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill('yellow')
        self.Shotgun=Text(Point(95,75),'S')
        self.Shotgun.draw(self.win)        

回答1:

You haven't shown all your code, but I believe you have something like this as your import statement:

import graphics

or

from module import graphics

You're trying to use the Point class:

p2 = Point(x+25, y+25) # This fails.

But Python doesn't know that Point lives inside the graphics module. You'll need to tell it that whenever you use Point:

p2 = graphics.Point(x+25, y+25) # This works fine!

You can import Point directly from graphics if you want to use it without the graphics. prefix.

from graphics import Point

p2 = Point(x+25, y+25) # This works fine now!

Edit

The question has been edited, so this answer is a bit redundant now. I'll leave it up though.