python3安装
下载 anaconda3
由于已经安装了python2,需要将python3配置一下


和平相处了

注意:python3使用pip
python3 -m pip
以python3为主的话,同样方法配置python2就行
anaconda pyqt5自带


保存生成一个untitled.ui文件,用vscode打开
vscode
安装PYQT插件


点击后自动生成Ui_untitled.py文件

vscode配置python3



创建py文件,运行
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDockWidget, QListWidget
from PyQt5.QtGui import *
from Ui_untitled import Ui_MainWindow #导入创建的GUI类
#自己建一个mywindows类,mywindow是自己的类名。QtWidgets.QMainWindow:继承该类方法
class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
#__init__:析构函数,也就是类被创建后就会预先加载的项目。
# 马上运行,这个方法可以用来对你的对象做一些你希望的初始化。
def __init__(self):
#这里需要重载一下mywindow,同时也包含了QtWidgets.QMainWindow的预加载项。
super(mywindow, self).__init__()
self.setupUi(self)
if __name__ == '__main__': #如果整个程序是主程序
# QApplication相当于main函数,也就是整个程序(很多文件)的主入口函数。
# 对于GUI程序必须至少有一个这样的实例来让程序运行。
app = QtWidgets.QApplication(sys.argv)
#生成 mywindow 类的实例。
window = mywindow()
#有了实例,就得让它显示,show()是QWidget的方法,用于显示窗口。
window.show()
# 调用sys库的exit退出方法,条件是app.exec_(),也就是整个窗口关闭。
# 有时候退出程序后,sys.exit(app.exec_())会报错,改用app.exec_()就没事
# https://stackoverflow.com/questions/25719524/difference-between-sys-exitapp-exec-and-app-exec
sys.exit(app.exec_())
F5选择python file

或者文件右键,选择 run python file in terminal

本文详细介绍了如何在已有Python2环境下安装Python3与Anaconda,确保两者并存且不冲突。并通过PyQt5创建GUI应用程序,涵盖了从环境配置到VSCode开发工具的使用,以及Python3的运行流程。
2487

被折叠的 条评论
为什么被折叠?



