Error with "Person Class"

Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 5:06 pm

I'm building a game and I have an error I don't understand how to fix. See code below.

Code: Select all
from tkinter import *
import random
import time

tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=500, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class Person:
    def _init_(self, id, move, color):
        self.canvas = Canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)

    def draw(self):
        self.canvas.move(self.id, 0, -1)
    person = Person(canvas, 'red')

    while 1:
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)

Below is the error message.
Code: Select all
Traceback (most recent call last):
  File "C:\Python33\Lib\idlelib\random.py", line 13, in <module>
    class Person:
  File "C:\Python33\Lib\idlelib\random.py", line 21, in Person
    person = Person(canvas, 'red')
NameError: name 'Person' is not defined
Last edited by Mekire on Fri Apr 11, 2014 5:09 pm, edited 1 time in total.
Reason: First post lock.
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby stranac » Fri Apr 11, 2014 5:52 pm

These lines are inside the class definition:
Code: Select all
    person = Person(canvas, 'red')

    while 1:
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)

My guess is that's not what you wanted, and you need to unindent them.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Error with "Person Class"

Postby Mekire » Fri Apr 11, 2014 6:26 pm

Also you wrote:
Code: Select all
_init_
This needs to be:
Code: Select all
__init__
Note there are two underscores before and after; not one.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 8:51 pm

Now the lower error message still remains, but the other one is gone. THX
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby stranac » Fri Apr 11, 2014 9:12 pm

Don't know what that means.
Post the updated code and traceback.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 9:17 pm

Traceback (most recent call last):
File "C:\Python33\Lib\idlelib\random.py", line 13, in <module>
class Person:
File "C:\Python33\Lib\idlelib\random.py", line 21, in Person
person = Person(canvas, 'red')
NameError: name 'Person' is not defined
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 9:18 pm

from tkinter import *
import random
import time

tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=500, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class Person:
def __init__(self, id, move, color):
self.canvas = Canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)

def draw(self):
self.canvas.move(self.id, 0, -1)
person = Person(canvas, 'red')

while 1:
tk.update_idletasks()
tk.update()
time.sleep(0.01)
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby stranac » Fri Apr 11, 2014 9:26 pm

Have you read my first reply?
Also, use code tags. They're explained here, along with other useful stuff: viewtopic.php?t=145

And you don't need to send me a PM every time you answer to my posts.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 9:29 pm

i did get ur first reply. any other problems?
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby stranac » Fri Apr 11, 2014 9:38 pm

I don't know.
If you fix that, and don't get any more errors, then there's no more problems.

Also, edit your posts to include code tags.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Error with "Person Class"

Postby Minifluffy » Fri Apr 11, 2014 10:06 pm

There is still the same problem. Here's the code
Code: Select all
from tkinter import *
import random
import time

tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=500, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class Person:
    def __init__(self, id, move, color):
        self.canvas = Canvas
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)

    def draw(self):
        self.canvas.move(self.id, 0, -1)
    person = Person(canvas, 'red')

    while 1:
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)
       
       
Minifluffy
 
Posts: 6
Joined: Fri Apr 11, 2014 4:56 pm

Re: Error with "Person Class"

Postby stranac » Fri Apr 11, 2014 10:08 pm

stranac wrote:These lines are inside the class definition:
Code: Select all
    person = Person(canvas, 'red')

    while 1:
        tk.update_idletasks()
        tk.update()
        time.sleep(0.01)

My guess is that's not what you wanted, and you need to unindent them.

You haven't changed that, those lines are still inside the class definition...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 3 guests