So I deduced down to this problem from my project that the setCentralWidget from the Slides Widget class I injected in the Main Window class causes the function of the buttons (i.e. opening a new Widget window) from the Main Window to not work at all.
If I remove the setCentralWidget the buttons work fine so without compromising any of the functions, what approach should I use for this? Should I use a different form of calling the Slides Widget class?
Any help would be appreciated, as always! Thanks!
from PyQt4 import QtCore, QtGui
import sys
from functools import partial
class MainWindow(QtGui.QMainWindow):
def __init__(self, image_files, parent=None):
super(MainWindow, self).__init__()
self.setupUi(self)
self.slides_widget = Slides(image_files, self)
#If you enable this down below, pushButton will not function
#and instead the slideshow will pop up and function correctly
#self.setCentralWidget(self.slides_widget)
def setupUi(self, MainWindow):
MainWindow.resize(1278, 688)
#MainWindow.setStyleSheet(self.styledata)
self.groupBox = QtGui.QGroupBox(MainWindow)
self.groupBox.setGeometry(QtCore.QRect(490, 220, 120, 371))
self.groupBox.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.groupBox.setFlat(False)
self.groupBox.setCheckable(False)
self.pushButton_Desc = QtGui.QPushButton(self.groupBox)
self.pushButton_Desc.setGeometry(QtCore.QRect(20, 30, 71, 61))
self.pushButton_Desc.clicked.connect(partial(self.DescWindow))
self.groupBox.raise_()
self.NewWindow = QtGui.QWidget()
def DescWindow(self):
self.NewWindow.show();
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
super(Slides, self).__init__(parent)
self.image_files = image_files
self.label = QtGui.QLabel("", self)
self.label.setGeometry(65, 225, 423, 363)
#buttons to rewind and forward
self.button = QtGui.QPushButton(". . .", self)
self.button.setGeometry(200, 100, 140, 30)
self.button.clicked.connect(self.timerEvent)
self.timer = QtCore.QBasicTimer()
self.step = 0
self.delay = 3000 #ms
def timerEvent(self, e=None):
if self.step >= len(self.image_files):
self.timer.start(self.delay, self)
self.step = 0
return
self.timer.start(self.delay, self)
file = self.image_files[self.step]
image = QtGui.QPixmap(file)
self.label.setPixmap(image)
#self.setWindowTitle("{} --> {}".format(str(self.step), file))
self.step += 1
image_files = ["images\slide1.jpg", "images\slide2.jpg", "images\slide3.jpg",
"images\slide4.jpg", "images\slide5.jpg"]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Form = MainWindow(image_files)
Form.show()
sys.exit(app.exec_())
The problem arises because first place the button and on this is placed the CentralWidget, the solution is to place the button in the CentralWidget, ie in Slides.
You must change:
def __init__(self, image_files, parent=None):
super(MainWindow, self).__init__()
self.setupUi(self)
self.slides_widget = Slides(image_files, self)
to:
def __init__(self, image_files, parent=None):
super(MainWindow, self).__init__()
self.slides_widget = Slides(image_files, self)
self.setupUi(self)
and
self.groupBox = QtGui.QGroupBox(MainWindow)
to:
self.groupBox = QtGui.QGroupBox(MainWindow.slides_widget)
Complete code:
from PyQt4 import QtCore, QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, image_files, parent=None):
super(MainWindow, self).__init__()
self.slides_widget = Slides(image_files, self)
self.setupUi(self)
def setupUi(self, MainWindow):
MainWindow.resize(1278, 688)
#MainWindow.setStyleSheet(self.styledata)
self.groupBox = QtGui.QGroupBox(MainWindow.slides_widget)
self.groupBox.setGeometry(QtCore.QRect(490, 220, 120, 371))
self.groupBox.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.groupBox.setFlat(False)
self.groupBox.setCheckable(False)
self.pushButton_Desc = QtGui.QPushButton(self.groupBox)
self.pushButton_Desc.setGeometry(QtCore.QRect(20, 30, 71, 61))
self.pushButton_Desc.clicked.connect(self.DescWindow)
self.setCentralWidget(self.slides_widget)
self.groupBox.raise_()
self.NewWindow = QtGui.QWidget()
def DescWindow(self):
self.NewWindow.show();
class Slides(QtGui.QWidget):
def __init__(self, image_files, parent=None):
super(Slides, self).__init__(parent)
self.image_files = image_files
self.label = QtGui.QLabel("", self)
self.label.setGeometry(65, 225, 423, 363)
#buttons to rewind and forward
self.button = QtGui.QPushButton(". . .", self)
self.button.setGeometry(200, 100, 140, 30)
self.button.clicked.connect(self.timerEvent)
self.timer = QtCore.QBasicTimer()
self.step = 0
self.delay = 3000 #ms
def timerEvent(self, e=None):
if self.step >= len(self.image_files):
self.timer.start(self.delay, self)
self.step = 0
return
self.timer.start(self.delay, self)
file = self.image_files[self.step]
image = QtGui.QPixmap(file)
self.label.setPixmap(image)
#self.setWindowTitle("{} --> {}".format(str(self.step), file))
self.step += 1
image_files = ["images\slide1.jpg", "images\slide2.jpg", "images\slide3.jpg",
"images\slide4.jpg", "images\slide5.jpg"]
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
Form = MainWindow(image_files)
Form.show()
sys.exit(app.exec_())