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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章