A
A
Anire19852022-03-13 19:53:05
PyQt
Anire1985, 2022-03-13 19:53:05

How to assign additional parameters to an already created object?

I created a main.ui form in the designer that already has buttons
pushButton_1 pushButton_3 pushButton_5
and just import all the code into my application main.py
Now I need to add additional functionality to these buttons, namely the sound when hovering over the button.
Something like hover. How to implement it?

All my efforts boiled down to the fact that a new button was created, and parameters were not added to the one created in the designer.

from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5 import QtCore, QtWidgets, uic, QtGui
import pygame
from pygame import mixer

mixer.init() # звуковой движок

Form, Window = uic.loadUiType("HL_main_new.ui")
app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)
window.setWindowFlags(QtCore.Qt.FramelessWindowHint)

class HoverButton(QPushButton):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
# это тот самый параметр которы я хочу добавить.
    def enterEvent(self, e):
        pygame.mixer.music.load('./res/musik/btn_hover.mp3')
        pygame.mixer.music.play(0)

    def leaveEvent(self, e):
        pass
form.pushButton_12 = HoverButton(window)
window.show()

Perhaps there is some other way? I'm bad with OOP. I'm just starting to figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anire1985, 2022-03-14
@Anire1985

Found the answer on stackoverflow by request

Perform actions on hover over QPushButton PyQt5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question