Oct-03-2018, 12:45 PM
Hi guys,
I'm doing my A Level computing project currently. Although I am aware it is more difficult to create a GUI on PyGame compared to, for instance, PyQT, I decided I was going to use PyGame for the entire project anyway - this is because my project is for young children, so the menu needs to be bright, colourful, and easy for them to understand, which I don't believe PyQT can do.
I was wondering, how do you label buttons in PyGame? I've been able to make a button, but I can't get the text to go in the middle - is there a way to do this? Also, since I will need to make a lot of buttons, I tried to make a button class, but honestly, that's just confused me too. I also attempted to make a function to initialise the text coming from what's in the class, but I'm not sure how to get it to work without me specifying which button I want (which would defeat the point of me making a function). This part might not make sense, but hopefully the code will at least make a bit more sense as to what I'm trying to do?
As a somewhat new programmer (I've done Python for a few years, but I haven't attempted big projects like this), all the examples I've looked at to try and understand have just been confusing.
Please can someone explain to me, very simply, how to go about making the GUI? This is my code so far - it's not great, and a lot of it is just testing, but it's just so you can have a basic idea of where I'm up to haha.
Thanks!
I'm doing my A Level computing project currently. Although I am aware it is more difficult to create a GUI on PyGame compared to, for instance, PyQT, I decided I was going to use PyGame for the entire project anyway - this is because my project is for young children, so the menu needs to be bright, colourful, and easy for them to understand, which I don't believe PyQT can do.
I was wondering, how do you label buttons in PyGame? I've been able to make a button, but I can't get the text to go in the middle - is there a way to do this? Also, since I will need to make a lot of buttons, I tried to make a button class, but honestly, that's just confused me too. I also attempted to make a function to initialise the text coming from what's in the class, but I'm not sure how to get it to work without me specifying which button I want (which would defeat the point of me making a function). This part might not make sense, but hopefully the code will at least make a bit more sense as to what I'm trying to do?
As a somewhat new programmer (I've done Python for a few years, but I haven't attempted big projects like this), all the examples I've looked at to try and understand have just been confusing.
Please can someone explain to me, very simply, how to go about making the GUI? This is my code so far - it's not great, and a lot of it is just testing, but it's just so you can have a basic idea of where I'm up to haha.
Thanks!
import pygame import os pygame.init() screen = pygame.display.set_mode((600, 400)) done = False clock = pygame.time.Clock() WHITE = (255, 255, 255) RED = (255, 0, 0) buttonText = pygame.font.SysFont('Comic Sans MS', 20) class Button: def __init__ (self, colour, x, y, width, height, label): self.colour = colour self.x = x self.y = y self.width = width self.height = height self.label = label def getColour(self): return(self.colour) def getX(self): return(self.x) def getY(self): return(self.y) def getWidth(self): return(self.getWidth) def getHeight(self): return(self.getHeight) def getLabel(self): return(self.getHeight) teacher = Button([255, 0, 0], 450, 100, 100, 50, "Teacher") student = Button([255, 0, 0], 450, 200, 100, 50, "Student") def initialiseText(buttonText, i): text = i.getLabel() textSurface = buttonText.render(text) while done == False: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill(WHITE) #teacher = pygame.draw.rect(screen, teacher.getColour, [teacher.getX, teacher.getY, teacher.getWidth, teacher.getHeight]) teacher = pygame.draw.rect(screen, RED, [200, 200, 100, 100]) initialiseText(buttonText, teacher) screen.blit(textSurface, teacher.center) if event.type == pygame.MOUSEBUTTONDOWN: mouse = pygame.mouse.get_pos() if button.collidepoint(mouse): print("hi")