4.20

4.20

控制事件绑定

事件绑定三要素: xxx发生xx就做xx
事件源 发生 事件 就 执行某个操作(事件驱动程序)
绑定程序:事件源.事件.connect(操作对应的函数)

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QPushButton, QLineEdit, QSpinBox


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.create_ui()
        self.setGeometry(200, 100, 800, 600)
        self.setWindowTitle('图书管理系统首页')
        self.show()

    def create_ui(self):
        btn1 = QPushButton('确定', self)
        btn1.move(50, 10)
        # 按钮点击事件
        btn1.clicked.connect(self.btn_action)

        self.input1 = QLineEdit(self)
        self.input1.setPlaceholderText('请输入手机号')
        self.input1.move(50, 60)
        # 输入框内容改变事件
        self.input1.textChanged.connect(self.input_text_action)


        num_input = QSpinBox(self)
        num_input.move(100, 100)
        num_input.valueChanged.connect(self.num_action)

    def num_action(self, value):
        print('数值改变', value)

    def btn_action(self, value):
        print('按钮被点击', value, self.input1.text())


    def input_text_action(self, value):
        print('输入框内容改变', value)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

图片的使用

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton
from PyQt5.QtGui import QImage, QIcon, QPixmap, QPalette
from PyQt5.QtCore import QSize
from PyQt5.QtCore import Qt
import requests

# Qt.AspectRatioMode


def download_image():
    response = requests.get('https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=158268377,3003399912&fm=26&gp=0.jpg')
    if response.status_code == 200:
        return response.content
    print('请求失败!', response)
    return None


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.create_ui()
        self.setGeometry(200, 100, 800, 450)
        self.setWindowTitle('图书管理系统首页')
        self.show()

    def create_ui(self):
        # 1. 设置窗口图标
        icon = QIcon('files/logo.png')
        self.setWindowIcon(icon)

        # 2. 显示图片
        self.bg_label = QLabel(self)
        self.bg_label.setGeometry(0, 0, 800, 450)
        self.bgpix = QPixmap('files/bg.jpg')
        self.bg_label.setPixmap(self.bgpix)

        username = QLabel('用户名:', self)
        username.move(100, 100)
        username.setStyleSheet('QLabel{color: rgb(255, 255, 255); font-size:30px;}')

        # 3.显示网络图片
        # 下载图片
        image_data = download_image()
        # 使用网络图片数据创建图片对象
        image = QImage.fromData(image_data)
        # 对图片进行缩放
        image = image.scaled(200, 200)
        pix = QPixmap.fromImage(image)
        # 显示图片
        btn = QLabel(self)
        btn.setGeometry(200, 100, 200, 200)
        btn.setPixmap(pix)

        # 思考:怎么将图片放在按钮上?






    # 在窗口的大小发送改变的时候会被自动调用
    def resizeEvent(self, a0) -> None:
        # 获取到最新的窗口大小
        w_size = self.size()

        # 对原图进行缩放
        image = QImage('files/bg.jpg')
        new_image = image.scaled(w_size.width(), w_size.height())
        pix = QPixmap.fromImage(new_image)

        # 将显示图片的label进行缩放
        self.bg_label.setGeometry(0, 0, w_size.width(), w_size.height())
        self.bg_label.setPixmap(pix)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

菜单

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QAction, QMenu
from PyQt5.QtGui import QIcon

# 如果想要在窗口上添加菜单,那么窗口的类型必须是QMainWindow或QMainWindow的子类


# 第一步,让窗口类继承QMainWindow
class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.create_ui()
        self.setGeometry(200, 100, 800, 600)
        self.setWindowTitle('图书管理系统首页')
        self.show()

    def create_ui(self):
        # 第二步:添加各种菜单
        # 1. 获取当前窗口上菜单栏
        menu_bar = self.menuBar()
        menu_bar.setNativeMenuBar(False)     # mac需要添加,否则菜单栏不显示

        # 2.在菜单栏上添加菜单
        file_menu = menu_bar.addMenu('File')
        edit_menu = menu_bar.addMenu('&Edit')

        # 3.在菜单上添加选项
        fa1 = QAction('New Project', self)    # 创建选项
        fa1.triggered.connect(self.new_project)    # 给选项绑定事件

        fa2 = QAction(QIcon('files/naozhong.png'), 'Open', self)
        fa2.triggered.connect(self.open_action)
        fa2.setShortcut('ctrl+o')       # 添加快捷键

        # 将选项添加到菜单上
        file_menu.addActions([fa1, fa2])

        # 4. 添加子菜单
        file_p_menu = QMenu('File Projecties', self)
        file_menu.addMenu(file_p_menu)

        fpa1 = QAction('File Encoding', self)
        fpa2 = QAction('Remove BOM', self)
        fpa3 = QAction('Make File Read-Only', self)
        file_p_menu.addActions([fpa1, fpa2, fpa3])

    # 在窗口上点鼠标右键的时候会被自动调用
    def contextMenuEvent(self, event) -> None:
        # 创建菜单对象
        right_menu = QMenu(self)

        # 在右键菜单上添加选项或者子菜单
        a1 = QAction('copy', self)
        a2 = QAction('Paste', self)
        a3 = QAction('Cut', self)
        right_menu.addActions([a1, a2, a3])

        # 让菜单显示在当前窗口鼠标所在的位置
        right_menu.exec_(self.mapToGlobal(event.pos()))














    def new_project(self):
        print('创建新的工程!')

    def open_action(self):
        print('打开工程!')









if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

对话框

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel,QPushButton, QMessageBox, QInputDialog, QColorDialog, QFontDialog, QFileDialog
from PyQt5.QtGui import QPalette, QColor

class Value:
    Yes = 16384
    No = 65536


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.create_ui()
        self.setGeometry(200, 100, 800, 600)
        self.setWindowTitle('图书管理系统首页')
        self.show()

    def create_ui(self):
        message_btn = QPushButton('确定', self)
        message_btn.move(100, 20)
        message_btn.clicked.connect(self.show_message_box)

        self.color_label = QLabel('你好', self)
        pa = QPalette()
        pa.setColor(self.backgroundRole(), QColor(255, 0, 0))
        self.setPalette(pa)
        self.autoFillBackground()

    def show_message_box(self):
        print('显示消息框!')
        # 1. QMessageBox
        # 消息类型:information、question、warning、critical、about
        # QMessageBox.消息类型()

        # QMessageBox.information(self, '对话框', '中午好!', QMessageBox.Ok)

        # result = QMessageBox.question(self, '问题', '是否删除改数据?', QMessageBox.Yes|QMessageBox.No)
        # if result == Value.Yes:
        #     print('YES')
        # else:
        #     print('NO')

        # 2. QInputDialog
        # result = QInputDialog.getText(self, '', '请输入名字:')
        # print(result)

        result = QColorDialog.getColor()
        print(result.rgb())

        pa = QPalette()
        pa.setColor(self.backgroundRole(), result)
        self.setPalette(pa)
        self.autoFillBackground()

        # QFontDialog.getFont()

        # QFileDialog.getOpenFileUrl()








if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值