Pyqt5中点击按钮弹出对话框

Pyqt5中点击按钮弹出对话框

4 年前 · 来自专栏 Python学习

编写串口界面的时候需要点击按钮,弹出另一个对话框进行参数配置。

示例代码如下:

# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QWidget, QApplication
# second ui
class SecondUI(QWidget):
    def __init__(self):
        super(SecondUI, self).__init__()
        self.resize(400, 300)
        self.setWindowTitle("Second UI")
        self.label = QtWidgets.QLabel(self)
        self.label.setGeometry(QtCore.QRect(100, 80, 181, 91))
        self.label.setText("Second UI")
# first ui
class FirstUI(QWidget):
    def __init__(self):
        super(FirstUI, self).__init__()
        self.resize(400, 300)
        self.setWindowTitle("First UI")
        self.changeP = QtWidgets.QPushButton(self)
        self.changeP.setGeometry(QtCore.QRect(100, 100, 181, 71))
        self.changeP.setText("Change to second ui")
        self.changeP.clicked.connect(self.open_second_ui)
    def open_second_ui(self):
        self.second_ui = SecondUI()
        self.second_ui.show()