問題描述
我正在使用 QT Designer 沒有任何問題,但是今天我開始了全新的 ubuntu 18.04 安裝,但是這次當(dāng)我從終端運(yùn)行 PyQt5 程序時(shí),它們沒有顯示任何窗口,從 atom-runner 運(yùn)行時(shí)出現(xiàn)同樣的問題(它沒有甚至顯示任何錯(cuò)誤)
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 導(dǎo)出為 .py,嘗試了一個(gè)簡(jiǎn)單的空白窗口和同樣的問題
I exported the .ui files into .py using pyuic5 directly after saving them from Qt Designer, tried a simple blank window and same problem
知道如何解決這個(gè)問題嗎?
Any idea how can i fix this?
這是一個(gè)代碼示例,一個(gè)應(yīng)該顯示但由于某種原因它不會(huì)顯示的簡(jiǎn)單窗口
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
選項(xiàng):
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_())
這篇關(guān)于使用 QTdesigner 創(chuàng)建的 PyQt5 程序從終端打開時(shí)不顯示任何窗口的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!