Confusion with Classes

Kirifam1 kirifam1 at
Mon Dec 24 08:37:08 CET 2001

I am pretty new to Python and object oriented programming in general.  I am
writing a gui module for another program and want to model it after the
standard gui widgets Visual Basic.  (Yes I know there are already GUI moduls
and packages for python, but i am writing my own, we all need to write our own
code sometimes).  To create this I am using PyGame so anyone that doesn't have
a background in pygame, might not quite get all of it.

I started with a form class.  This create form objects.  My plan was that all
widgets such as buttons, images, labels, etc., would be underneath this.  such

NewForm = form()

then u could set parameters such as

NewForm.pos = (100,100)
NewForm.bgcolor = (255,255,255)
NewForm.bgimage = "bgimage.bmp"

Then the form class has a method for rndering the form and all objects
underneath it onto a single surface, return that surface and blit it onto the
screen whereever the user wants, such as

screen.blit(NewForm.render(), (0,0))

Ok, so that's the's where the problem comes in.  To get all the
widget objects underneath the form I nested the classes as u can see in the
attached code.  Now what i need to do is figure out a way to reach the parent
object (namely the form) from within the widget's methods to add the widget to
a list in the form object.  So for example


would hold a list of all the labels in NewForm such as

If I am going about this all wrong, please help, but please try to get my basic
idea.  Thanks a lot for any help you could provide.  Email me at
akiriwas at please.  Thanks

-Anton Kiriwas

#/usr/bin/env python

# imports
import pygame, pygame.font, pygame.image, pygame.transform
from pygame.locals import *


class cursor:
        cursorlist = []
        def __init__(self, pointpos=(0,0), image="c:\windows\pinstripe.bmp",
            self.pointpos = pointpos     #To know where the point is on the
            self.image = image
            self.maskrgb = maskrpg
            self.currentpos = (0,0)
        def update(self):
            self.currentpos = pygame.mouse.get_pos()

class form:
    def __init__(self, formwidth, formheight, bgimage=None, bgcolor=(0,0,0)):
        self.formwidth = formwidth
        self.formheight = formheight
        self.bgimage = bgimage
        self.bgcolor = bgcolor

    def render(self):
        main_surface = pygame.Surface((self.formwidth, self.formheight),
        if self.bgimage != None:
            tempimg = pygame.image.load(self.bgimage)
            tempimgrect = tempimg.get_rect()
            tempimg = pygame.transform.scale(tempimg, (self.formwidth,
            main_surface.blit(tempimg, (0,0))
            del tempimg, tempimgrect
        for x in self.label.labellist:
            font = pygame.font.Font(x.fontname, x.fontsize)
            templabel = font.render(x.text,1,x.fontcolor)
            templabelpos = templabel.get_rect()
            print "the templabelpos is ", templabelpos
            print "blitted 1"
            templabel = ""
        return main_surface

    class label:
        labellist = []
        def __init__(self, pos=(0,0), text="sample text", fontsize=12,
fontcolor=(255,255,255), fontname=None):
            self.pos = pos
            self.text = text
            self.fontsize = fontsize
            self.fontcolor = fontcolor
            self.fontname = fontname

    class button:
        buttonlist = []
        def __init__(self):
            self.imageup = "sampleup.bmp"
            self.imagedown = None
            self.imageover = None
            self.fontname = None
            self.fontsize = 20
            self.fontcolor = (0,0,0)
            self.text = ""
            self.locx = 0
            self.locy = 0

More information about the Python-list mailing list