PyQt-1

PyQt Tutorial 1: Menu, Toolbar, and Button

PyQt4 is implemented as a set of Python modules. It has 440 classes and 6000 functions and methods. PyQt4’s classes are divided into several modules:

  • QtCore: The QtCore module contains the core non GUI functionality. This module is used for working with time, files and directories, various data types, streams, URLs, mime types, threads or processes.
  • QtGui: The QtGui module contains the graphical components and related classes. These include for example buttons, windows, status bars, toolbars, sliders, bitmaps, colours, and fonts.
  • QtNetwork: The QtNetwork module contains the classes for network programming. These classes facilitate the coding of TCP/IP and UDP clients and servers by making the network programming easier and more portable.
  • QtXml: The QtXml contains classes for working with XML files. This module provides implementation for both SAX and DOM APIs.
  • QtSvg: The QtSvg module provides classes for displaying the contents of SVG files. Scalable Vector Graphics (SVG) is a language for describing two-dimensional graphics and graphical applications in XML.
  • QtOpenGL: The QtOpenGL module is used for rendering 3D and 2D graphics using the OpenGL library. The module enables seamless integration of the Qt GUI library and the OpenGL library.
  • QtSql: The QtSql module provides classes for working with databases.

A Simple Example

        #!/usr/bin/python
        # -*- coding: utf-8 -*-

        import sys
        from PyQt4 import QtGui, QtCore
        #定义一个继承自QtGui.QMainWindow类的Example类
        class Example(QtGui.QMainWindow):
            def __init__(self):
                super(Example, self).__init__()
                #以下为四个自定义的函数:
                #设置窗口大小位置,窗口标题和图标
                self.initUI()
                #创建窗口中的Button
                self.defineButton()
                #创建窗口中的Menu
                self.defineMenu()
                #创建窗口中的Toolbar
                self.defineToolbar()

                self.show()

            def initUI(self):
                self.setGeometry(300, 300, 400, 400)
                self.setWindowTitle('Icon')
                self.setWindowIcon(QtGui.QIcon('icon.png'))

            def defineButton(self):
                button = QtGui.QPushButton("Quit", self)
                #将定义好的Button与所要执行的程序相连(signal and slot)
                button.clicked.connect(QtGui.qApp.quit)
                #使用推荐的Button大小,设置Button位置
                button.resize(button.sizeHint())
                button.move(100, 100)

            def defineMenu(self):

                #定义一个动作及其各种属性(快捷键,状态提示,动作触发的程序)
                exitAction = QtGui.QAction('&Exit', self)
                exitAction.setShortcut('Ctrl+Q')
                exitAction.setStatusTip('Exit application')
                exitAction.triggered.connect(QtGui.qApp.quit)

                self.statusBar()
                menubar = self.menuBar()
                fileMenu = menubar.addMenu('&File')
                fileMenu.addAction(exitAction)


            def defineToolbar(self):
                #定义一个动作
                exitAction = QtGui.QAction('&Exit', self)
                exitAction.setShortcut('Ctrl+Q')
                exitAction.setStatusTip('Exit application')
                exitAction.triggered.connect(QtGui.qApp.quit)

                self.toolBar = self.addToolBar("Extraction")
                self.toolBar.addAction(exitAction)


        def main():
            app = QtGui.QApplication(sys.argv)
            ex = Example()
            sys.exit(app.exec_())


        if __name__ == '__main__':
            main()

When runnuing this example, we should see a window like this:
SimplExample

In this example, all the actions I used are QtGui.qApp.quit because I am lazy, but we can definely use self defined methods here.

### PyQt-Fluent-Widgets 的使用教程与下载 #### 1. PyQt-Fluent-Widgets 简介 PyQt-Fluent-Widgets 是基于 C++ Qt 和 PyQt/PySide 开发的个流畅设计组件库,旨在提升 Qt 应用程序的用户体验。它提供了丰富的控件集合,支持现代 UI 风格的设计[^1]。 --- #### 2. 安装方法 为了使用 PyQt-Fluent-Widgets,需先确保 Python 及其包管理工具 `pip` 已正确安装。以下是两种主要的安装方式: ##### (1) 轻量版安装(不包括 AcrylicLabel) 适用于不需要高级功能的应用场景: ```bash pip install PyQt-Fluent-Widgets -i https://pypi.org/simple/ ``` ##### (2) 完整版安装 如果需要完整的功能集(如 AcrylicLabel),可以执行以下命令: ```bash pip install "PyQt-Fluent-Widgets[full]" -i https://pypi.org/simple/ ``` 这两种安装方式均依赖于官方 PyPI 源,因此网络环境可能会影响安装速度[^3]。 --- #### 3. 基本使用示例 以下是个简单的例子,展示如何创建个带有透明按钮 (`TransparentPushButton`) 的窗口应用: ```python from PySide6.QtWidgets import QApplication, QWidget from qfluentwidgets import TransparentPushButton app = QApplication([]) window = QWidget() # 创建透明按钮并设置文本 button = TransparentPushButton("Cancel", window) # 设置按钮图标(可选) button.setIcon("cancel_icon.png") # 显示按钮和窗口 button.show() window.show() app.exec() ``` 此代码片段展示了如何利用 `TransparentPushButton` 控件实现具有透明背景的按钮,并保持交互效果[^4]。 --- #### 4. 更多功能探索 除了基础的按钮控件外,PyQt-Fluent-Widgets 还提供了系列其他实用的功能模块,例如对话框、导航栏、状态提示等。具体文档和案例可以通过访问项目主页获取更多信息: - **项目地址**: [https://gitcode.com/gh_mirrors/py/PyQt-Fluent-Widgets](https://gitcode.com/gh_mirrors/py/PyQt-Fluent-Widgets)[^1] --- #### 5. 注意事项 在实际开发过程中需要注意以下几点: - 如果遇到安装失败的情况,请确认本地环境是否满足最低版本要求。 - 对于某些特殊功能(如 Acrylic 效果),可能需要额外配置操作系统级别的权限或依赖项[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值