• GUI编程--PyQt5--QPushButton


    创建按钮

    btn1 = QPushButton()
    btn1.setParent(win)
    btn1.setText("btn1")
    
    btn2 = QPushButton(win)
    btn2.setText("btn2")
    
    btn3 = QPushButton("btn3", win)
    
    btn4 = QPushButton(QIcon, "btn4", win)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    菜单功能

    在这里插入图片描述

    # 菜单对象
     menu_obj1 = QMenu() # 菜单控件  独立
     # 菜单添加 动作
     action1 = QAction(menu_obj1) # 动作控件  放入菜单
     action1.setText("新建")
     action1.setIcon(QIcon("./imgs/dog.jpg"))
     # action1.setFont(QFont("字体", 20, 10, True))
     action1.triggered.connect(lambda : print("开始新建..."))
    
     # 菜单对象 添加 动作
     menu_obj1.addAction(action1) # 动作
    
     # 添加子菜单
     sub_menu = QMenu(menu_obj1)
     sub_menu.setTitle("最近打开")
     sub_action = QAction("文件1", sub_menu)
     sub_menu.addAction(sub_action)
     menu_obj1.addMenu(sub_menu)
    
     # 添加分隔符
     menu_obj1.addSeparator()
    
     # 退出
     exit_action = QAction("退出", menu_obj1)
     menu_obj1.addAction(exit_action)
    
     # 按钮添加菜单
     btn3.setMenu(menu_obj1)
    
     btn3.showMenu()  #  展示菜单
     btn3.menu()   # 查看菜单
    	
     btn3.setFlat(True)  # 扁平化  不在展示背景色 不在凸起
     btn3.isFlat()
     btn3.setDefault(True)
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    右键菜单

    在这里插入图片描述
    默认的右键菜单

    # __author__ = "laufing"
    # class_based_qt
    # laufing_qt
    
    # __author__ = "laufing"
    
    from PyQt5.QtGui import QIcon, QPixmap, QCursor, QKeyEvent
    from PyQt5 import QtGui
    from PyQt5.QtCore import Qt
    from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QDesktopWidget, QPushButton, QMenu, QMenuBar, QAction
    import sys
    
    
    class Window(QWidget):
        def __init__(self):
            super(Window, self).__init__()
            # 窗口标题
            self.setWindowTitle("laufing[*]")
            # 窗口图标
            pixmap = QPixmap("./imgs/dog.jpg").scaled(50, 50)
            icon = QIcon(pixmap)
            # icon.addPixmap(pixmap, QIcon.Normal, QIcon.Off)
            self.setWindowIcon(icon)
    
            # 设置宽高
            self.resize(500, 600)
    
            # 窗口居中
            desktop_geo = QDesktopWidget().geometry()
            width, height = desktop_geo.width(), desktop_geo.height()
            self.move(width / 2 - self.width() / 2, height / 2 - self.height() / 2)
    
    
            # 设置文本
            self.label = QLabel(self)
            self.label.setText("测试按钮被按下")
            self.label.resize(200, 200)
            self.label.move(100, 100)
    	
        def contextMenuEvent(self, event: QtGui.QContextMenuEvent):
            # 右键 的事件
            # 创建菜单
            menu = QMenu()
            action = QAction("打开", menu)
            action2 = QAction("编辑", menu)
            action3 = QAction("退出", menu)
    
            menu.addAction(action)
            menu.addAction(action2)
            menu.addSeparator()
            menu.addAction(action3)
    
            # 展示菜单
            menu.exec_(event.globalPos())
    
    
    
    if __name__ == '__main__':
        import time
        # 窗口应用程序
        app = QApplication(sys.argv)
    
        window = Window()
        window.show()
    
        status_code = app.exec_()
        sys.exit(status_code)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    另外,还可以设置右键菜单的策略:

    window = Window()
    # 设置右键的菜单策略
    # window.setContextMenuPolicy(Qt.ContextMenuPolicy.DefaultContextMenu) 默认的右键 触发contextMenuEvent
    window.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
    window.customContextMenuRequested.connect(lambda : print("用户自定义的右键菜单"))
    
    window.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

     
     

    子类QCommandLineButton

    btn = QCommandLineButton("text", 'description', parent)
    
    btn.setText("xxxx")
    btn.setDescription("xxx")
    btn.setIcon(QIcon(xx))
    
    btn.text()
    btn.description()
    btn.icon()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    相对于窗口的局部坐标点转为相对于屏幕的全局坐标点

    window.mapToGlobal(point)
    window.mapToParent(point)
    
    • 1
    • 2
  • 相关阅读:
    玩一下Spring Boot
    Linux【3】系统管理
    微信小程序--自定义组件(超详细 从新建到使用)
    【HMS Core】【FAQ】【AR Engine】AR Engine常见问题合集
    【JVM调优实战100例】03——JVM堆调优四例
    代码随想录算法训练营第五十八天| 583. 两个字符串的删除操作 72. 编辑距离
    算法分享三个方面学习方法(做题经验,代码编写经验,比赛经验)
    二次创业,都市丽人照旧
    基于原子变量的内存模型优化
    Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例
  • 原文地址:https://blog.csdn.net/weixin_45228198/article/details/127923367