zoukankan      html  css  js  c++  java
  • Simple example

    This is a simple example showing a small window. Yet we can do a lot with this window. We can resize it, maximise it, or minimise it. This requires a lot of coding. Someone already coded this functionality. Because it repeats in most applications, there is no need to code it over again. PyQt4 is a high level toolkit. If we would code in a lower level toolkit, the following code example could easily have hundreds of lines.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    In this example, we create a simple
    window in PyQt4.
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: October 2011
    """
    
    import sys
    from PyQt4 import QtGui
    
    
    def main():
        
        app = QtGui.QApplication(sys.argv)
    
        w = QtGui.QWidget()
        w.resize(250, 150)
        w.move(300, 300)
        w.setWindowTitle('Simple')
        w.show()
        
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    The above code shows a small window on the screen.

    import sys
    from PyQt4 import QtGui
    

    Here we provide the necessary imports. The basic GUI widgets are located in the QtGui module.

    app = QtGui.QApplication(sys.argv)
    

    Every PyQt4 application must create an application object. The application object is located in theQtGui module. The sys.argv parameter is a list of arguments from the command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.

    w = QtGui.QWidget()
    

    The QtGui.QWidget widget is the base class of all user interface objects in PyQt4. We provide the default constructor for QtGui.QWidget. The default constructor has no parent. A widget with no parent is called a window.

    w.resize(250, 150)
    

    The resize() method resizes the widget. It is 250px wide and 150px high.

    w.move(300, 300)
    

    The move() method moves the widget to a position on the screen at x=300 and y=300 coordinates.

    w.setWindowTitle('Simple')
    

    Here we set the title for our window. The title is shown in the titlebar.

    w.show()
    

    The show() method displays the widget on the screen. A widget is first created in memory and later shown on the screen.

    sys.exit(app.exec_())
    

    Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the exit() method or the main widget is destroyed. The sys.exit()method ensures a clean exit. The environment will be informed how the application ended.

    The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_()was used instead.

    SimpleFigure: Simple

  • 相关阅读:
    [翻译].NET Shell Extensions
    设计模式复习小结一(Strategy Pattern/Observer Pattern/Decorator Patter/Factory Pattern)
    学习定制自己的博文1
    SQLServer复习文档1(with C#)
    C#学习笔记-基础知识篇(不定期更新)
    C#学习笔记-迭代器模式
    C#学习笔记-组合模式
    C#学习笔记-备忘录模式
    C#学习笔记-适配器模式
    C#学习笔记-状态模式
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4434350.html
Copyright © 2011-2022 走看看