The Error only occurs when hovering the mouse over the maximize button that I have created in the program.
I’m using the pygame 2.3.0 on python 3.11.2
Error Message:
pygame 2.3.0 (SDL 2.24.2, Python 3.11.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "e:\Prototype\main.py", line 53, in <module>
window.Draw()
File "e:\Prototype\apps\window.py", line 37, in Draw
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.wsize[0], self.wsize[1])
~~~~~~~~~~^^^
TypeError: 'builtin_function_or_method' object is not subscriptable
PS E:\Prototype>
The code is this: listed under: the directory /apps/window.py
class Window:
def __init__(self, Pos: list[int, int], WinSize: list[int, int], screen: pygame.display, WindowEvent: object):
self.screen = screen
self.x_button = None
self.maximize_button = None
self.minimize_button = None
self.running = True
self.WindowEvent = WindowEvent
self.pos = Pos
self.wsize = WinSize
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.wsize[0], self.wsize[1])
# Colour Sets
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
self.GRAY = (99, 102, 106)
self.LIGHTGRAY = (217, 217, 214)
self.RED = (255, 0, 0)
self.select_x_button = self.GRAY # Due to this needing a colour its set here
def Draw(self):
if not self.running:
return None
self.rect = pygame.Rect(self.pos[0], self.pos[1], self.wsize[0], self.wsize[1])
win_x = self.rect.x # Gets the window POS x
win_y = self.rect.y # Gets the window POS y
win_size = self.rect.size # Gets the window size for x and y
x_button_text = "X"
x_button_font = pygame.font.Font(None, 32)
x_button_colour = self.BLACK
x_button_render = x_button_font.render(x_button_text, True, x_button_colour)
control_bar_rect = pygame.Rect(win_x, win_y, win_size[0], 32) # Sets the values for the control bar
x_button_rect = pygame.Rect(win_x + win_size[0] - 32, win_y, 32, 32) # Sets the values for the x button
maximize_button_rect = pygame.Rect(win_x + win_size[0] - 64, win_y, 32, 32) # Sets the values for the maximize button
minimize_button_rect = pygame.Rect(win_x + win_size[0] - 96, win_y, 32, 32) # Sets the values for the minimize button
self.x_button = x_button_rect # Sets the Class Variable x_button to the created rect
self.maximize_button = maximize_button_rect # Sets the Class Variable maximize_button to the created rect
self.minimize_button = minimize_button_rect # Sets the Class Variable minimize_button to the created rect
pygame.draw.rect(self.screen, self.WHITE, self.rect) # Renders Main Window
pygame.draw.rect(self.screen, self.LIGHTGRAY, control_bar_rect) # Renders the bar that contains the action buttons
pygame.draw.rect(self.screen, self.select_x_button, x_button_rect) # Renders the x button
self.screen.blit(x_button_render, (x_button_rect.x, x_button_rect.y)) # Renders the x button text "X"
pygame.draw.rect(self.screen, self.GRAY, maximize_button_rect) # Renders the maximize button
pygame.draw.rect(self.screen, self.GRAY, minimize_button_rect) # Renders the minimize button
self.WindowEvent(self.screen)
return None
def Update(self, mouse_pos: [int, int], mouse_pressed: [bool, bool, bool]):
# width/2 <= mouse[0] <= width/2+140 and height/2 <= mouse[1] <= height/2+40
if not self.running:
return None
x = self.rect.x
y = self.rect.y
size = self.rect.size
x_ = self.x_button
minimize_ = self.minimize_button
maximize_ = self.maximize_button
if x_.x <= mouse_pos[0] <= x_.x + 32 and x_.y <= mouse_pos[1] <= x_.y + 32:
self.select_x_button = self.RED
if mouse_pressed[0] == True:
self.running = False
if not x_.x <= mouse_pos[0] <= x_.x + 32 and x_.y <= mouse_pos[1] <= x_.y + 32:
self.select_x_button = self.GRAY
if maximize_.x <= mouse_pos[0] <= maximize_.x + 32 and maximize_.y <= mouse_pos[1] <= maximize_.y + 32:
winSize = self.screen.get_rect
self.wsize = winSize
return None
and the main project file main.py
from apps.window import Window
if not __name__ == "__main__":
sys.exit()
pygame.init()
pygame.font.init()
WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
YELLOW = ( 0, 255, 255)
PINK = (255, 0, 255)
# Resolution: 1920, 1080 # Average Display Resolution
# Resolution: 720, 720 # The Default Resolution
resolution = [ 720, 720 ] # The resolution for the program
screen = pygame.display.set_mode(resolution, pygame.RESIZABLE)
def WindowEvent(screen: pygame.display):
text = "Hello World!"
textFont = pygame.font.Font(None, 32)
textColour = (255, 0, 0)
textRender = textFont.render(text, True, textColour)
screen.blit(textRender, (200, 200))
return None
Running = True
window = Window([50, 50], [600, 400], screen, WindowEvent)
while Running:
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running = False
window.Draw()
window.Update(mouse_pos, mouse_pressed)
pygame.display.update()
screen.fill((0,0,0))
sys.exit()
I’ve tried to check for improper indentation and i’ve checked the value type.
In
winSize = self.screen.get_rect
self.wsize = winSize
you’re reassigning self.wsize
to be the function self.screen.get_rect
(since you’re not calling that function), and trying to index into that function later on, you get your exception.
You’d want to call that function, and since it returns a Rect
and we know you want a 2-tuple of width, height:
rect = self.screen.get_rect()
self.wsize = rect.size
This problem has nothing to do with pygame. You just pass an object somewhere instead of a list.