Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改一个类,以便当鼠标悬停在该类上时,它会更改颜色-Pygame_Python_Pygame - Fatal编程技术网

Python 更改一个类,以便当鼠标悬停在该类上时,它会更改颜色-Pygame

Python 更改一个类,以便当鼠标悬停在该类上时,它会更改颜色-Pygame,python,pygame,Python,Pygame,我有这个类来定义和创建Pygame中的按钮。此时,当鼠标悬停在按钮上时,它不会改变颜色。我试图在类中添加另一个方法,以便在鼠标悬停在按钮上时,它会改变颜色。但是,当我运行HoveredOver方法时,它不会更改按钮的颜色 import pygame as pg import sys import time # All pygame stuff under here pg.init() # Font definitions backFont = pg.font.SysFont("monospa

我有这个类来定义和创建Pygame中的按钮。此时,当鼠标悬停在按钮上时,它不会改变颜色。我试图在类中添加另一个方法,以便在鼠标悬停在按钮上时,它会改变颜色。但是,当我运行HoveredOver方法时,它不会更改按钮的颜色

import pygame as pg
import sys
import time

# All pygame stuff under here
pg.init()

# Font definitions
backFont = pg.font.SysFont("monospace", 40)
titleFont = pg.font.SysFont("garamond", 100)
cipherFont = pg.font.SysFont("garamond", 50)
buttonFont = pg.font.SysFont("garamond", 25)
bigFont = pg.font.SysFont("garamond", 100)
Font = pg.font.SysFont(None, 32)
inputFont = pg.font.SysFont('consola', 35)
errorFont = pg.font.SysFont('tahoma', 20)
diagramFont = pg.font.SysFont('courier new', 25)

# Colour definitions
BackGray = pg.Color('gray60')
screenGray = pg.Color('gray80')
buttonGray1 = pg.Color('gray40')
buttonGray2 = pg.Color('gray50')
buttonGray3 = pg.Color('gray30')
textColour = pg.Color('navy')

# Screen size set
screen = pg.display.set_mode((800, 600))

# Clock initiated to allow regular typing speeds
clock = pg.time.Clock()

# Change button class so that when the mouse pos is sent through, changes colour ======================
class Button(pg.sprite.Sprite):
    def __init__(self, text, x, y, width, height, enabled):
        super().__init__()
        self.colour = buttonGray2
        self.image = pg.Surface((width, height))
        self.image.fill(buttonGray2)
        self.rect = self.image.get_rect()
        txt = buttonFont.render(text, True, textColour)
        txtRect = txt.get_rect(center=self.rect.center)
        self.image.blit(txt, txtRect)
        self.rect.topleft = x, y
        self.enabled = enabled

    def isPressed(self, event):
        if self.enabled == True:
            if event.type == pg.MOUSEBUTTONDOWN:
                # MOUSE... events have an event.pos attribute (the mouse position)
                # which you can pass to the collidepoint method of the rect.
                if self.rect.collidepoint(event.pos):
                    return True
        return False
    def HoveredOver(self,event):
        print("Ocurring")
        if event.type == pg.MOUSEMOTION:
            if self.rect.collidepoint(event.pos):
                print("Hoveredover")
                self.colour = buttonGray1
            else:
                self.colour = buttonGray2

def FrontPage():
    # Back screen
    screen.fill(screenGray)

    # Button definition for continuing
    Continue = Button('Continue', 105, 455, 120, 50, True)
    buttonsGroup = pg.sprite.Group(Continue)

    # Pygane while loop for events
    while True:
        for event in pg.event.get():
            mouseX, mouseY = pg.mouse.get_pos()
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            elif Continue.isPressed(event):
                print("Continue")
            Continue.HoveredOver(event)

        buttonsGroup.draw(screen)

        pg.display.flip()
        clock.tick(60)

FrontPage()
我如何使此方法能够改变按钮的颜色


提前谢谢

仅仅更改精灵的
颜色
属性是不够的,您还必须更改
图像
,因为pygame在您调用
按钮Group.draw(screen)
时会将图像显示在屏幕上

我会在
\uuuu init\uuuu
方法中创建图像,或者将它们作为参数传递,然后在鼠标悬停在按钮上方时,通过将当前图像分配给
self.image
来交换它们

import pygame as pg
import sys
import time

# All pygame stuff under here
pg.init()

# Font definitions
backFont = pg.font.SysFont("monospace", 40)
titleFont = pg.font.SysFont("garamond", 100)
cipherFont = pg.font.SysFont("garamond", 50)
buttonFont = pg.font.SysFont("garamond", 25)
bigFont = pg.font.SysFont("garamond", 100)
Font = pg.font.SysFont(None, 32)
inputFont = pg.font.SysFont('consola', 35)
errorFont = pg.font.SysFont('tahoma', 20)
diagramFont = pg.font.SysFont('courier new', 25)

# Colour definitions
BackGray = pg.Color('gray60')
screenGray = pg.Color('gray80')
buttonGray1 = pg.Color('gray40')
buttonGray2 = pg.Color('gray50')
buttonGray3 = pg.Color('gray30')
textColour = pg.Color('navy')

# Screen size set
screen = pg.display.set_mode((800, 600))

# Clock initiated to allow regular typing speeds
clock = pg.time.Clock()

# Change button class so that when the mouse pos is sent through, changes colour ======================
class Button(pg.sprite.Sprite):
    def __init__(self, text, x, y, width, height, enabled):
        super().__init__()
        self.colour = buttonGray2
        self.image = pg.Surface((width, height))
        self.image.fill(buttonGray2)
        self.rect = self.image.get_rect()
        txt = buttonFont.render(text, True, textColour)
        txtRect = txt.get_rect(center=self.rect.center)
        self.image.blit(txt, txtRect)
        self.rect.topleft = x, y
        self.enabled = enabled

    def isPressed(self, event):
        if self.enabled == True:
            if event.type == pg.MOUSEBUTTONDOWN:
                # MOUSE... events have an event.pos attribute (the mouse position)
                # which you can pass to the collidepoint method of the rect.
                if self.rect.collidepoint(event.pos):
                    return True
        return False
    def HoveredOver(self,event):
        print("Ocurring")
        if event.type == pg.MOUSEMOTION:
            if self.rect.collidepoint(event.pos):
                print("Hoveredover")
                self.colour = buttonGray1
            else:
                self.colour = buttonGray2

def FrontPage():
    # Back screen
    screen.fill(screenGray)

    # Button definition for continuing
    Continue = Button('Continue', 105, 455, 120, 50, True)
    buttonsGroup = pg.sprite.Group(Continue)

    # Pygane while loop for events
    while True:
        for event in pg.event.get():
            mouseX, mouseY = pg.mouse.get_pos()
            if event.type == pg.QUIT:
                pg.quit()
                sys.exit()
            elif Continue.isPressed(event):
                print("Continue")
            Continue.HoveredOver(event)

        buttonsGroup.draw(screen)

        pg.display.flip()
        clock.tick(60)

FrontPage()

如果我错了,请纠正我,但我相信行
super().init()
应该是
super()。\uuu init\uuu()
@MichealO'Dwyer你在哪里看到
super().init()
?我可能看到了
super().init()
没关系。对不起,麻烦了。