Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:32:36+00:00 2026-06-09T15:32:36+00:00

I’m currently need GUI library for a project. I’m familiar with python and found

  • 0

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 confused about the following example program

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we draw text in Russian azbuka.

author: Jan Bodnar
website: zetcode.com 
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
    
        self.initUI()
    
    def initUI(self):      

        self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('Draw text')
        self.show()

    def paintEvent(self, event):

        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawText(event, qp)
        qp.end()
    
    def drawText(self, event, qp):
  
        qp.setPen(QtGui.QColor(168, 34, 3))
        qp.setFont(QtGui.QFont('Decorative', 10))
        qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)        
            
    
def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Here, in main function, an Example object is created, thus __init__() function, initUI() is called.
My question is where does paintEvent() function is called?? since if we run the program, self.text(some Russian letters) will exactly appear on the widget.

In other words, what does sys.exit(app.exec_()) actually do? why it will call paintEvent() function?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:32:38+00:00Added an answer on June 9, 2026 at 3:32 pm

    From PyQt docs:

    int QApplication.exec_ ()

    Enters the main event loop and waits until exit() is called, then
    returns the value that was set to exit() (which is 0 if exit() is
    called via quit()).

    It is necessary to call this function to start event handling. The
    main event loop receives events from the window system and dispatches
    these to the application widgets.

    From another source:

    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.

    About painting:

    4.2.1. When Painting Occurs

    The paintEvent() method is called automatically when

    • Your widget is shown for the first time.

    • After a window has been moved to reveal some part (or all) of the
      widget.

    • The window in which the widget lies is restored after being minimized.

    • The window in which the widget lies is resized.

    • The user switches from another desktop to the desktop on which the
      widget’s window lies.

    You can generate paint events manually by calling QWidget::update().
    QWidget::update() erases the widget before generating the paint event.
    You can pass arguments to update(), which can restrict painting only
    to areas (rectangles, in particular) that need it. The two equivalent
    forms of the method are QWidget::update (int x, int y, int width, int
    height)
    and QWidget::update (QRect rectangle), where x and y give the
    upper-left corner of the rectangle, and width and height are obvious.
    Because update() places a paint event into the event queue, no
    painting occurs until the current method exits and control returns to
    the event handler. This is a good thing because other events may be
    waiting there to be processed, and events need to be processed in a
    timely manner for the GUI to operate smoothly.

    You can also invoke painting of the widget by calling QWidget::repaint (int x, int y, int width, int height, bool erase) (or one of several
    convenience-method forms), where all the arguments mean the same as in
    the case of the update() method, and erase tells repaint whether to
    erase the rectangle before painting it. repaint() calls paintEvent()
    directly. It does not place a paint event into the event queue, so use
    this method with care. If you try to call repaint() repeatedly from a
    simple loop to create an animation, for example, the animation will be
    drawn, but the rest of your user interface will be unresponsive
    because the events corresponding to mouse button clicks, keyboard
    presses, and so on will be waiting in the queue. Even if you are not
    performing a task as potentially time-consuming as animation, it is
    generally better to use update() to help keep your GUI alive.

    If you paint something on your widget outside the paintEvent(), you
    still need to include the logic and commands necessary to paint that
    same thing in paintEvent(). Otherwise, the painting you did would
    disappear the next time the widget is updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I am currently running into a problem where an element is coming back from
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.