PyQt5实现帮你向小姐姐表白

利用PyQt进行GUI编程,实现表白神器件

1. 先看效果

在这里插入图片描述

2. 代码实现
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date    : 2020-01-11 23:00:44
# @Author  : King-Key 
# @Link    : kingkey.club
# @E-mail  : guo_wang_113@163.com

import sys
import random
from PyQt5 import QtCore,QtMultimedia
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, QDesktopWidget, QMessageBox
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtGui import *
import cv2


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()  # 界面绘制函数

    def initUI(self):
        self.truetext="我愿意"
        self.falsetext="拒绝"
        
        #读取图片,作为背景图片
        self.image=cv2.imread("./love.jpg")
        print(self.image.shape)
        
        # 设置窗口位置和大小
        self.setGeometry(300, 300, self.image.shape[1], self.image.shape[0])
        self.setWindowFlags(QtCore.Qt.WindowTitleHint|QtCore.Qt.WindowCloseButtonHint)
        self.center()
        # 设置标题
        self.setWindowTitle("我喜欢你")
        # 设置窗口图标
        self.setWindowIcon(
            QIcon("./love.jpg"))
        #设置背景图片
        background=QPalette()
        background.setBrush(self.backgroundRole(),QBrush(QPixmap("./love.jpg")))
        self.setPalette(background)

        # 创建按钮
        # “我愿意“”按钮
        trueButton = QPushButton(self.truetext, self)
        # “我愿意”按钮的时间绑定
        trueButton.clicked.connect(self.trueButtonEvent)
		# “拒绝”按钮
        self.falseButton=QPushButton(self.falsetext,self)
        # 获取鼠标焦点
        self.falseButton.installEventFilter(self)
        # 按钮的位置根据窗口的大小进行自动调整
        trueButton.move(self.image.shape[1]/3, (self.image.shape[0]/5)*4)
        self.falseButton.move(self.image.shape[1]/2,(self.image.shape[0]/5)*4)

        # 显示
        self.show()
	# “我愿意”按钮的事件,关闭窗口
    def trueButtonEvent(self):
        sender=self.sender()
        event=QApplication.instance()
        event.quit()


    # “拒绝”按钮移动事件
    def eventFilter(self,object,event):
    	if event.type()==QtCore.QEvent.HoverMove:
    		self.falseButton.move(random.randint(10,int(self.image.shape[1]/5)*4),random.randint(10,int(self.image.shape[0]/5)*4))
    		return True
    	return False

    #点击窗口关闭事件
    def closeEvent(self, event):
        reply = QMessageBox.question(
            self, "小姐姐", "不能逃避的哦", QMessageBox.Yes, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
        	event.ignore()

    # 窗口显示在屏幕中间
    def center(self):
        # 获取窗口
        qr = self.frameGeometry()
        # 获取屏幕中心点
        cp = QDesktopWidget().availableGeometry().center()
        # 显示在屏幕中间
        qr.moveCenter(cp)
        self.move(qr.topLeft())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Example()
    sys.exit(app.exec_())
3. 源码链接:

表白神器,GitHub地址

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值