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: