设置PyQt5环境+配置+运行生成的.py程序
PyQt5安装与配置
安装
可以直接在pycharm设置中下载pyqt相关的库,主要有sip、PyQt5、PyQt5、PyQs†
组:Qt
程序:D:\python\untitled1\venv\Scripts\python.exee (我的 python 路径)
参数: -m PyQt5.uic.pyuic File -N alefiiName F 。文件名无扩展名 FileNameWithoutExtensionFileNameWithoutExtension.py
工作目录:文件名无扩展名 程序标题 文件标题 自动地址目录 2. QTdesigner 配置
3.
名称: QtDesigner
组:Qt
程序:D:\python\untitled1\venv\Scripts\python.exe
工作目录:P r o j e c t F i r ProjectFileDProjektFileD♷ Programs 后面的标题是 python 标题
查找模块规范“PyQt5.uic.pyuic”时出现错误解决问题(ModuleNotFoundError:没有名为“PyQt5”的模块)
问题是引入的python.exe文件不属于你的项目。如果出现这个问题,说明你使用的是pycharm,但是引入的python.exe在python目录下。只需要在pyuic配置中添加上面的内容,将python.exe地址改为pycharm文件中的地址即可。
(实在找不到的话,直接在文件夹里搜索)
创建UI
首先在QTdesigner中打开pycharm
可以在Q中创建界面,然后在这里使用。 ? 右键点击刚刚生成的.ui程序,然后点击pyuic
修改.py程序
刚刚生成的.py程序无法生成窗口程序。我们仍然需要改变。以下是一些简单的更改。
原来生成的代码是:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 220, 72, 15))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
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", "JHKJGJKGK"))
复制代码
改变后的程序是:
import untitled # 需要运行的.py文件名
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 220, 72, 15))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
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", "JHKJGJKGK"))
if __name__ == '__main__':
app = QApplication(sys.argv) # 创建应用程序
mainwindow = QMainWindow() # 创建主窗口
ui = untitled.Ui_MainWindow() # 调用中的主窗口
ui.setupUi(mainwindow) # 向主窗口添加控件
mainwindow.show() # 显示窗口
sys.exit(app.exec_()) # 程序执行循环
复制代码
重点是添加主窗体供他使用并在最后显示,而且只有一个目录。
效果
作者:巴石杨
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。