当前位置: 首页 > 工具软件 > PushButton > 使用案例 >

pyQt5-pushButton常用方法

皮安顺
2023-12-01
# -*- coding:utf-8 -*-
"""
@Time:2022/3/23 21:04
@Author:小柠檬
@File:push_button.py
"""
from PyQt5 import uic, QtCore
from PyQt5.QtWidgets import QApplication


class PushButton:

    def __init__(self):
        # 加载UI界面
        self.ui = uic.loadUi("push_button.ui")

        # 1.单击按钮
        self.ui.pushButton.clicked.connect(self.slot_clicked)

        # 2.设置按钮文本
        self.ui.pushButton.setText('新设置的文本')

        # 3.禁用按钮
        self.ui.pushButton.setEnabled(False)

        # 4.启用按钮
        self.ui.pushButton.setEnabled(True)

        # 5.查看按钮状态
        push_button_status = self.ui.pushButton.isEnabled()
        print(push_button_status)

        # 6.设置按钮快捷键
        self.ui.pushButton.setShortcut('enter')  # 按下回车键相当于点击操作

    @staticmethod
    def slot_clicked():
        print('这是单击信号槽')


def main():
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)  # 和designer设计的窗口比例一致
    app = QApplication([])
    push_button = PushButton()
    push_button.ui.show()
    app.exec_()


if __name__ == '__main__':
    main()

 类似资料: