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

  • 相关阅读:
    java环境的配置
    java基本语法
    QTP的那些事QTP回放iFrame控件时间非常慢的问题分析
    QTP的那些事有关xml的操作函数
    QTP的那些事xpath的使用(转)
    QTP的那些事有关web的自动化测试框架saffron的使用
    selenium的那些事命令行启动selenium并运行测试(转)
    QTP的那些事XPath的重要使用
    QTP的那些事有关report manager的使用
    测试环境服务器windows server 2003资源下载
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4434350.html
Copyright © 2011-2022 走看看