• PyQt4工具栏


    工具栏

    菜单对程序中的所有命令进行分组防治,而工具栏则提供了快速执行最常用命令的方法。

    #!/usr/bin/python
    # -*- coding:utf-8 -*-
    
    import sys
    from PyQt4 import QtGui, QtCore
    
    class MainWindow(QtGui.QMainWindow):
        def __init__(self, parent = None):
            QtGui.QMainWindow.__init__(self)
            self.resize(250, 150)
            self.setWindowTitle('tool bar')
            self.exit = QtGui.QAction(QtGui.QIcon('exit.png'), 'Exit', self)
            self.exit.setShortcut('Ctrl+Q')
            self.connect(self.exit, QtCore.SIGNAL('triggered()'), 
                              QtGui.qApp, QtCore.SLOT('quit()'))
            self.toolbar = self.addToolBar('Exit')
            self.toolbar.addAction(self.exit)
            
    app = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

    效果:

    点击工具栏图标窗口关闭。

            self.exit = QtGui.QAction(QtGui.QIcon('exit.png'), 'Exit', self)
            self.exit.setShortcut('Ctrl+Q')

    GUI程序的行为是由命令来控制的,这些命令可以来自菜单、上下文菜单、工具栏或它们的快捷方式。PyQt通过引入actions来简化变成难度,一个action对象可以拥有菜单、文本、图标、快捷方式、状态信息、“这是什么?”文本或工具提示等。在我们的示例程序中,我们定义了一个拥有图标/工具提示和快捷方式的action对象。

    self.connect(self.exit, QtCore.SIGNAL('triggered()'), QtGui.qApp, QtCore.SLOT('quit()'))

    // 这里用

    //  self.exit.connect(self.exit, QtCore.SIGNAL('triggered()'), QtGui.qApp, QtCore.SLOT('quit()'))

    // 令我的感觉好像是用什么来connect是随意的,主要是信号-槽机制的信号与槽之间的关系。

    该语句将action对象的triggered()信号连接到与定义的quit()槽函数。

    self.toolbar = self.addToolBar('Exit')

    该语句创建一个工具栏,然后使用语句self.toolbar.addAction(self.exit)将action对象(这里是exit)添加到该工具栏。

  • 相关阅读:
    MongoDB的安全写入GetLastError
    mysql更新字段部分内容,连接条件过滤
    markdown 语法练习(样式输出)
    markdown 语法练习
    数据科学家访谈录 摘录(二)
    使用docker容器,创建镜像
    docker contioner报错:locale.Error: unsupported locale setting
    psql: FATAL: database "" does not exist 解决步骤
    ubuntu下docker 安装、使用mysql
    ubuntu使用crontab启动定时任务
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5327617.html
Copyright © 2020-2023  润新知