About a PyQt example program

前端 未结 3 387
感情败类
感情败类 2021-01-17 19:27

I\'m currently need GUI library for a project. I\'m familiar with python and found PyQt might be a good choice.

I\'m reading a tutorial about PyQt, and quite confuse

3条回答
  •  渐次进展
    2021-01-17 20:01

    It becomes more clear when one has some experience with low level programming, for example in Winapi or the X toolkit in C language. PyQt is a (very) high level toolkit. It comes with a huge built-in functionality. Similar example would require hundreds or maybe thousands of lines of C code. As a consequence, there is a lot of going on behind the scenes. Somebody already created code that deals with painting on a basic level. GUI programming is very complex and with modern GUI toolkits the application programmer is shielded from this complexity. It is inevitable that programmers are confused if they do not know all the technical details.

    In PyQt, we are essentially dealing with events in two ways. We connect signals to slots or reimplement event handlers (an event handler is a synonym for a slot). The provided example inherited from the QtGui.QWidget which already has some painting code available. In order to do our custom painting, we have to reimplement the existing paintEvent() event handler. Depending on the situation, we may or may not call the parent's paintEvent() method.

    The sys.exit(app.exec_()) does not call the paintEvent() method. The exec_() method starts an event loop. The event loop catches and dispatches events. Paint events are triggered by users or by the operating system. For example, when we launch the example, the paint event is triggered twice. (Put a print "Event occurred" line in the event handler to see how many times this method is called.) Resizing windows, loosing or gaining focus, minimizing or maximizing windows, all these cause painting events to be triggered.

提交回复
热议问题