提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
提示:以下是本篇文章正文内容,下面案例可供参考
PyQt5是 Python 的图形用户界面 (GUI) 模块,您可以使用 Python 制作桌面应用程序。
win+R打开运行,键入cmd回车进入cmd命令窗口
在cmd命令窗口打开中打开python:
C:\Users\Administrator>python
导入pip:
import pip
用main函数打开pip
pip.main()
下载PyQt5
pip download PyQt5
安装PyQt5
pip install PyQt5
如果pip需要更新(如果不需要,忽略):
C:\Users\Administrator>python -m pip install --upgrade pip
和其他GUI一样,首先要导入模块
import sys
from PyQt5.QtWidgets import QApplication, QPushButton,QWidget
QtWidgets,QtGui,QtCore是PyQt5的三大组件,而QWidget是QtWidgets的子类,一般创建窗口用QtWidgets的子类QWidget,QMainWindow。
然后创建一个类继承自QWidget
class window(QWidget):
并且必须继承父类的__init__()方法,不然会报错
super().__init__()
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication, QPushButton,QWidget
app = QApplication(sys.argv)
class window(QWidget):
def __init__(self):
super().__init__()
self.lb=QPushButton("hello,world",self)
self.show()
if __name__=='__main__':
w=window()
app.exec()
QLabel是QLabel用于显示文本的小部件
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication, QLabel,QWidget
app = QApplication(sys.argv)
class window(QWidget):
def __init__(self):
super().__init__()
self.lb=QLabel("hello,world",self)
self.show()
if __name__=='__main__':
w=window()
app.exec()
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QCheckBox
app = QApplication(sys.argv)
class window(QMainWindow):
mylist="java","C++","python"
def __init__(self):
super().__init__()
self.width = 640
self.height = 400
self.setGeometry(10,10, self.width, self.height)
self.sp=QCheckBox("java",self)
self.sp2=QCheckBox("pyton",self)
self.sp.move(100,100)
self.sp2.move(100,200)
self.show()
if __name__=='__main__':
w=window()
app.exec()
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QComboBox
app = QApplication(sys.argv)
class window(QMainWindow):
mylist="java","C++","python"
def __init__(self):
super().__init__()
self.width = 640
self.height = 400
self.setGeometry(10,10, self.width, self.height)
self.combobox=QComboBox(self)
self.combobox.addItems(self.mylist)
self.show()
if __name__=='__main__':
w=window()
app.exec()
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QRadioButton
app = QApplication(sys.argv)
class window(QMainWindow):
def __init__(self):
super().__init__()
self.width = 640
self.height = 400
self.setGeometry(10,10, self.width, self.height)
self.rbtn=QRadioButton("java",self)
self.rbtn2=QRadioButton("C++",self)
self.rbtn3=QRadioButton("python",self)
self.rbtn.move(100,100)
self.rbtn2.move(100,200)
self.rbtn3.move(100,300)
self.show()
if __name__=='__main__':
w=window()
app.exec()
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
app = QApplication(sys.argv)
class window(QMainWindow):
def __init__(self):
super().__init__()
self.width = 640
self.height = 400
self.setGeometry(10,10, self.width, self.height)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
editMenu = mainMenu.addMenu('Edit')
viewMenu = mainMenu.addMenu('View')
searchMenu = mainMenu.addMenu('Search')
toolsMenu = mainMenu.addMenu('Tools')
helpMenu = mainMenu.addMenu('Help')
self.show()
if __name__=='__main__':
w=window()
app.exec()
完整代码如下(示例):
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow,QRadioButton,QSpinBox
app = QApplication(sys.argv)
class window(QMainWindow):
mylist="java","C++","python"
def __init__(self):
super().__init__()
self.width = 640
self.height = 400
self.setGeometry(10,10, self.width, self.height)
self.sp=QSpinBox(self)
self.show()
if __name__=='__main__':
w=window()
app.exec()