You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python的PyQt5中实现关于窗口的关闭功能?

问题解决:PyQt5关于窗口按钮无法关闭窗口

问题描述

我有一个main.py文件,负责加载PyQt5的basic.ui(主界面)和about.ui(关于窗口),其中about.ui是通过Qt Designer创建的,窗口内包含一个点击后应关闭窗口的按钮。目前点击主界面中的关于按钮可以成功打开about窗口,但无法通过该窗口的按钮关闭它。希望所有控制逻辑都在main.py中实现,求解决方法和示例代码。

错误原因

  • 在_aboutWindow方法中,重新创建了一个全新的QPushButton实例,而非绑定about.ui界面上已存在的按钮,导致界面上的OK按钮根本没有关联任何点击事件。
  • close_about_window方法仅输出日志,没有执行关闭about窗口的实际操作。

修正后的main.py代码

from PyQt5 import QtWidgets, uic
import sys


class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('basic.ui', self)
        # 绑定主界面的关于按钮触发事件
        self.actionAbout.triggered.connect(self._aboutWindow)
        self.show()

    def _aboutWindow(self):
        self.aboutWindow = QtWidgets.QDialog()
        uic.loadUi('about.ui', self.aboutWindow)
        # 绑定about窗口中已有的OK按钮点击事件
        self.aboutWindow.pushButton.clicked.connect(self.close_about_window)
        self.aboutWindow.show()

    def close_about_window(self):
        print('Close About Window')
        # 执行关闭about窗口的操作
        self.aboutWindow.close()


app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

关键修改点

  • 移除了重新创建QPushButton的代码,改为直接通过self.aboutWindow.pushButton获取about.ui中设计好的按钮,绑定点击事件。
  • 在close_about_window方法中添加self.aboutWindow.close(),真正触发窗口关闭操作。

原文件参考

about.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>420</width>
    <height>220</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>About MAC address spoofing</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>330</x>
     <y>190</y>
     <width>80</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>OK</string>
   </property>
  </widget>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>70</y>
     <width>400</width>
     <height>50</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>DejaVu Sans</family>
     <pointsize>11</pointsize>
    </font>
   </property>
   <property name="text">
    <string>This program</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

about.py(由about.ui转换而来)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mac_about.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(420, 220)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(330, 190, 80, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(10, 70, 400, 50))
        font = QtGui.QFont()
        font.setFamily("DejaVu Sans")
        font.setPointSize(11)
        self.label.setFont(font)
        self.label.setObjectName("label")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "About MAC address spoofing"))
        self.pushButton.setText(_translate("Dialog", "OK"))
        self.label.setText(_translate("Dialog", "This program"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

内容的提问来源于stack exchange,提问作者user21379959

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:22:05