Let's first look at the effect:
Demand:
- Python3.6
- PyQT5 Library
Without much nonsense, go straight to the code:
-- coding: utf-8 --
import sys
from PyQt5 import QtWidgets
From pyqt5.QtGui import QFont, qicon × qtwidgets does not contain QFont must call QtGui
from PyQt5 import QtGui,QtCore
import random
class MessageBox(QtWidgets.QWidget): (inherited from the parent class QtWidgets.QWidget)
CloseAllowed=0
Definition init (self, parent = none): parent = none means that this QWidget belongs to the top-level window, that is, MainWindows
QtWidgets.QWidget.init(self) ා because of the inheritance, the parent class should be initialized
Initialize the parent class through super, the init() function has no self. If you directly use QtWidgets.QWidget.init(self), there is self in brackets
self.setGeometry(300, 300, 800,800) # The setGeometry() method performs two functions -- setting the position of the window on the screen and setting the size of the window itself. Its first two parameters are the x and y coordinates of the window on the screen. The last two parameters are the width and height of the window itself #self.resize(1000, 500) # Set the size of the form. This line is optional. self.center()#Customize a centered function self.setFixedSize(self.width(),self.height());#PyQT disable window size and Window maximize button #self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint)#PyQT disable Window maximize button and close button self.setWindowTitle(u'Confession artifact by Python Learning group: 125240963') # Only set the title of the form, not the location. self.setWindowIcon(QIcon('rose.png')) # When calling the QIcon constructor, we need to provide the path (relative or absolute) of the icon to display. Also note: This module must be imported from PyQt5.QtGui import QIcon when using the type of QIcon self.setToolTip(u'Python Learning group: 125240963 source code acquisition')#Call the setToolTip() method, which takes parameters in rich text format, such as css. QtWidgets.QToolTip.setFont(QFont('Chinese script', 10))#Set font and font size self.label1 = QtWidgets.QLabel(u'<b>Little sister, I have been observing you for a long time!</b>', self) # Create a label self.label1.move(150, 40) # Move this label to the position of this part (260, 40) self.label1.setFont(QFont("Timers", 20));#Set font and font size self.label2= QtWidgets.QLabel(u'<b>How about being my girlfriend?</b>', self) # Create a label self.label2.move(150, 100) # Move this label to the position of this part (260100) self.label2.setFont(QFont("Timers", 20));#Set font and font size #The palette QPalette class provided in Qt is designed to manage the appearance of controls. The QPalette class is equivalent to a dialog box or control's palette, and manages all colors of controls and forms. #Each form and control contains a QPalette object. When it is displayed, you can set it accordingly self.window_pale = QtGui.QPalette()#Instantiate QPalette class self.window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("biu.jpg")))#Open the picture. self.setPalette(self.window_pale)#Apply background color # setStyleSheet to set the appearance of the graphical interface self.buttonOK = QtWidgets.QPushButton(u'Agree!',self) # Because we need to add a button, we introduced the QPushButton class, which is an instance of the QPushButton class. The first argument to the constructor is the label of the button. The second parameter is the parent widget. The parent widget is a sample widget, which is inherited through QWidget self.buttonOK.setFocusPolicy(QtCore.Qt.NoFocus)#Button no focus # Qt::TabFocus 0x1 accept Tab focus # Qt::ClickFocus 0x2 accept mouse click as focus # Qt::StrongFocus TabFocus | ClickFocus | 0x8 accept Tab key and mouse click as focus # Qt::WheelFocus StrongFocus | 0x4 pulley as focus selected event # Qt::NoFocus 0 does not accept focus self.buttonOK.move(50, 700) # The move() method specifies the placement coordinates of the part. The vertex of the coordinates is the upper left corner of the window self.buttonOK.clicked.connect(self.showDialogOK) self.buttonE = QtWidgets.QPushButton(u'Consider',self) # Because we need to add a button, we introduced the QPushButton class, which is an instance of the QPushButton class. The first argument to the constructor is the label of the button. The second parameter is the parent widget. The parent widget is a sample widget, which is inherited through QWidget self.buttonE.setFocusPolicy(QtCore.Qt.NoFocus) # Button no focus # Qt::TabFocus 0x1 accept Tab focus # Qt::ClickFocus 0x2 accept mouse click as focus # Qt::StrongFocus TabFocus | ClickFocus | 0x8 accept Tab key and mouse click as focus # Qt::WheelFocus StrongFocus | 0x4 pulley as focus selected event # Qt::NoFocus 0 does not accept focus self.buttonE.move(330, 700) # The move() method specifies the placement coordinates of the part. The vertex of the coordinates is the upper left corner of the window self.buttonE.clicked.connect(self.showDialogEE) self.buttonNO = QtWidgets.QPushButton(u'refuse',self) # Because we need to add a button, we introduced the QPushButton class, which is an instance of the QPushButton class. The first argument to the constructor is the label of the button. The second parameter is the parent widget. The parent widget is a sample widget, which is inherited through QWidget self.buttonNO.setFocusPolicy(QtCore.Qt.NoFocus) # Button no focus # Qt::TabFocus 0x1 accept Tab focus # Qt::ClickFocus 0x2 accept mouse click as focus # Qt::StrongFocus TabFocus | ClickFocus | 0x8 accept Tab key and mouse click as focus # Qt::WheelFocus StrongFocus | 0x4 pulley as focus selected event # Qt::NoFocus 0 does not accept focus self.buttonNO.move(610, 700) # The move() method specifies the placement coordinates of the part. The vertex of the coordinates is the upper left corner of the window self.buttonNO.clicked.connect(self.showDialogNO) def showDialogOK(self): QtWidgets.QMessageBox.information(self, "O ye", "love you,So, so, so, so, so~~~", QtWidgets.QMessageBox.Ok) self.CloseAllowed = 1 def showDialogEE(self): QtWidgets.QMessageBox.information(self, "Don't get entangled.", "You're done. Your mother asked you to marry me", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "That's what your dad said", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "Your grandmother asked you to marry me", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "Your brother agreed. Your family agreed", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "Your best friend said it was right to marry me", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "If your father says he doesn't agree, he'll hit you", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "Accept the reality, I will treat you well", QtWidgets.QMessageBox.Ok) QtWidgets.QMessageBox.information(self, "Don't get entangled.", "You're all my people", QtWidgets.QMessageBox.Ok) def showDialogNO(self): self.q = random.randint(0, 650) # Generate random X coordinate in 0-650 self.w = random.randint(150, 650) # Generate random Y coordinate in 150-650 self.buttonNO.move(self.q, self.w) #The enterEvent event PyQt runs automatically without calling # def enterEvent(self,event):#Rewrites the mouse's enterEvent event event. Since it inherits the window class, this function will start as soon as the mouse enters the main window # self.q=random.randint(0,650)#Generate random X coordinate in 0-650 # self.w=random.randint(150,650)#Generate random Y coordinate in 150-650 # self.buttonNO.move(self.q,self.w)
When we close a window, a QCloseEvent event will be triggered in PyQt. Normally, this window will be closed directly,
But we don't want this to happen, so we need to redefine QCloseEvent. The function name is closeEvent, which is immutable
def closeEvent(self,event):#Function name is fixed and immutable if self.CloseAllowed==1: event.accept()#close window else: QtWidgets.QMessageBox.information(self, "No response", "Cute girl,Please don't run away!", QtWidgets.QMessageBox.Ok) event.ignore()#Ignore click X events def center(self): screen=QtWidgets.QDesktopWidget().screenGeometry()#Get screen resolution
QDesktopWidget() also has brackets in QtWidgets.QDesktopWidget().screenGeometry()
size=self.geometry()#Get window size self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)#Using move function window to center
app=QtWidgets.QApplication(sys.argv)
window=MessageBox()
window.show()
sys.exit(app.exec_())
If you want to send it to someone you like, you must pack it into exe
!/usr/bin/env python
-- coding: utf-8 --
"""
title = 'convert calculator project to exe file'
author = 'Sui yuejing Hao'
"""
from PyInstaller.main import run
if name == 'main':
opts = ['douyin.py', '-w', '--onefile'] #opts = ['douyin.py', '-F'] #opts = ['douyin.py', '-F', '-w'] #opts = ['douyin.py', '-F', '-w', '--icon=TargetOpinionMain.ico','--upx-dir','upx391w'] run(opts)