0

I am learning python.I am new to it.

http://zetcode.com/gui/pyqt4/firstprograms/

from this website, I need help understanding the code.

#
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()
#

My question is below

app = QtGui.QApplication(sys.argv)

in the code, app variable seems actually used. so why does it have to assign? and what is purpose of it? for me, w seems window object...I am confused..

if __name__ == '__main__':
    main()

where __name__ and __main__ comes from? what is the functionality of this if statement?

2
  • ->in the code, app variable is not used Commented Feb 10, 2015 at 21:21
  • ->where underscore "name" and "main" comes from? what is the functoinality of this if statement? Commented Feb 10, 2015 at 21:26

3 Answers 3

1

From the page you quote itself:

Every PyQt4 application must create an application object. The application object is located in the QtGui 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.

You need to initialize a QApplication object because that is what is centric to Qt handling all of your environment. If you want to learn more on that, have a look at Qt's (as in the overall project, not only PyQT) introductionary documentation.

Regardin __name__: that's a basic python thing; it's the name under which the module (in this case the module is the python file) is loaded; __main__ is the magic value that variable gets when you run the python file directly (by making it executable and running it or by running it python pythonfile.py).

Sign up to request clarification or add additional context in comments.

3 Comments

name variable, in this case, will be like string underscore "main"? and compare that and go into main() class?
it's the comparison exactly like in the code; the variable __name__ gets compared to the string '__main__'. If the comparison returns True, main() is executed. We can't teach you the very basics of python. Start at python.org and read the first steps.
sorry about my silly quetion but thank you for answering. I could not understand why it has to have such a conditional statement, just start running main()....but thank you very much.
0

If you try to run without this line you'll find the answer: no GUI component (i.e., window, in this case) can be created without the app object existing first.

In short, it is the master object in which all other Qt gui objects live, and it controls the event loop. You start the event loop with the exec_() call, which tells the app object to run with all the things that have been defined for it so far.

1 Comment

then, assigning app variable makes something like GUI playground field and we can play with it using QtGui.QWidget()?
0
if __name__ == "__main__": 
    # do something 

is a common code block to test if the script is run as 'main' or imported as a module. if this code was imported by another script this block would not run.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.