失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python gui界面实例_Python界面(GUI)编程PyQt5工具栏和菜单

python gui界面实例_Python界面(GUI)编程PyQt5工具栏和菜单

时间:2022-08-31 08:50:31

相关推荐

python gui界面实例_Python界面(GUI)编程PyQt5工具栏和菜单

工具栏

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

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

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

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

import sys

import requests

import json

from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QWidget

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QAction

from PyQt5.QtWidgets import QLineEdit, QTextEdit

from PyQt5.QtWidgets import QStatusBar

from PyQt5.QtWidgets import QToolBar

from 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

运行效果:

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

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

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

import sys

from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QWidget

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QAction

from PyQt5.QtWidgets import QLineEdit, QTextEdit

from PyQt5.QtWidgets import QStatusBar

from PyQt5.QtWidgets import QToolBar

from 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

运行效果:

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

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

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

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

import sys

from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QWidget

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QAction

from PyQt5.QtWidgets import QLineEdit, QTextEdit

from PyQt5.QtWidgets import QStatusBar

from PyQt5.QtWidgets import QToolBar

from 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

运行效果:

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

import sys

from PyQt5.QtCore import Qt

from PyQt5.QtWidgets import QWidget

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QAction

from PyQt5.QtWidgets import QLineEdit, QTextEdit

from PyQt5.QtWidgets import QStatusBar

from PyQt5.QtWidgets import QToolBar

from 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

运行效果:

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

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

import sys

from PyQt5.QtCore import Qt

from PyQt5.QtCore import QSize

from PyQt5.QtGui import QIcon

from PyQt5.QtWidgets import QWidget

from PyQt5.QtWidgets import QApplication

from PyQt5.QtWidgets import QDesktopWidget, QMessageBox, QLabel, QPushButton, QAction

from PyQt5.QtWidgets import QStatusBar

from PyQt5.QtWidgets import QToolBar

from 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

运行结果:

精美的16x16图标

下载地址:/usr/uploads//08/236663.zip

图片和内容源自网络分享,若有侵权,请联系删除!

上海艾磊科技有限公司专门为企业提供IT咨询,IT外包,系统集成,以及各类IT增值服务。其中增值服务包括OFFICE 365云服务,鼎捷企业ERP管理软件,云备份,企业邮箱,无线覆盖,上网行为管理,VPN架设,网络安全服务,INTERNET接入,设备租赁, IP电话服务

如果觉得《python gui界面实例_Python界面(GUI)编程PyQt5工具栏和菜单》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。