问题描述
我正在使用 QT Designer 没有任何问题,但是今天我开始了全新的 ubuntu 18.04 安装,但是这次当我从终端运行 PyQt5 程序时,它们没有显示任何窗口,从 atom-runner 运行时出现同样的问题(它没有甚至显示任何错误)
I was working with QT Designer with no issues but today i started a fresh ubuntu 18.04 install, but this time when i run the PyQt5 programs from terminal they doesnt show any windows, same issue when running from atom-runner (It doesnt even show any error)
我将 .ui 文件从 Qt Designer 保存后直接使用 pyuic5 导出为 .py,尝试了一个简单的空白窗口和同样的问题
I exported the .ui files into .py using pyuic5 directly after saving them from Qt Designer, tried a simple blank window and same problem
知道如何解决这个问题吗?
Any idea how can i fix this?
这是一个代码示例,一个应该显示但由于某种原因它不会显示的简单窗口
This is an example of the code, a simple window that should show but for some reason it wont
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'label.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(315, 142) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(20, 20, 371, 111)) font = QtGui.QFont() font.setPointSize(45) self.label.setFont(font) self.label.setObjectName("label") MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "DA LABEL"))
推荐答案
要生成可以使用 pyuic 显示窗口的代码,您必须使用 -x 选项:
To generate a code that can show a window using pyuic you must use the -x option:
pyuic5 input.ui -o output.py -x
使用 -x 的上一条命令将以下代码添加到文件末尾:
The previous command using the -x adds the following code to the end of the file:
if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())