Python Forum
Calling functions from within a class: PYQT6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling functions from within a class: PYQT6
#5
I think it is a bad idea using setGeometry() to size and position your button in the window. I also think it is a bad idea passing the size and position as a parameter when you create the button. The ​common practice to use a geometry manager to place widgets in a window.

I don't think the button should have a button_visibility() method and I don't think the button should bind the clicked signal to call this method. What good is a button that hides itself? Buttons should not do things like set their size or bind their clicked events. Buttons should be generic reusable things. The application should use the button API to bind the events.

This is how I would write your code. I use PySide instead of PyQt, so there are some minor differences related to that. I also removed the style sheets and anything that is not absolutely required to demonstrate the issue related to the post topic. And just for fun I changed the hover event to not only emit a signal but change the button icon.
import sys
import PySide6.QtWidgets as QtWidgets
import PySide6.QtCore as QtCore
import PySide6.QtGui as QtGui

class HoverButton(QtWidgets.QPushButton):
    """A Pushbutton that changes it's icon when you hover"""
    hover = QtCore.Signal(bool)
 
    def __init__(self, parent, icon, hover_icon):
        super().__init__(parent)
        self.icon = icon
        self.hover_icon = hover_icon
        self.setMouseTracking(True)
        self.setIcon(self.icon)
 
    def enterEvent(self, event):
        self.setIcon(self.hover_icon)
        self.hover.emit(True)
 
    def leaveEvent(self, event):
        self.setIcon(self.icon)
        self.hover.emit(False)
 
class Window(QtWidgets.QWidget):
    """A window that demonstrates how to use my hover button"""
    def __init__(self):
        super().__init__()
        play_button = HoverButton(
                self,
                QtGui.QIcon("ttt_x.png"),
                QtGui.QIcon("ttt_o.png")
        )
        # Bind events outside the button
        play_button.hover.connect(lambda flag: print("Hover", flag))
        play_button.clicked.connect(lambda flag: play_button.setVisible(False))

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(play_button)
 
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
Reply


Messages In This Thread
RE: Calling functions from within a class: PYQT6 - by deanhystad - Dec-09-2021, 12:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calling functions by making part of their name with variable crouzilles 4 871 Nov-02-2023, 12:25 PM
Last Post: noisefloor
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 1,082 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Calling a base class variable from an inherited class CompleteNewb 3 1,741 Jan-20-2022, 04:50 AM
Last Post: CompleteNewb
  Calling a class from a function jc4d 5 1,867 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Why built in functions are defined as class? quazirfan 5 2,880 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  should I ... go class or stick with functions? 3Pinter 4 2,140 Nov-14-2020, 10:40 AM
Last Post: 3Pinter
Question trouble with functions "def", calling/defining them Duck_Boom 13 4,446 Oct-21-2020, 03:50 AM
Last Post: Duck_Boom
  NameError when calling a class method mfreudenberg 2 2,340 Sep-25-2020, 07:40 AM
Last Post: mfreudenberg
  calling on a method from one class into another class which is not a child NABA 5 2,879 Apr-29-2020, 07:49 PM
Last Post: deanhystad
  using class functions in roguelike game trousers1 3 2,600 Dec-02-2019, 08:22 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020