pyqt5实现窗口跳转并关闭上一个窗口

关键在于要定义一个关闭窗体的函数colsewin()

然后将按键与该函数连接(connect)在一起即可

import sys
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication


class First(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()
    def closewin(self):
        self.close()

    def initUI(self):
        self.btn = QPushButton("Button", self)
        self.btn.move(30, 50)

        self.setGeometry(300, 300, 500, 500)
        self.setWindowTitle('Event sender')
        self.show()

class Second(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Get sender')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    a = First()
    b = Second()
    a.show()
    a.btn.clicked.connect(a.closewin)
    a.btn.clicked.connect(b.show)
    sys.exit(app.exec_())

UI.py文件

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

class Communicate(QObject):
    closeApp = pyqtSignal()

class Example1(QMainWindow):
    _startPos = None
    _endPos = None
    _isTracking = False

    def __init__(self):
        super().__init__()
        self.initUI()
        self.center()
    def closewin(self):
        self.close()

    def center(self, screenNum=0):
        '''多屏居中支持'''

        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()

        self.normalGeometry2 = QRect((screen.width() - size.width()) / 2 + screen.left(),
                                     (screen.height() - size.height()) / 2, size.width(), size.height())
        self.setGeometry((screen.width() - size.width()) / 2 + screen.left(),
                         (screen.height() - size.height()) / 2, size.width(), size.height())


    def initUI(self):
        self.setFixedSize(QSize(400, 300))

        self.setWindowFlags(Qt.FramelessWindowHint)  # 无边框
        btn1 = QPushButton("Button 1", self)
        btn1.move(80, 150)

        btn2 = QPushButton("Button 2", self)
        btn2.move(220, 150)
        btn3 = QPushButton("",self)
        btn3.move(0, 0)
        btn4 = QPushButton("", self)
        btn4.move(300, 260)

        btn1.clicked.connect(self.buttonClicked)
        btn2.clicked.connect(self.buttonClicked)
        btn3.clicked.connect(self.buttonClicked)
        btn3.clicked.connect(self.closewin)
        btn4.clicked.connect(self.buttonClicked)
        btn4.clicked.connect(self.closewin)

        self.statusBar()

        self.c = Communicate()
        self.c.closeApp.connect(self.close)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Event sender')
        self.window().setStyleSheet('''
            QPushButton{
                border:none;
                color:black;
                font-size:20px;
                height:40px;
                padding-left:5px;
                padding-right:10px;
                text-align:left;
            }
            QPushButton:hover{
                color:red;
                border:1px solid #F3F3F5;
                border-radius:10px;
                background:LightGray;
            }
            
            QMainWindow{
                background:gray;
                border-top:1px solid white;
                border-bottom:1px solid white;
                border-left:1px solid white;
                border-right:1px solid white;
                border-top-left-radius:12px;
                border-bottom-left-radius:12px;
                border-top-right-radius:12px;
                border-bottom-right-radius:12px;
            }
        ''')
        self.show()

    def buttonClicked(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text() + ' was pressed')
        if sender.text()=="Button 1":

            print("执行%s"%sender.text())
        elif sender.text()=="Button 2":
            print("执行%s"%sender.text())
        elif sender.text()=="":
            print("执行%s"%sender.text())

            import qt5
            self.qt = qt5.Example()
            self.qt._initUI()
       self.qt.center()
else: print("执行%s"%sender.text()) import qt5 self.qt=qt5.Example() self.qt._initUI()
       self.qt.center()
def mouseMoveEvent(self, e: QMouseEvent): # 重写移动事件 self._endPos = e.pos() - self._startPos self.move(self.pos() + self._endPos) def mousePressEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = True self._startPos = QPoint(e.x(), e.y()) def mouseReleaseEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = False self._startPos = None self._endPos = None if __name__ == '__main__': app = QApplication(sys.argv) ex = Example1() sys.exit(app.exec_())

qt5文件:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class Example(QWidget):
    _startPos = None
    _endPos = None
    _isTracking = False

    def __init__(self):
        super().__init__()
        self._initUI()
        self.center()
        #self.Use_Qss()
        #self.show()
    def closewindow(self):
        self.close()


    def center(self, screenNum=0):
        '''多屏居中支持'''

        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()

        self.normalGeometry2 = QRect((screen.width() - size.width()) / 2 + screen.left(),
                                     (screen.height() - size.height()) / 2, size.width(), size.height())
        self.setGeometry((screen.width() - size.width()) / 2 + screen.left(),
                         (screen.height() - size.height()) / 2, size.width(), size.height())

    def _initUI(self):
        #self.resize(800, 400)
        self.setWindowFlags(Qt.FramelessWindowHint)  # 无边框
        #def Use_Qss(self):
        palette = QPalette()
        palette.setBrush(QPalette.Background, QBrush(QPixmap("./p1.jpg")))
        self.setPalette(palette)
        #self.setObjectName("MainWindow")
        # #todo 1 设置窗口背景图片
        #self.setStyleSheet("#MainWindow{border-image:url(.\p1.jpg);}")
        # todo 2 设置窗口背景色
        #self.setStyleSheet("#MainWindow{background-color: gray}")
        btn1 = QPushButton("back", self)
        btn1.move(240, 300)
        btn1.setStyleSheet(
            '''QPushButton{
              border:none;
        color:white;
        background:blue;
        border:1px solid #F3F3F5;
        border-radius:10px;
        font-size:20px;
        height:40px;
        padding-left:5px;
        padding-right:10px;
        text-align:left;
    }
    QPushButton:hover{
        color:black;
        border:1px solid #F3F3F5;
        border-radius:10px;
        background:LightGray;
    }
        ''')
        btn1.clicked.connect(self.buttonClicked)
        btn1.clicked.connect(self.closewindow)
        self.lb1 = QLabel(self)
        qle = QLineEdit(self)

        qle.move(60, 300)
        self.lb1.move(60, 40)
        qle.setStyleSheet(
            '''QLineEdit{
                    border:1px solid gray;
                    background:cyan;
                    width:120px;
                    border-radius:10px;
                    padding:2px 4px;
                    font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; 
            }''')
        self.lb1.setStyleSheet(
            '''QLabel{
            color:red;
            font-size:60px;
            font-family: 微软雅黑,宋体,Arial,Helvetica,Verdana,sans-serif; 
            }
            '''
        )



        qle.textChanged[str].connect(self.onChanged)

        self.setGeometry(600, 300, 480, 370)
        self.setWindowTitle('单行文本')
        self.show()
    def buttonClicked(self):
        import UI
        self.qt = UI.Example1()
        self.qt.initUI()
     self.qt.center()
def onChanged(self, text): self.lb1.setText(text) self.lb1.adjustSize() def mouseMoveEvent(self, e: QMouseEvent): # 重写移动事件 self._endPos = e.pos() - self._startPos self.move(self.pos() + self._endPos) def mousePressEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = True self._startPos = QPoint(e.x(), e.y()) def mouseReleaseEvent(self, e: QMouseEvent): if e.button() == Qt.LeftButton: self._isTracking = False self._startPos = None self._endPos = None if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())

效果图:

 btn1按键透明化:

op =QGraphicsOpacityEffect()
# 设置透明度的值,0.0到1.0,最小值0是透明,1是不透明
op.setOpacity(0.5)
btn1.setGraphicsEffect(op)
   

效果图:

 

posted @ 2019-03-18 15:40  autotest_man  阅读(7092)  评论(0编辑  收藏  举报