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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值