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代码质量吧:正确使用String、StringBuffer、StringBuilder
    IAAS云计算产品畅想-云主机产品内涵
    Boa服务器在ARM+Linux上的移植
    二叉树的遍历的迭代和递归实现方式
    OpenRisc-42-or1200的ALU模块分析
    根据不同的下拉值,出现相应的文本输入框
    boost------asio库的使用2(Boost程序库完全开发指南)读书笔记
    C++一些注意点之操作符重载
    mac下修改mysql的默认字符集为utf8
    jquery_EasyUI的学习
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4434350.html
Copyright © 2011-2022 走看看