PyQt:按下按钮后不显示第二个窗口

时间:2019-01-29 22:30:21

标签: python pyqt pyqt5

尝试制作我的第一个PyQt5应用程序,当我单击我的按钮时,代码会运行,但我的选项窗口不会出现在屏幕上。

#!/usr/bin/env python3

import sys

from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QLabel, QPushButton, QVBoxLayout
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtCore import QSize


class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.initUI()

    def initUI(self):               
        self.setWindowTitle('Main Window')
        label = QLabel(self)
        pixmap = QPixmap('background.png')
        label.setPixmap(pixmap)

        self.resize(pixmap.width(), pixmap.height())
        self.initButtons()
        self.center()
        self.show()

    def initButtons(self):
        self.button = QPushButton('', self)
        # button style things...
        self.button.clicked.connect(self.handleButton)
        self.dialog = OptionsWindow(self)

    def handleButton(self):
        self.dialog.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


class OptionsWindow(QWidget):
    def __init__(self, parent=None):
        super(OptionsWindow, self).__init__(parent)

        self.setWindowTitle("Options")
        self.resize(200,200)
        self.center()
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我想念什么吗?我尝试了各种操作,例如删除“居中”功能,删除背景图像等,但是在按按钮时仍然没有第二个窗口

1 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from PyQt5.QtWidgets import (QWidget, QDesktopWidget, QApplication, 
                             QLabel, QPushButton, QVBoxLayout)
from PyQt5.QtGui     import QPixmap, QIcon
from PyQt5.QtCore    import QSize


class MainWindow(QWidget):                           
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)    
        self.initUI()

    def initUI(self):               
        self.setWindowTitle('Main Window')
        label  = QLabel(self)
        pixmap = QPixmap('D:/_Qt/img/pyqt.jpg')
        label.setPixmap(pixmap)

        self.resize(pixmap.width(), pixmap.height())
        self.initButtons()
        self.center()
        self.show()

    def initButtons(self):
        self.button = QPushButton('button', self)
        # button style things...
        self.button.clicked.connect(self.handleButton)
#        self.dialog = OptionsWindow(self)                 # ---

    def handleButton(self):
        self.dialog = OptionsWindow()                      # +++ - self
        self.dialog.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


class OptionsWindow(QWidget):
    def __init__(self, parent=None):
        super(OptionsWindow, self).__init__(parent)

        self.setWindowTitle("Options")
        self.resize(200,200)
        self.center()
#        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

enter image description here