Python Forum
Help with Pygame GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Pygame GUI
#1
I am creating a house GUI using Pygame in python. i have been getting an error and i dont know how to solve it.

the code is below


import pygame, os, time, sys, picture
from pygame.locals import *
 
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (205,205,0)
 
pygame.init()
basicFont=pygame.font.SysFont(None,48)
# Set the width and height of the screen [width, height]
screen = pygame.display.set_mode ((1024, 700),0, 32) 
pygame.display.set_caption("My Smart Home")
 
# Loop control until the user clicks the close button.
done = False
 
# Used to manage how fast the screen updates
clock = pygame.time.Clock()



#create the objects

title=basicFont.render("My Smart Home Program",True,BLACK)

titlerect=title.get_rect()

titlerect.left = 350

titlerect.top = 25

#solar panels text on screen

solar_text=basicFont.render("Solar Panels",True,BLACK)

solar_textrect=solar_text.get_rect()

solar_textrect.left = 20

solar_textrect.top = 300

#bathroom windows text on screen

bathroomwin_text=basicFont.render("bathroom windows", True,BLACK)

bathroomwin_textrect=bathroomwin_text.get_rect()

bathroomwin_textrect.left = 300

bathroomwin_textrect.top = 250

#living room windows text on screen
livingroomwin_text=basicFont.render("Living Room Windows", True,BLACK)

livingroomwin_textrect=livingroomwin_text.get_rect()

livingroomwin_textrect.left = 650

livingroomwin_textrect.top = 270

#living room TV text on screen
livingroomtv_text=basicFont.render("Living Room TV", True,BLACK)

livingroomtv_textrect=livingroomtv_text.get_rect()
livingroomtv_textrect.left = 20
livingroomtv_textrect.top = 555

#Garage Door text on screen
garagedoor_text=basicFont.render("Garage Door", True,BLACK)
garagedoor_textrect=garagedoor_text.get_rect()
garagedoor_textrect.left = 380
garagedoor_textrect.top = 555

#Contact Doctor
contactdoctor_text=basicFont.render("Contact Doctor", True,BLACK)
contactdoctor_textrect=contactdoctor_text.get_rect()
contactdoctor_textrect.left = 750
contactdoctor_textrect.top = 590

#Help button
helpbuttun = basicFont.render("Help", True,BLACK)
helpbuttun = helpbuttun.get_rect()
helpbuttunrect.left = 20
helpbuttunrect.top = 10

#help text
helptext = basicFont.render("Hello Everyone", True, YELLOW, BLACK)
helptextRect = help_text.get_rect()
helptextRect.left = 20
helptextRect.top = 650


clickpic = picture.pictureclass(20,90, "solar-off.jpg")
clickpic2 = picture.pictureclass(350,100, "bathclose.jpg")
clickpic3 = picture.pictureclass(350,100, "livingwinclose.jpg")
clickpic4 = picture.pictureclass(350,100, "tv-off.jpg")
clickpic5 = picture.pictureclass(350,100, "garagedoorclose.jpg")
clickpic6 = picture.pictureclass(750,335, "contactdoctor.png")


solarpanelss = False
bathroomwindow = False
livingroomwindow = False
television= False
garagedoors = False
contactthedoctor = False
help1 = False


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
            done = True
        if event.type == MOUSEBUTTONDOWN:
            if clickpic.picRect.collidepoint(event.pos):
                if (solarpanelss == False):
                    solarpanelss = True
                else:
                    solarpanelss = False
            if clickpic2.picRect.collidepoint(event.pos):
                if (bathroomwindow ==False):
                    bathroomwindow = True
                else:
                    bathroomwindow = False
            if clickpic3.picRect.collidepoint(event.pos):
                if (livingroomwindow ==False):
                    livingroomwindow = True
                else:
                    livingroomwindow = False
            if clickpic4.picRect.collidepoint(event.pos):
                if (television ==False):
                    television = True
                else:
                    television = False
            if clickpic5.picRect.collidepoint(event.pos):
                if (garagedoors ==False):
                    garagedoors = True
                else:
                    garagedoors = False
            if clickpic6.picRect.collidepoint(event.pos):
                if (contactthedoctor ==False):
                    contactthedoctor = True
            if help_buttunRect.collidepoint(event.pos):
                help1 = True
            

    #solar panels IF statement
    if (solarpanelss == True):
        clickpic.updatepic(20,50, "solar-on.jpg")
        
    else:
        clickpic.updatepic(20,90, "solar-off.jpg")

    screen.blit(clickpic.pic, clickpic.picRect)





    #bathroom windows IF statement

    if (bathroomwindow == True):
        clickpic2.updatepic(350,100, "bathopen.jpg")

    else:
        clickpic2.updatepic(350,100, "bathclose.jpg")


    screen.blit(clickpic2.pic, clickpic2.picRect)





            
    #living room windows IF statement
    if (livingroomwindow == True):
        clickpic3.updatepic(800,100, "livingwinopen.jpg")

    else:
        clickpic3.updatepic(800,100, "livingwinclose.jpg")


    screen.blit(clickpic3.pic, clickpic3.picRect)




            
    #television IF statement
    if (television == True):
        clickpic4.updatepic(20,350, "tv-on.jpg")

    else:
        clickpic4.updatepic(20,350, "tv-off.jpg")


    screen.blit(clickpic4.pic, clickpic4.picRect)





    #garage doors IF statement  

    if (garagedoors == True):
        clickpic5.updatepic(350,350, "garagedooropen.jpg")

    else:
        clickpic5.updatepic(350,350, "garagedoorclose.jpg")


    screen.blit(clickpic5.pic, clickpic5.picRect)


    #Doctor Contacted IF Statement

    if (contactthedoctor == True):
        clickpic6.updatepic(750,335, "contactdoctor2.png")

    
    screen.blit(clickpic6.pic, clickpic6.picRect)

    #Help Text IF statement
    if help1 == True:
        screen.blit(helptext, helptextRect)

 

    #Title shown on screen
    screen.blit(title, titlerect)

    #Images of object
    screen.blit(clickpic.pic, clickpic.picRect)
    screen.blit(clickpic2.pic, clickpic2.picRect)
    screen.blit(clickpic3.pic, clickpic3.picRect)
    screen.blit(clickpic4.pic, clickpic4.picRect)
    screen.blit(clickpic5.pic, clickpic5.picRect)
    screen.blit(clickpic6.pic, clickpic6.picRect)


    #All Text on screen
    screen.blit(solar_text, solar_textrect)
    screen.blit(bathroomwin_text, bathroomwin_textrect)
    screen.blit(livingroomwin_text, livingroomwin_textrect)
    screen.blit(livingroomtv_text, livingroomtv_textrect)
    screen.blit(garagedoor_text, garagedoor_textrect)
    screen.blit(contactdoctor_text, contactdoctor_textrect)
    screen.blit(helpbuttun, helpbuttunRect)


    #update screen
    pygame.display.flip()

    #limiting the frames to 60 per second
    clock.tick(60)

    #changing the color of the background to white
    screen.fill(WHITE)


# Close the window and quit.
pygame.quit()
                               
Error:
Traceback (most recent call last): File "C:\Users\Shahed\Desktop\Python\Portable Python 3.2.1.1\OOP Assignment\OOP Assignment V5.py", line 86, in <module> helpbuttunrect.left = 20 NameError: name 'helpbuttunrect' is not defined
Can someone help me with this please.
Reply
#2
I think the traceback is doing a good job of telling you what's wrong Wink

Shouldn't line 85 be:
helpbuttunrect = helpbuttun.get_rect()
rather than:
helpbuttun = helpbuttun.get_rect()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020