Confusion with Classes
Kirifam1
kirifam1 at cs.com
Mon Dec 24 02:37:08 EST 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
as
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 plan...here'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
NewForm.labellist
would hold a list of all the labels in NewForm such as
NewForm.label1
NewForm.label2
NewForm.label3
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 poboxes.com please. Thanks
-Anton Kiriwas
ATTACHED CODE
#/usr/bin/env python
# imports
import pygame, pygame.font, pygame.image, pygame.transform
from pygame.locals import *
pygame.init()
class cursor:
cursorlist = []
def __init__(self, pointpos=(0,0), image="c:\windows\pinstripe.bmp",
maskrpg=(255,0,255)):
self.__class__.cursorlist.append(self)
self.pointpos = pointpos #To know where the point is on the
cursor
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):
#self.widgetlist.append(self.label.labellist)
#self.widgetlist.append(self.button.buttonlist)
#self.widgetlist.append(self.cursor.cursorlist)
main_surface = pygame.Surface((self.formwidth, self.formheight),
HWSURFACE).convert()
main_surface.fill(self.bgcolor)
if self.bgimage != None:
tempimg = pygame.image.load(self.bgimage)
tempimgrect = tempimg.get_rect()
tempimg = pygame.transform.scale(tempimg, (self.formwidth,
self.formheight))
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
main_surface.blit(templabel,x.pos)
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.__class__.labellist.append(self)
self.pos = pos
self.text = text
self.fontsize = fontsize
self.fontcolor = fontcolor
self.fontname = fontname
class button:
buttonlist = []
def __init__(self):
self.__class__.buttonlist.append(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