分享

Python界面(GUI)编程PyQt5工具栏和菜单

 copy_left 2020-10-13
Python界面(GUI)编程PyQt5工具栏和菜单

工具栏

工具栏是最常见的用户界面元素之一。工具栏是用于在应用程序中执行常见任务的图标和文本栏。

Qt工具栏支持图标,文本的显示,还可以包含任何标准Qt小部件。但是,对于按钮,最好的方法是利用QAction系统在工具栏上放置按钮。

让我们从向应用程序添加工具栏开始。

在Qt中,从QToolBar类创建工具栏。首先,创建该类的实例,然后调用.addToolbar。传入字符串作为第一个参数来设置工具栏的名称,该名称将用于在UI中标识工具栏。

import sysimport requestsimport jsonfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QWidgetfrom PyQt5.QtWidgets import QApplicationfrom PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QActionfrom PyQt5.QtWidgets import QLineEdit, QTextEditfrom PyQt5.QtWidgets import QStatusBar from PyQt5.QtWidgets import QToolBarfrom PyQt5.QtWidgets import QMainWindow class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle('高效码农') label = QLabel('高效码农') label.setAlignment(Qt.AlignCenter) self.setCentralWidget(label) toolbar = QToolBar('高效码农 Toolbar') self.addToolBar(toolbar) self.show() def onMyToolBarButtonClick(self, s): print('click', s)if __name__ == '__main__': app = QApplication(sys.argv) tools = MainWindow() sys.exit(app.exec_()) pass

运行效果:

Python界面(GUI)编程PyQt5工具栏和菜单

我们应该使工具栏更有趣一些。我们可以只添加一个QButton部件,但在Qt中还有一个更好的方法可以让你获得一些很酷的特性——那就是通过QAction。QAction是一个类,它提供了一种描述抽象用户接口的方法。在英语中,这意味着您可以在一个对象中定义多个接口元素,通过与该元素交互的效果来统一这些元素。例如,在工具栏和菜单中都有一些函数表示,比如Edit——>Cut,它既出现在编辑菜单中,也出现在工具栏上,就像剪刀一样,也可以通过快捷键Ctrl-X (Mac上的Cmd-X)。

如果没有QAction,就必须在多个地方定义它。但是使用QAction,您可以定义单个QAction,定义被触发的动作,然后将这个动作添加到菜单和工具栏中。每个QAction都有您可以连接的名称、状态消息、图标和信号(以及更多)。

在下面的代码中,您可以看到添加了第一个QAction。

import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QWidgetfrom PyQt5.QtWidgets import QApplicationfrom PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QActionfrom PyQt5.QtWidgets import QLineEdit, QTextEditfrom PyQt5.QtWidgets import QStatusBar from PyQt5.QtWidgets import QToolBarfrom PyQt5.QtWidgets import QMainWindow class MainWindow(QMainWindow):    def __init__(self, *args, **kwargs):        super(MainWindow, self).__init__(*args, **kwargs)                self.setWindowTitle('高效码农 App')                label = QLabel('高效码农!!!')        label.setAlignment(Qt.AlignCenter)                self.setCentralWidget(label)                toolbar = QToolBar('高效码农 toolbar')        self.addToolBar(toolbar)                button_action = QAction('高效码农 button', self)        button_action.setStatusTip('T高效码农 button')        button_action.triggered.connect(self.onMyToolBarButtonClick)        toolbar.addAction(button_action)        self.show()                    def onMyToolBarButtonClick(self, s):        print('click', s)if __name__ == '__main__':    app = QApplication(sys.argv)    tools = MainWindow()    sys.exit(app.exec_())    pass

运行效果:

Python界面(GUI)编程PyQt5工具栏和菜单

首先,我们创建接收QAction信号的函数,这样我们就可以看到它是否在工作。接下来我们定义QAction本身。在创建实例时,我们可以传递动作的标签和/或图标。您还必须传递任何QObject以作为操作的父对象——这里我们将self作为对主窗口的引用传递。

接下来,我们可以选择设置一个状态提示——一旦我们设置了一个状态提示,这个文本将显示在状态栏上。最后,我们将.triggered信号连接到定制函数。这个信号将在QAction被触发(或激活)时触发。

接下来我们可以添加一个状态栏。

我们通过调用QStatusBar来创建一个状态栏对象,以获得一个新的状态栏对象,然后将其传递给。setstatusbar。因为我们不需要改变状态栏的设置,我们也可以在创建时传入它:

import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QWidgetfrom PyQt5.QtWidgets import QApplicationfrom PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QActionfrom PyQt5.QtWidgets import QLineEdit, QTextEditfrom PyQt5.QtWidgets import QStatusBar from PyQt5.QtWidgets import QToolBarfrom PyQt5.QtWidgets import QMainWindow class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle('高效码农 App') label = QLabel('高效码农!!!') label.setAlignment(Qt.AlignCenter) self.setCentralWidget(label) toolbar = QToolBar('高效码农 toolbar') self.addToolBar(toolbar) button_action = QAction('高效码农 button', self) button_action.setStatusTip('高效码农 button') button_action.triggered.connect(self.onMyToolBarButtonClick) toolbar.addAction(button_action) self.setStatusBar(QStatusBar(self)) self.show() def onMyToolBarButtonClick(self, s): print('click', s)if __name__ == '__main__': app = QApplication(sys.argv) tools = MainWindow() sys.exit(app.exec_()) pass

运行效果:

Python界面(GUI)编程PyQt5工具栏和菜单

接下来,我们将把QAction切换为toggleable——点击将选中它,再次点击将选中取消。为此,我们在QAction对象上调用setCheckable(True)。

import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtWidgets import QWidgetfrom PyQt5.QtWidgets import QApplicationfrom PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QActionfrom PyQt5.QtWidgets import QLineEdit, QTextEditfrom PyQt5.QtWidgets import QStatusBar from PyQt5.QtWidgets import QToolBarfrom PyQt5.QtWidgets import QMainWindow class MainWindow(QMainWindow):    def __init__(self, *args, **kwargs):        super(MainWindow, self).__init__(*args, **kwargs)                self.setWindowTitle('高效码农 App')                label = QLabel('高效码农!!!')        label.setAlignment(Qt.AlignCenter)                self.setCentralWidget(label)                toolbar = QToolBar('高效码农 toolbar')        self.addToolBar(toolbar)                button_action = QAction('高效码农 button', self)        button_action.setStatusTip('高效码农 button')        button_action.triggered.connect(self.onMyToolBarButtonClick)        button_action.setCheckable(True)        toolbar.addAction(button_action)                self.setStatusBar(QStatusBar(self))        self.show()                    def onMyToolBarButtonClick(self, s):        print('click', s)if __name__ == '__main__':    app = QApplication(sys.argv)    tools = MainWindow()    sys.exit(app.exec_())    pass

运行效果:

Python界面(GUI)编程PyQt5工具栏和菜单

单击该按钮可以看到它从选中状态切换到未选中状态。注意,我们现在创建的自定义槽函数会交替输出'True'和'False'。

现在界面功能基本完善,但是很丑;让我们美化一下:

import sysfrom PyQt5.QtCore import Qtfrom PyQt5.QtCore import QSizefrom PyQt5.QtGui import QIconfrom PyQt5.QtWidgets import QWidgetfrom PyQt5.QtWidgets import QApplicationfrom PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QActionfrom PyQt5.QtWidgets import QStatusBar from PyQt5.QtWidgets import QToolBarfrom PyQt5.QtWidgets import QMainWindow class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) self.setWindowTitle('高效码农 App') label = QLabel('高效码农!!!') label.setAlignment(Qt.AlignCenter) self.setCentralWidget(label) toolbar = QToolBar('My main toolbar') toolbar.setIconSize(QSize(16,16)) self.addToolBar(toolbar) button_action = QAction(QIcon('user.png'), 'Your button', self) button_action.setStatusTip('This is your button') button_action.triggered.connect(self.onMyToolBarButtonClick) button_action.setCheckable(True) toolbar.addAction(button_action) self.setStatusBar(QStatusBar(self)) self.show() def onMyToolBarButtonClick(self, s): print('click', s)if __name__ == '__main__': app = QApplication(sys.argv) tools = MainWindow() sys.exit(app.exec_()) pass

运行结果:

Python界面(GUI)编程PyQt5工具栏和菜单

精美的16x16图标


下载地址:https://www./usr/uploads/2020/08/2366200963.zip

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多