Closed MichaelWoodc closed 11 months 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"
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)
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
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
def drawButtons(): ButtonArray(
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():
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:
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()`
This has been fixed in the latest version 1.1.3
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