pyqt:setCentralwidget与qpushbutton函数发生冲突



因此,我从项目中得出了这个问题,即从幻灯片窗口窗口类别I注入的setCentralWidget在主窗口类中注入的setCentralWidget会导致按钮的功能(即打开一个新的widget窗口(从主窗口完全不工作。

如果我删除了setCentralwidget按钮正常工作,因此在不损害任何功能的情况下,我应该使用哪种方法?我应该使用不同形式的调用幻灯片窗口小部件类吗?

一如既往的任何帮助将不胜感激!谢谢!

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 = ["imagesslide1.jpg", "imagesslide2.jpg", "imagesslide3.jpg",
 "imagesslide4.jpg", "imagesslide5.jpg"]
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Form = MainWindow(image_files)
    Form.show()
    sys.exit(app.exec_())

问题出现了,因为首先将按钮放置在中央网络上,解决方案是将按钮放在centralwidget中,即幻灯片。

您必须更改:

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)

self.groupBox = QtGui.QGroupBox(MainWindow)

to:

self.groupBox = QtGui.QGroupBox(MainWindow.slides_widget)

完成代码:

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 = ["imagesslide1.jpg", "imagesslide2.jpg", "imagesslide3.jpg",
 "imagesslide4.jpg", "imagesslide5.jpg"]
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    Form = MainWindow(image_files)
    Form.show()
    sys.exit(app.exec_())

相关内容

  • 没有找到相关文章

最新更新