One : install PyQt5
pip install pyqt5
Two :PyQt5 Easy to use 1: Use PyQt5 Create a simple window
import sys
from PyQt5 import QtWidgets
# Create an application (Application) object ,sys.argv Parameter is a list of parameters from the command line ,
# Python Scripts can be shell Run in . This is a way we use to control the startup of our application .
app = QtWidgets.QApplication(sys.argv)
# Create a widget Component base class
windows = QtWidgets.QWidget()
# Set up widget The size of the component (w,h)
windows.resize(500,500)
# Set up widget Location of components (x,y)
windows.move(100,100)
"""
# Set up widget The position of the component is centered
qr = windows.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
windows.move(qr.topLeft())
"""
# Equate to w.resize(500,500) and w.move(100,100) Combination of two sentences ,(x,y,w,h)
#windows.setGeometry(100,100,500,500)
#show() Method displays... On the screen widget Components
windows.show()
# Loop execution window trigger event , Exit without leaving garbage after completion , If you do not add it, you will create a new one widget Components will flash by
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
# Create an application (Application) object ,sys.argv Parameter is a list of parameters from the command line ,
# Python Scripts can be shell Run in . This is a way we use to control the startup of our application .
app = QtWidgets.QApplication(sys.argv)
# Create a widget Component base class
windows = QtWidgets.QWidget()
# Set up widget The size of the component (w,h)
windows.resize(500,500)
# Set up widget Location of components (x,y)
windows.move(100,100)
"""
# Set up widget The position of the component is centered
qr = windows.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
windows.move(qr.topLeft())
"""
# Equate to w.resize(500,500) and w.move(100,100) Combination of two sentences ,(x,y,w,h)
#windows.setGeometry(100,100,500,500)
# to widget Component set title
windows.setWindowTitle(' title ')
# to widget Component settings Icon
windows.setWindowIcon(QtGui.QIcon('2.png'))
#show() Method displays... On the screen widget Components
windows.show()
# Loop execution window trigger event , Exit without leaving garbage after completion , If you do not add it, you will create a new one widget Components will flash by
sys.exit(app.exec_())
The phenomenon is as follows :
3: Set the button and prompt for the created window
import sys
from PyQt5 import QtWidgets,QtGui
# Create an application (Application) object ,sys.argv Parameter is a list of parameters from the command line ,
# Python Scripts can be shell Run in . This is a way we use to control the startup of our application .
app = QtWidgets.QApplication(sys.argv)
# Create a widget Component base class
windows = QtWidgets.QWidget()
# Set up widget The size of the component (w,h)
windows.resize(500,500)
# Set up widget Location of components (x,y)
windows.move(100,100)
"""
# Set up widget The position of the component is centered
qr = windows.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
windows.move(qr.topLeft())
"""
# Equate to w.resize(500,500) and w.move(100,100) Combination of two sentences ,(x,y,w,h)
#windows.setGeometry(100,100,500,500)
# to widget Component set title
windows.setWindowTitle(' title ')
# to widget Component settings Icon
windows.setWindowIcon(QtGui.QIcon('2.png'))
# Set the font and size of the prompt
QtWidgets.QToolTip.setFont(QtGui.QFont('SansSerif', 10))
# to widget Component setting prompt
windows.setToolTip(' This is the window prompt ')
# Set the button and give it a name
btn = QtWidgets.QPushButton('button',windows)
# Set the position of the button (x,y,w,h)
btn.setGeometry(200,200,100,50)
# Set the prompt for the button
btn.setToolTip(' This is the button prompt ')
# Set button style
btn.setStyleSheet("background-color: rgb(164, 185, 255);"
"border-color: rgb(170, 150, 163);"
"font: 75 12pt \"Arial Narrow\";"
"color: rgb(126, 255, 46);")
# Click the button to close the created window
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
#show() Method displays... On the screen widget Components
windows.show()
# Loop execution window trigger event , Exit without leaving garbage after completion , If you do not add it, you will create a new one widget Components will flash by
sys.exit(app.exec_())
The phenomenon is as follows ( Click on button Button , The window closed ):
4: Set the label (lable) Information
import sys
from PyQt5 import QtWidgets,QtGui,QtCore
# Create an application (Application) object ,sys.argv Parameter is a list of parameters from the command line ,
# Python Scripts can be shell Run in . This is a way we use to control the startup of our application .
app = QtWidgets.QApplication(sys.argv)
# Create a widget Component base class
windows = QtWidgets.QWidget()
# Set up widget The size of the component (w,h)
windows.resize(500,500)
# Set up widget Location of components (x,y)
windows.move(100,100)
"""
# Set up widget The position of the component is centered
qr = windows.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
windows.move(qr.topLeft())
"""
# Equate to w.resize(500,500) and w.move(100,100) Combination of two sentences ,(x,y,w,h)
#windows.setGeometry(100,100,500,500)
# to widget Component set title
windows.setWindowTitle(' title ')
# to widget Component settings Icon
windows.setWindowIcon(QtGui.QIcon('2.png'))
# Set up lable Information
label = QtWidgets.QLabel(windows)
label.setGeometry(QtCore.QRect(100, 10, 100, 60))
label.setText(' This is a lable Information ')
label.setObjectName('label')
#show() Method displays... On the screen widget Components
windows.show()
# Loop execution window trigger event , Exit without leaving garbage after completion , If you do not add it, you will create a new one widget Components will flash by
sys.exit(app.exec_())
The phenomenon is as follows :
5: Configure the input box
import sys
from PyQt5 import QtWidgets,QtGui,QtCore,Qt
# Create an application (Application) object ,sys.argv Parameter is a list of parameters from the command line ,
# Python Scripts can be shell Run in . This is a way we use to control the startup of our application .
app = QtWidgets.QApplication(sys.argv)
# Create a widget Component base class
windows = QtWidgets.QWidget()
# Set up widget The size of the component (w,h)
windows.resize(500,500)
# Set up widget Location of components (x,y)
windows.move(100,100)
"""
# Set up widget The position of the component is centered
qr = windows.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
windows.move(qr.topLeft())
"""
# Equate to w.resize(500,500) and w.move(100,100) Combination of two sentences ,(x,y,w,h)
#windows.setGeometry(100,100,500,500)
# to widget Component set title
windows.setWindowTitle(' title ')
# to widget Component settings Icon
windows.setWindowIcon(QtGui.QIcon('2.png'))
# Set input box
textbox = Qt.QLineEdit(windows)
textbox.resize(100,20)
textbox.move(50,50)
#show() Method displays... On the screen widget Components
windows.show()
# Loop execution window trigger event , Exit without leaving garbage after completion , If you do not add it, you will create a new one widget Components will flash by
sys.exit(app.exec_())
The phenomenon is as follows :
3、 ... and : Summarize the above methods and realize a simple function , as follows : The function is : After entering the value in the input box , Clicking the button will print out the value you entered , Closing the window will prompt
import sys
from PyQt5 import QtWidgets,QtGui,QtCore,Qt
class GUI(QtWidgets.QWidget):
def __init__(self):
# initialization ————init__
super().__init__()
self.initGUI()
def initGUI(self):
# Set window size
self.resize(500,500)
# Set the window position ( The following configuration is located in the middle of the screen )
qr = self.frameGeometry()
cp = QtWidgets.QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
# Set window title and icon
self.setWindowTitle(' Window title ')
self.setWindowIcon(QtGui.QIcon('2.png'))
# Set the window prompt
self.setToolTip(' Window tip ')
# Set up label Information
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(100, 10, 100, 60))
self.label.setText(' This is a lable Information ')
self.label.setObjectName('label')
# Set up label Tips
self.label.setToolTip('label Tips ')
# Set input box
self.textbox = Qt.QLineEdit(self)
self.textbox.resize(100, 20)
self.textbox.move(100, 50)
# Set the input box prompt
self.textbox.setToolTip(' Input box prompt ')
# Set button
self.btn =QtWidgets.QPushButton(' Button ',self)
self.btn.resize(100,20)
self.btn.move(200,50)
# Set button style
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);")
# Set button prompt
self.btn.setToolTip(' Button prompt ')
# Click the mouse to trigger the event
self.btn.clicked.connect(self.clickbtn)
# Display window
self.show();
# Click the mouse to trigger the function
def clickbtn(self):
# Print out the information in the input box
textboxValue = self.textbox.text()
QtWidgets.QMessageBox.question(self, " Information ", ' The input box you entered is :' + textboxValue,QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok)
# Clear the input box information
self.textbox.setText('')
# Close window event override
def closeEvent(self, QCloseEvent):
reply = QtWidgets.QMessageBox.question(self, ' Warning '," OK to close the current window ?", 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 phenomenon is :
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/101091.html Link to the original text :https://javaforall.cn