AustL / PygameWidgets

A module for use with Pygame. Includes fully customisable buttons, textboxes, sliders and many more, as well as the ability to create and run animations on these widgets.
MIT License
57 stars 16 forks source link

Button array has no text displayed #61

Closed MichaelWoodc closed 11 months ago

MichaelWoodc commented 1 year ago

Describe the bug Even when executing the code found directly here: https://pygamewidgets.readthedocs.io/en/latest/widgets/buttonarray/ there is no text displayed for the buttons

To Reproduce Steps to reproduce the behaviour: run the code from the above link

Expected behaviour I would expect the numbers to be displayed, but they aren't.

Screenshots If applicable, add screenshots to help explain your problem.

Version Numbers

ratbasketball commented 1 year ago

It seems you need to explicitly define the font or pass it to the function. Here's code that works, although it doesn't easily explain why:

`import pygame import pygame_menu import sys import time from csv import DictReader import os import pygame_widgets from pygame_widgets.button import ButtonArray os.environ["SDL_VIDEO_CENTERED"] = "1"

Set up Pygame

padding = 100 pygame.init() pygame.font.init() # you have to call this at the start if you want to use the module textTopPadding = 200 surface = pygame.display.set_mode() displayX, displayY = surface.get_size() surface = pygame.display.set_mode((displayX-10, displayY-10),pygame.RESIZABLE,display=0) x, y = surface.get_size() surfaceRectangle = surface.get_rect() font1 = pygame.font.Font(pygame.font.match_font('arial'), 50) font2 = pygame.font.Font(pygame.font.match_font('impact'), 55) my_font = pygame.font.Font('freesansbold.ttf', 50)

fonts=(font1,font1,font1,font1,font1)

fontslist=[font1,font1,font1,font1,font1] black = (0,0,0)

textDrawn = False

def drawText(surface, text, color, rect, font, aa=False, bkg=None): global textDrawn rect = rect y = pygame.Rect(rect).top lineSpacing = -2

get the height of the font

fontHeight = font.size("Tg")[1]
while text:
    i = 1
    # determine if the row of text will be outside our area
    if y + fontHeight > pygame.Rect(rect).bottom:
        break
    # determine maximum width of line
    while font.size(text[:i])[0] < pygame.Rect(rect).width and i < len(text):
        i += 1
    # if we've wrapped the text, then adjust the wrap to the last word
    if i < len(text): 
        i = text.rfind(" ", 0, i) + 1
    # render the line and blit it to the surface
    if bkg:
        image = font.render(text[:i], 1, color, bkg)
        image.set_colorkey(bkg)
    else:
        image = font.render(text[:i], aa, color)
    surface.blit(image, (pygame.Rect(rect).left, y))
    y += fontHeight + lineSpacing
    # remove the text we just blitted
    text = text[i:]
# textDrawn = True
return text

def printClicked(emotion): print(emotion)

def colorClickedButton(button): global fontslist # this is so we can alter the list outside this function for index, item in enumerate(fontslist): fontslist[index] = font1 fontslist[button] = font2 if button == 4: fontslist[button] = font1

def handleClickedButton(button): pass

Creates an array of buttons

def drawButtons(): ButtonArray(

Mandatory Parameters

surface,  # Surface to place button array on
x/9,  # X-coordinate
y-(y/8),  # Y-coordinate
x*(8/10),  # Width
100,  # Height
(5, 1),  # Shape: 2 buttons wide, 2 buttons tall
border=10,  # Distance between buttons and edge of array
texts=('Happy','Sad','Angry','Fearful','Continue'),  # Sets the texts of each button (counts left to right then top to bottom)
# When clicked, print number
fonts=(tuple(fontslist)),
onClicks=((lambda: (printClicked('Happy'), colorClickedButton(0), ), lambda: (printClicked('Sad'),colorClickedButton(1)), lambda: (printClicked('Angry'), colorClickedButton(2)), lambda: (printClicked('Fearful'), colorClickedButton(3)),lambda: (printClicked('Continue'),colorClickedButton(4)))),
colour = (255,255,255))

def showInstructions():

surface.fill((200,200,200))

textbox = ((surfaceRectangle[0]+padding),(surfaceRectangle[1]+textTopPadding),(surfaceRectangle[2]-padding*2),(surfaceRectangle[2]-padding))
instructions = "I am going to show you some people's faces and I want you to tell me how they feel. I want you to tell me if they are happy, sad, angry, or fearful (scared). Let's get started. Click anywhere to begin. (Esc will quit)"
drawText(surface, instructions, black, textbox, my_font)
# pygame.display.flip()
# waitForStart()
# run_the_game()

def displayUpdate(): events = pygame.event.get() pygame_widgets.update(events) # Call once every loop to allow widgets to render and listen pygame.display.update()

run = True while run: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() run = False quit() if event.type == pygame.VIDEORESIZE:

There's some code to add back window content here.

        surface = pygame.display.set_mode((event.w, event.h),
                                        pygame.RESIZABLE)
        print(surface)
        print (surface.get_size())
        x, y = surface.get_size()
        surface.fill((255, 255, 255))
        pygame.display.flip()

surface.fill((255, 255, 255))

# pygame.display.flip()
drawButtons()
if textDrawn == False:
    showInstructions()
# events = pygame.event.get()
# pygame_widgets.update(events)  # Call once every loop to allow widgets to render and listen
# pygame.display.update()
displayUpdate()`
AustL commented 11 months ago

This has been fixed in the latest version 1.1.3