-2

I think I'm a bit tired and probably missing the obvious, so please, go easy on me ;)

I'm trying to troubleshoot an issue, my custom QPushButton class object is being displayed in a separate window (see attached image). If I implement the button via a function and not a class, then it appears within the main window. However, since the application has several buttons (I've removed buttons to simplify the code and aid troubleshooting), to minimise the repetition of code, I want to implement a reusable class.

Clearly, the code is functioning correctly: the button is showing. My question is, how do I link my ButtonClass object to my Window object?

Here is my code:

from PyQt6.QtWidgets import QApplication, QPushButton, QWidget
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import *

import sys

class ButtonClass(QPushButton):
    def __init__(self, set_image, set_button_geometry, set_button_resize, set_button_visible):
        super().__init__()

        # Instance variables assigned with arguments
        self.set_image = set_image
        self.set_button_geometry = set_button_geometry
        self.set_button_resize = set_button_resize
        self.set_button_visible = set_button_visible

        # Initialise instance of QPushButton
        self.button = QPushButton()

        # Declare variable, inherit QPushButton->QAbstractButton->QWidget functions
        self.button_attributes()

    def button_attributes(self):
        

        # Assign icon to QPushButton object and insert custom image
        self.button.setIcon(QIcon(self.set_image))

        # Specify button geometry
        self.button.setGeometry(self.set_button_geometry[0],
                                self.set_button_geometry[1],
                                self.set_button_geometry[2],
                                self.set_button_geometry[3])

        # Specify Icon size
        self.button.setIconSize(QSize(self.set_button_resize[0],
                                      self.set_button_resize[1]))

        # Stylesheet attributes
        self.button.setStyleSheet("border: 0px")

        # Set button visibility
        self.button.setVisible(self.set_button_visible)


class Window(QWidget):
    def __init__(self):
        super().__init__()
        # Define window title
        self.setWindowTitle("Soular")

        # Define window height
        self.setFixedHeight(700)

        # Define window width
        self.setFixedWidth(400)

        # window stylesheet defines window background colour
        self.setStyleSheet("background-color:'#323232'")
        self.move(10, 10)

        # initialises play_button from class 'Button_class'
        self.play_button = ButtonClass('Images/Play_standard.png', [165, 580, 70, 70], [70, 70], True)


app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())

And this is what the output:

Output two PyQT windows

  • 2
    Why are you creating *another* button *inside* a button class? – musicamante Dec 07 '21 at 14:00
  • Are you referring to the QPushButton reference 'class ButtonClass(QPushButton)' and 'self.button_attributes()'? – Ian Thompson Dec 07 '21 at 14:04
  • 1
    @IanThompson TYPO: remove `self.button = QPushButton()` and change `self.button` to `self` – eyllanesc Dec 07 '21 at 14:06
  • Oh, of course, doh! Thank you, I appreciate the help. Do you have any idea why my button still appears in a separate window? – Ian Thompson Dec 07 '21 at 14:13
  • 1
    @IanThompson because you're not creating the widget (the button) with a parent, or, more properly, you are not adding it to a [layout manager](https://doc.qt.io/qt-5/layout.html) as you should. – musicamante Dec 07 '21 at 14:15

1 Answers1

-1

You're missing the part that you need to tell the QPushButton which window it should be a part of. See for example this question, where the QPushButton receives the QWidget as an argument. In your case, you can tell ButtonClass that it is located within Window, by passing a reference to Window (I'll call the parameter parent here) to the ButtonClass.__init__-method and passing parent to the QPushButton.__init__-method, like this:

def __init__(self, parent, set_image, set_button_geometry, set_button_resize, set_button_visible):
        super().__init__(parent)

and by calling ButtonClass with a reference to Window, like this:

self.play_button = ButtonClass(self, 'Images/Play_standard.png', [165, 580, 70, 70], [70, 70], True)
eandklahn
  • 537
  • 4
  • 8