Detailed tutorial for using PyQt5 in python (code example)
This article brings you a detailed tutorial (code example) on using PyQt5 in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
One: Install PyQt5
pip install pyqt5
Two: Simple use of PyQt5
1: Use PyQt5 to create a simple window
import sys from PyQt5 import QtWidgets #创建一个应用(Application)对象,sys.argv参数是一个来自命令行的参数列表, # Python脚本可以在shell中运行。这是我们用来控制我们应用启动的一种方法。 app = QtWidgets.QApplication(sys.argv) #创建一个widget组件基础类 windows = QtWidgets.QWidget() #设置widget组件的大小(w,h) windows.resize(500,500) #设置widget组件的位置(x,y) windows.move(100,100) """ #设置widget组件的位置居中 qr = windows.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) windows.move(qr.topLeft()) """ #等同于 w.resize(500,500)和w.move(100,100)两句结合,(x,y,w,h) #windows.setGeometry(100,100,500,500) #show()方法在屏幕上显示出widget组件 windows.show() #循环执行窗口触发事件,结束后不留垃圾的退出,不添加的话新建的widget组件就会一闪而过 sys.exit(app.exec_())
The phenomenon is as follows:
2: Add a title and icon to the created window
import sys from PyQt5 import QtWidgets,QtGui #创建一个应用(Application)对象,sys.argv参数是一个来自命令行的参数列表, # Python脚本可以在shell中运行。这是我们用来控制我们应用启动的一种方法。 app = QtWidgets.QApplication(sys.argv) #创建一个widget组件基础类 windows = QtWidgets.QWidget() #设置widget组件的大小(w,h) windows.resize(500,500) #设置widget组件的位置(x,y) windows.move(100,100) """ #设置widget组件的位置居中 qr = windows.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) windows.move(qr.topLeft()) """ #等同于 w.resize(500,500)和w.move(100,100)两句结合,(x,y,w,h) #windows.setGeometry(100,100,500,500) #给widget组件设置标题 windows.setWindowTitle('标题') #给widget组件设置图标 windows.setWindowIcon(QtGui.QIcon('2.png')) #show()方法在屏幕上显示出widget组件 windows.show() #循环执行窗口触发事件,结束后不留垃圾的退出,不添加的话新建的widget组件就会一闪而过 sys.exit(app.exec_())
The phenomenon is as follows:
3: Add a title and icon to the created window The window setting button and setting prompt
import sys from PyQt5 import QtWidgets,QtGui #创建一个应用(Application)对象,sys.argv参数是一个来自命令行的参数列表, # Python脚本可以在shell中运行。这是我们用来控制我们应用启动的一种方法。 app = QtWidgets.QApplication(sys.argv) #创建一个widget组件基础类 windows = QtWidgets.QWidget() #设置widget组件的大小(w,h) windows.resize(500,500) #设置widget组件的位置(x,y) windows.move(100,100) """ #设置widget组件的位置居中 qr = windows.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) windows.move(qr.topLeft()) """ #等同于 w.resize(500,500)和w.move(100,100)两句结合,(x,y,w,h) #windows.setGeometry(100,100,500,500) #给widget组件设置标题 windows.setWindowTitle('标题') #给widget组件设置图标 windows.setWindowIcon(QtGui.QIcon('2.png')) #设置提示语的字体和大小 QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10)) #给widget组件设置提示语 windows.setToolTip('这是窗口提示') #设置按钮并给按钮命名 btn = QtWidgets.QPushButton('button',windows) #给按钮设置位置(x,y,w,h) btn.setGeometry(200,200,100,50) #给按钮设置提示语 btn.setToolTip('这是按钮提示') #设置按钮样式 btn.setStyleSheet("background-color: rgb(164, 185, 255);" "border-color: rgb(170, 150, 163);" "font: 75 12pt \"Arial Narrow\";" "color: rgb(126, 255, 46);") #点击按钮关闭创建的窗口 btn.clicked.connect(QtCore.QCoreApplication.instance().quit) #show()方法在屏幕上显示出widget组件 windows.show() #循环执行窗口触发事件,结束后不留垃圾的退出,不添加的话新建的widget组件就会一闪而过 sys.exit(app.exec_())
The phenomenon is as follows (click the button button, the window closes):
4: Set label information
import sys from PyQt5 import QtWidgets,QtGui,QtCore #创建一个应用(Application)对象,sys.argv参数是一个来自命令行的参数列表, # Python脚本可以在shell中运行。这是我们用来控制我们应用启动的一种方法。 app = QtWidgets.QApplication(sys.argv) #创建一个widget组件基础类 windows = QtWidgets.QWidget() #设置widget组件的大小(w,h) windows.resize(500,500) #设置widget组件的位置(x,y) windows.move(100,100) """ #设置widget组件的位置居中 qr = windows.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) windows.move(qr.topLeft()) """ #等同于 w.resize(500,500)和w.move(100,100)两句结合,(x,y,w,h) #windows.setGeometry(100,100,500,500) #给widget组件设置标题 windows.setWindowTitle('标题') #给widget组件设置图标 windows.setWindowIcon(QtGui.QIcon('2.png')) #设置lable信息 label = QtWidgets.QLabel(windows) label.setGeometry(QtCore.QRect(100, 10, 100, 60)) label.setText('这是lable信息') label.setObjectName('label') #show()方法在屏幕上显示出widget组件 windows.show() #循环执行窗口触发事件,结束后不留垃圾的退出,不添加的话新建的widget组件就会一闪而过 sys.exit(app.exec_())
The phenomenon is as follows:
import sys from PyQt5 import QtWidgets,QtGui,QtCore,Qt #创建一个应用(Application)对象,sys.argv参数是一个来自命令行的参数列表, # Python脚本可以在shell中运行。这是我们用来控制我们应用启动的一种方法。 app = QtWidgets.QApplication(sys.argv) #创建一个widget组件基础类 windows = QtWidgets.QWidget() #设置widget组件的大小(w,h) windows.resize(500,500) #设置widget组件的位置(x,y) windows.move(100,100) """ #设置widget组件的位置居中 qr = windows.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) windows.move(qr.topLeft()) """ #等同于 w.resize(500,500)和w.move(100,100)两句结合,(x,y,w,h) #windows.setGeometry(100,100,500,500) #给widget组件设置标题 windows.setWindowTitle('标题') #给widget组件设置图标 windows.setWindowIcon(QtGui.QIcon('2.png')) #设置输入框 textbox = Qt.QLineEdit(windows) textbox.resize(100,20) textbox.move(50,50) #show()方法在屏幕上显示出widget组件 windows.show() #循环执行窗口触发事件,结束后不留垃圾的退出,不添加的话新建的widget组件就会一闪而过 sys.exit(app.exec_())
The function is: after entering the value in the input box, click the button to print out the value you entered, and there will be a prompt when closing the window
import sys from PyQt5 import QtWidgets,QtGui,QtCore,Qt class GUI(QtWidgets.QWidget): def __init__(self): #初始化————init__ super().__init__() self.initGUI() def initGUI(self): #设置窗口大小 self.resize(500,500) #设置窗口位置(下面配置的是居于屏幕中间) qr = self.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) #设置窗口标题和图标 self.setWindowTitle('窗口标题') self.setWindowIcon(QtGui.QIcon('2.png')) #设置窗口提示 self.setToolTip('窗口提示') #设置label信息 self.label = QtWidgets.QLabel(self) self.label.setGeometry(QtCore.QRect(100, 10, 100, 60)) self.label.setText('这是lable信息') self.label.setObjectName('label') # 设置label提示 self.label.setToolTip('label提示') #设置输入框 self.textbox = Qt.QLineEdit(self) self.textbox.resize(100, 20) self.textbox.move(100, 50) # 设置输入框提示 self.textbox.setToolTip('输入框提示') #设置按钮 self.btn =QtWidgets.QPushButton('按钮',self) self.btn.resize(100,20) self.btn.move(200,50) # 设置按钮样式 self.btn.setStyleSheet("background-color: rgb(164, 185, 255);" "border-color: rgb(170, 150, 163);" "font: 75 12pt \"Arial Narrow\";" "color: rgb(126, 255, 46);") # 设置按钮提示 self.btn.setToolTip('按钮提示') #点击鼠标触发事件 self.btn.clicked.connect(self.clickbtn) #展示窗口 self.show(); #点击鼠标触发函数 def clickbtn(self): #打印出输入框的信息 textboxValue = self.textbox.text() QtWidgets.QMessageBox.question(self, "信息", '你输入的输入框内容为:' + textboxValue,QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok) #清空输入框信息 self.textbox.setText('') #关闭窗口事件重写 def closeEvent(self, QCloseEvent): reply = QtWidgets.QMessageBox.question(self, '警告',"确定关闭当前窗口?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: QCloseEvent.accept() else: QCloseEvent.ignore() if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) gui = GUI() sys.exit(app.exec_())
The above is the detailed content of Detailed tutorial for using PyQt5 in python (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
