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

PyCharm+Qt Designer开发:Windows双击运行窗口闪退求助

Windows下双击PyQt5 GUI程序窗口立即关闭的解决方法

问题现象

  • Windows系统双击.py/.pyw文件时,程序窗口瞬间关闭或根本不显示
  • PyCharm调试、Windows控制台运行python 文件名.py时程序正常
  • 带input()的命令行脚本双击运行无问题
  • Python与.py文件关联正常,python -v输出大量日志但不确定是否相关

核心原因

双击运行时,程序的工作目录不是脚本所在目录,导致uic.loadUi('MainForm.ui', self)找不到UI文件,程序抛出异常后直接退出,因为没有控制台窗口所以看不到错误信息。

解决方案

修改代码,让脚本加载UI文件时使用脚本所在目录的绝对路径,而不是相对路径。具体修改后的代码如下:

import sys
import os
from PyQt5 import QtWidgets, uic

counter = 0


class MainForm(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        # 获取脚本所在目录的绝对路径
        script_dir = os.path.dirname(os.path.abspath(__file__))
        # 拼接UI文件的完整路径
        ui_path = os.path.join(script_dir, 'MainForm.ui')
        uic.loadUi(ui_path, self)
        self.IncBtn.clicked.connect(self.evt_inc_btn_clicked)

    def evt_inc_btn_clicked(self):
        global counter
        counter += 1
        self.CounterLbl.setText('Количество нажатий: ' + str(counter))


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWind = MainForm()
    MainWind.show()
    sys.exit(app.exec_())

验证方法(可选)

如果想确认是否是路径问题,可以临时添加错误捕获,把异常信息写入文件:

if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        MainWind = MainForm()
        MainWind.show()
        sys.exit(app.exec_())
    except Exception as e:
        with open('error_log.txt', 'w', encoding='utf-8') as f:
            f.write(str(e))

双击运行后,查看脚本目录下的error_log.txt,大概率会显示FileNotFoundError: [Errno 2] No such file or directory: 'MainForm.ui'。


原代码与UI文件

原Python代码

import sys
from PyQt5 import QtWidgets, uic

counter = 0


class MainForm(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        uic.loadUi('MainForm.ui', self)
        self.IncBtn.clicked.connect(self.evt_inc_btn_clicked)

    def evt_inc_btn_clicked(self):
        global counter
        counter += 1
        self.CounterLbl.setText('Количество нажатий: ' + str(counter))


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWind = MainForm()
    MainWind.show()
    sys.exit(app.exec_())

MainForm.ui内容

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainForm</class>
 <widget class="QMainWindow" name="MainForm">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>550</width>
    <height>350</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainForm</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="CounterLbl">
    <property name="geometry">
     <rect>
      <x>140</x>
      <y>40</y>
      <width>231</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Times New Roman</family>
      <pointsize>14</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Количество нажатий: 0</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignCenter</set>
    </property>
   </widget>
   <widget class="QPushButton" name="IncBtn">
    <property name="geometry">
     <rect>
      <x>210</x>
      <y>130</y>
      <width>91</width>
      <height>41</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Times New Roman</family>
      <pointsize>16</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Inc +1</string>
    </property>
    <property name="autoExclusive">
     <bool>false</bool>
    </property>
    <property name="autoDefault">
     <bool>false</bool>
    </property>
    <property name="default">
     <bool>true</bool>
    </property>
    <property name="flat">
     <bool>false</bool>
    </property>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 22:05:28