recherche

Maison  >  Questions et réponses  >  le corps du texte

python - PyQt5点击button如何弹出新窗口?

怎么通过点击button,来打开一个新的窗口?
是写在同一个脚本里,还是把新窗口的代码放在另一个脚本里,然后通过点击按钮来运行?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class FirstWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.windowUI()

    def windowUI(self):
        self.setWindowTitle("Login")
        self.textfield()
        self.center()

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

    def textfield(self):
        QToolTip.setFont(QFont('SansSerif', 12))
        user = QLabel("User:")
        userEdit = QLineEdit()
        userEdit.setToolTip("请输入你的帐号")

        passWord = QLabel("PassWord:")
        passWordEdit = QLineEdit()
        passWordEdit.setToolTip("请输入你的密码")

        grid = QGridLayout()
        grid.setSpacing(0)

        grid.addWidget(user, 0, 0)
        grid.addWidget(userEdit, 1, 0)
        grid.addWidget(passWord, 2, 0)
        grid.addWidget(passWordEdit, 3, 0)
        empty = QLabel()
        grid.addWidget(empty, 4, 0)

        btn_logon = QPushButton("Log on")
        btn_quit = QPushButton("Quit")
        grid.addWidget(btn_logon, 5, 0, 1, 2)
        grid.addWidget(btn_quit, 6, 0, 1, 2)

        btn_logon.clicked.connect(self.onclick)
        btn_quit.clicked.connect(quit)

        self.setLayout(grid)

    def onclick(self):
        newWindow = SecondWindow()
        newWindow.show()
        newWindow.exec_()

class SecondWindow(QWidget):
    def __init__(self):
        super().__init__
        self.newWindowUI

    def newWindowUI(self):
        self.resize(300,300)
        self.move(200,200)

if __name__ == "__main__":
    App = QApplication(sys.argv)
    ex = FirstWindow()
    ex.show()
    sys.exit(App.exec_())
黄舟黄舟2804 Il y a quelques jours1890

répondre à tous(2)je répondrai

  • ringa_lee

    ringa_lee2017-04-17 17:55:12

    Si la quantité de code n'est pas importante, mettez-le simplement dans un seul fichier. S'il y a beaucoup de code, compte tenu de la commodité de la maintenance, il est préférable de le mettre dans deux fichiers pour ce type de déclenchement d'événement et d'interface. la commutation utilise généralement un signal - Fabriqué par slot, la sémantique semble très claire

    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore import *
    
    
    class FirstWindow(QWidget):
    
        close_signal = pyqtSignal()
        def __init__(self, parent=None):
            # super这个用法是调用父类的构造函数
            # parent=None表示默认没有父Widget,如果指定父亲Widget,则调用之
            super(FirstWindow, self).__init__(parent)
            self.resize(100, 100)
            self.btn = QToolButton(self)
            self.btn.setText("click")
    
        def closeEvent(self, event):
            self.close_signal.emit()
            self.close()
    
    
    class SecondWindow(QWidget):
        def __init__(self, parent=None):
            super(SecondWindow, self).__init__(parent)
            self.resize(200, 200)
            self.setStyleSheet("background: black")
    
        def handle_click(self):
            if not self.isVisible():
                self.show()
    
        def handle_close(self):
            self.close()
    
    
    if __name__ == "__main__":
        App = QApplication(sys.argv)
        ex = FirstWindow()
        s = SecondWindow()
        ex.btn.clicked.connect(s.handle_click)
        ex.btn.clicked.connect(ex.hide)
        ex.close_signal.connect(ex.close)
        ex.show()
        sys.exit(App.exec_())

    répondre
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:55:12

    -- codage : utf-8 --

    importer sys
    depuis PyQt5.QtWidgets import *
    depuis PyQt5.QtCore import *

    classe FirstWindow(QWidget) :

    close_signal = pyqtSignal()
    def __init__(self, parent=None):
        # super这个用法是调用父类的构造函数,parent=None表示默认没有父Widget,如果有则调用之
        super(FirstWindow, self).__init__(parent)
        self.resize(800, 600)
        self.setMaximumSize(QSize(800, 600))
        self.btn = QToolButton(self)
        self.btn.setText("click")
        self.two = SecondWindow()
        self.btn.clicked.connect(self.two.handle_click)
        #self.btn.clicked.connect(self.hide)
        self.close_signal.connect(self.closeSlot)
        print("FirstWindow")
    
    def closeEvent(self, event):
        print("close signal emit by FirstWindow")
        self.close_signal.emit()
        #self.close()
    
    def closeSlot(self):
        print("close signal slot to SecondWindow")
        self.two.close()
    

    classe SecondWindow(QWidget) :

    close_signal = pyqtSignal()
    def __init__(self, parent=None):
        super(SecondWindow, self).__init__(parent)
        self.resize(800, 600)
        self.setMaximumSize(QSize(800, 600))
        self.setStyleSheet("background: black")
        self.close_signal.connect(self.closeSlot)
        
    def handle_click(self):
        if not self.isVisible():
            self.show()
            print("SecondWindow")
    
    def closeEvent(self, event):
        print("close signal emit by SecondWindow")
        self.close_signal.emit()
        #self.close()
    
    def closeSlot(self):
        print("close signal slot to FirstWindow")
        one.close()
    

    if nom == "__main__":

    App = QApplication(sys.argv)
    one = FirstWindow()
    one.show()
    sys.exit(App.exec_())
    

    répondre
    0
  • Annulerrépondre