I have made my own Class Button and I'm trying to modify self.text_color
(in mybutton.py under [#Rectangle (comment)] ) from main.py
with this command button1.text_color = (25,25,25)
but I can't see any change. All other properties are modifying except this one. You copy and run the code to check the problem.
main.py
import pygame
import sys
from mybutton import Button
pygame.init()
screen = pygame.display.set_mode((500, 300))
pygame.display.set_caption('Gui Menu')
clock = pygame.time.Clock()
button1 = Button('Click me', (255,255,255), (255,0,0), 200, 40, ((500-200)//2, 200))
button2 = Button('Click me', (2,25,255), (255,25,255), 200, 40, ((500-200)//2, 100))
button1.text_color = (25,25,25)
button1.hov_bg_color = (25,255,25)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
a = button1.draw(screen)
b = button2.draw(screen)
print(button1.text_color)
if a != None:
print(a)
elif b != None:
print(b)
pygame.display.update()
clock.tick(60)
mybutton.py
import pygame
pygame.init()
class Button:
unq_number = 0
def __init__(self, text, bg_color, hov_bg_color, width, height, pos):
self.pressed = False
# unique number for each button
Button.unq_number = 1
self.unq_number = Button.unq_number
# Rectangle
self.org_width = width
self.rect = pygame.Rect(pos, (width, height))
self.border_radius = 12
self.org_bg_color = bg_color
self.bg_color = self.org_bg_color
self.hov_bg_color = hov_bg_color
self.trans_speed = 4
self.trans_len = 30
self.font = pygame.font.Font(None, 30)
self.text_color = (0, 0, 0)
# text
self.text_surf = self.font.render(text, True, self.text_color)
self.text_rect = self.text_surf.get_rect(center=self.rect.center)
def draw(self, surface):
pygame.draw.rect(surface, self.bg_color, self.rect,
border_radius=self.border_radius)
surface.blit(self.text_surf, self.text_rect)
return self.check_click()
def check_click(self):
mouse_pos = pygame.mouse.get_pos()
if self.rect.collidepoint(mouse_pos):
self.bg_color = self.hov_bg_color
if self.rect.width < (self.org_width self.trans_len):
self.rect.width = self.trans_speed
self.rect.x -= self.trans_speed/2
if pygame.mouse.get_pressed()[0]:
self.pressed = True
else:
if self.pressed == True:
self.pressed = False
return self.unq_number
else:
if self.rect.width > self.org_width:
self.rect.width -= self.trans_speed
self.rect.x = self.trans_speed/2
self.bg_color = self.org_bg_color
CodePudding user response:
The color of the text doesn't magically change when the text_color
attribute is changed. The text_color
is not tied to the surface rendered by the text, you must re-render the text with the new color if you change the color.
Add a method that changes the color attribute and renders the text with the new color:
class Button:
def __init__(self, text, bg_color, hov_bg_color, width, height, pos):
# [...]
self.text = text
def set_text_color(self, color)
self.text_color = color
self.text_surf = self.font.render(self.text, True, self.text_color)
self.text_rect = self.text_surf.get_rect(center=self.rect.center)
Call this method instead of changing the attribute:
button1.set_text_color((25,25,25))
CodePudding user response:
i think you need to change self.text_surf
as well when you set the self.color
you probably need to make a methode def set_text_color()
that change self.text_surf text's color