Your first GUI app with Python and PyQt


Many people struggle with learning how to build a GUI app. The most common reason is, they don’t even know where to start. Most tutorials are purely text based, and it’s hard to learn GUI development using text, since GUIs are mainly a visual medium.

We will get around that by building a simple GUI app, and show you how easy it is to get started. Once you understand the basics, it’s easy to add advanced stuff.

This is what we will be building:


A simple GUI app that takes in a price, a tax rate and calculates the final price.

Most tutorials on GUI apps try to layout the GUI blocks using code, but that is very painful to do. We will be using the superb QT Designer tool to layout our app:


So no struggling laying out the design by hand. Everything will be done graphically.

All the source code is here.


If you followed my advice and installed Anaconda, you will already have PyQt4. If not, you will need to get it from here.

You also need Qt Designer.

Update: As Steve points out in the comments, Anaconda comes with qt-designer, so you don’t need to download anything! It is in: …\Anaconda3\Library\bin and is called designer-qt4.exe.

I recommend you download the whole QT suite, as there are some other useful tools in there too.

Getting Started

Note: You can click on any image below to see the full size, in case you want more detail.

Start QT Designer. In the window that comes up, choose Main Window, as it will give is a blank canvas:



The next thing to do is to select the Text Edit box on the left:



Drag Text Edit to the main window, to get a box like:



See the right side, where I have clumsily red circled a box? That is where the name of the object is. The name is the way this object will be called from our Python code, so call it something sensible.



I’m calling it price_box, as we will enter the price into this box. The next thing we will do is attach a label to the box, to make it clear to the user what this box is for.


Above, I have circled the label. Drag it across to the main window.



It gets the default text of TextLabel. Double click it and change it to Price. You can also make the text large and bold, as seen here:


For the tax box, we are going to use something different. See the spin box:



The circle on the left is a spin box. This limits the values you can enter. We don’t need a spinbox, it’s just good to see how you can use different widgets that QT Creator provides. Drag the spin box to the window. The first thing we do is change the objectName to something sensible, tax_rate in our case. Remember, this is how this object will be called from Python.


We can choose a default value for our spinbox. I’m choosing 20:


If you look at the image above, you can also set the minimum and maximum limits. We will keep them to what they are.

We will also add another label called Tax Rate, same as we did before. Also look at the circled Push Button we will be using next:



Now, select the Push Button box and drag it to our window.


The button just says PushButton, which isn’t very helpful. By now, you should know how to change this. But before that, we change the name of the button (and not the text) to calc_tax_button.


Next,  we change the actual text:


Drag another Text Edit box on to the window. You don’t need to label it, as we will print the output in here. Do change it’s name to results_window (not shown below, but you should know how to do it by now).


If you want, you can add a header. This is a simple label box with the font increased:


And save your work:

qt017This file will be used in the next part when we write the code, so store it somewhere you can access it.

This file that we  created is just a XML file. Open it in a text editor, if you want, and you will find something like this:

Writing the code

Qt’s code is object oriented, and in  a manner that is easy to follow. Each of the widgets we added is an object, with it’s own functions like toPlainText() (to read the value in a box). This makes it quite easy to use.

I’m sure the official documentation mentions this somewhere, but you have to do some setup before you can use the code. I couldn’t find this setup anywhere, so I worked back from the official examples (as well as other online tutorials) to find the smallest program you need to initialize the class. I have checked this function in as

This is useful, as everytime you start a new PyQt project, use this skeleton to start off, and add your code.

The code is:

The main thing to note is line 3:

This is where you add the file you created earlier. It is loaded using the inbuilt function:


Let’s take a quick look at the code:

The main code creates a new Qt Gui application. Passing in sys.argv is required, as QT can be configured from the command line. We won’t be doing that.
Finally, we create a class called MyApp, which inherits from Qt libraries and initializes the parent classes:

You don’t need to know the details of this code. Just use the skeleton and work on that.

Take this file,, and rename it to That’s because we don’t want to edit the original file.
The first thing to do is add our XML file, the one that contains our GUI, to the code. Replace this line:


This will load our GUI file into memory. Now, the key widget in our GUI was the button. Once you press the button, something happens. What? We need to tell our code what to do when the user presses the Calculate Tax button. In the __init__ function, add this line:

What does this do? Remember we called our button calc_tax_button? (This was the name of the object, not the text that was displayed on it.) clicked is an internal function that is called when (surprise) someone clicks on the button. All QT widgets have specific functions, which you can find our by Googling. The last part of the code says connect(self.CalculateTax). This says that connect this button to a function called self.CalculateTax, so that everytime the user presses this button, that function is called.

We haven’t written that function yet. Let’s do it now.

In the MyApp Class, add another function. We will look at the whole function first, and then go into details:

Okay, let’s look at the code above line by line.

We have to do two things: Read the price box, read the tax box, and calculate the final price. Let’s do that now. Remember, we will call the objects by the names we gave them (which is why I asked you not to use the default generic names like box1, as that would have been confusing pretty soon).

We read our price_box. toPlainText() is an internal function that reads the value stored in that box. You don’t have to remember all these functions, by the way. I just Google something like “Qt Textbox read data” to find out what the name of the function is, though you will start to remember the names after some time, as they are very logically named.

The read value is a string, so we convert it to an integer and store it in a variable called price.

Next, we read the tax box:

Again, value() is the function for reading from a spinbox. Thanks, Google.

Now that we have both these values, we can calculate the final price using very high tech maths:

We create a string with our final price. This is because we will output this string directly to our app:

In our results_window, we call the function setText(), which outputs the string we created.

Just run the file using:



And there you go. A simple introduction to PyQt.

If you want more fun, trying playing with the different widgets, though be warned, you can add too many widgets and make your app confusing to use.

Are you interested in seeing more on this? If so, please leave a comment, or contact me.

104 thoughts on “Your first GUI app with Python and PyQt”

  1. Hi
    Very interesting tool as Python always lacked a good GUI. Wonder whether this will work on a Raspberry Pi platform?



    1. QT5 is the shinier newer version. Unfortunately, it breaks backward compatibility.

      I chose PYQy4 merely because it came with my install of Anaconda.

  2. Hi Shantnu,

    Awesome guide, really helped me out. Thanks for posting!

    I’m a Mechanical Engineer using Qt Designer alongside PySide to develop engineering analysis applications. I would love for some more tutorials on using Qt Designer and its implementation through PySide (or Qt). Specifically, I’m trying to implement plots from matplotlib into a GUI designed using Qt designer.

    Any more tutorials/help on this subject matter would be greatly appreciated!

    Thanks so much!


    1. Thanks Lemay.

      When I last tried PySide, I found it poorly documented & hard to use, but maybe it has improved now.

      Let’s see.

  3. As a complete beginner I struggled getting this to work with PyQt5, however i think i’m finally there (it works at least!). This may be of help to someone else:


    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5 import uic

    Ui_MainWindow, QtBaseClass = uic.loadUiType(“tax_calc.ui”)

    class MyApp(QMainWindow):
    def __init__(self):
    super(MyApp, self).__init__()
    self.ui = Ui_MainWindow()

    def CalculateTax(self):
    price = int(self.ui.price_box.toPlainText())
    tax = (self.ui.tax_rate.value())
    total_price = price + ((tax / 100) * price)
    total_price_string = “The total price with tax is: ” + str(total_price)

    if __name__ == “__main__”:
    app = QApplication(sys.argv)
    window = MyApp()

    1. Hello Sam,
      I tried to use your code for QT5, but I get every time this error message:

      RESTART: H:/Programmierung Python/Projekte/test UI/PyQt_first-master/
      Traceback (most recent call last):
      File “H:/Programmierung Python/Projekte/test UI/PyQt_first-master/”, line 7, in
      class MyApp(QMainWindow):
      File “H:/Programmierung Python/Projekte/test UI/PyQt_first-master/”, line 23, in MyApp
      window = MyApp()
      NameError: name ‘MyApp’ is not defined

      I used original your source and WinPython

      Sorry, I am new in Python AND QT5.
      Can you help me?

      1. Hi Guenter. Looks like you are inheriting the wrong stuff in “class MyApp(QMainWindow):”. That line should be “class MyApp(QtGui.QMainWindow, Ui_MainWindow):”.

    2. I am Using the same code but still getting error

      Traceback (most recent call last):
      File “C:\Users\saksh\Desktop\Python3Learn\”, line 8, in
      class MyApp(QtGui.QMainWindow, Ui_MainWindow):
      AttributeError: module ‘PyQt5.QtGui’ has no attribute ‘QMainWindow’

      here’ the code

      import sys

      from PyQt5 import QtCore, QtGui, QtWidgets
      from PyQt5 import uic

      Ui_MainWindow , QtBaseClass = uic.loadUiType(“”)

      class MyApp(QtGui.QMainWindow, Ui_MainWindow):
      def __init__(self):
      super(MyApp , self).__init__()
      self.ui = Ui_MainWindow()

      def CalculateTax(self):
      price = int(self.ui.price_box.toPlainText())
      tax = (self.ui.tax_rate.value())
      total_price = price + ((tax/100)*price)
      total_price_string = “The total price with tax is : “+str(total_price)

      if __init__ == “__main__”:
      app = QApplication(sys.argv)
      window = MyApp()

      I am usingPyQT5…..
      Python 3.5.2 32 bit

      1. My example is written in PyQt4.

        If you want to use PyQt5, look at the other comments, someone got it working with Pyqt5

    3. Thanks Sam for the effort. It works great. I am new to Python.
      How to get rid of the following warning (perhaps a simple one ?) when I run the .py file from Spyder ?


      UserWarning: To exit: use ‘exit’, ‘quit’, or Ctrl-D.
      warn(“To exit: use ‘exit’, ‘quit’, or Ctrl-D.”, stacklevel=1)
      An exception has occurred, use %tb to see the full traceback.

      SystemExit: -1


      1. Hi, had same issue and foumd the answer on a couple of stackoverflow pages
        First of all add this line


        after the line app = QApplication(sys.argv)

        This solve the problem of kernel dying.
        Then to get rid of warning you have to substitute sys.exit(app.exec_()) with app.exec_()

        Hope it works for you as well

        1. Hi Richard. Your modifications work for me as well. Thank you very much for the suggestion.

          On a different issue: When I run this program successively a couple of times, I get a Windows screen saying “Python has stopped working…Close the program / Restart the program ?” . After dismissing that the screen (or choosing close the program), my IPython kernel restarts again. Now the on launching the program again it works fine a couple of time successively before it flips again.

          Any thoughts/ suggestions on this ?



        2. Your suggestion solve my problem ! It was pretty unpleasant to restart the kernel every time… So thanks a lot Richard

        3. Hello Richard,
          That was the most helpful until now for me thank you very much…
          However there is one more issue.. the Kernel dies and “doesnt die” one after another time I run my code . This is consistent no matter how many times I run it. A possible solution would be much appreciated 🙂 Thank again.

    4. Sam, you are a godsend and currently My Hero. I’m a beginner (by now I guess you’ve become an expert) and being bald, had no hair to tear out in tufts. You’ve saved my sorry butt. Kudos and thank you.

  4. Its excellent,thanks for your tutorial,if you can share more tutorial then again it will helpful for all of us.i am trying to make application for raspeberry pi B

    1. Thanks Manoj.

      I am writing a few more articles. Subscribe to my list if you want to know when the next one will be out, or just check the website regularly.

  5. Hi,

    Your introduction about how to direct import ui file by ‘qtCreatorFile’ method instead pyuic was very useful to me; however I have question about popup windows by button click/press in main window…

    1. to design popup window, should I create new ui file by qt designer or can be save in mainwindow ui?
    2. how to load the popup window ui, and issue signal to trigger this popup window?


    1. Depends. Is it a new window, or do you want to put it in the existing one?

      If new, you’ll have to do a new design. Otherwise, chuck it in mainwindow.

      As to how to switch between them, I cant remember off the top of my head. You will have to Google it.

  6. Thank you so much, Shantnu and Sam : you two made my day !
    Shantnu, how can I subscribe your list please?
    Sam, on my Archlinux, I had to change ” to ‘ and had some indentation.

  7. Hi,
    I am following this skeleton but I get an error when calling setupUi. This is the code:
    from PyQt4 import QtCore, QtGui, uic

    Ui_MainWindow, QtBaseClass = uic.loadUiType(“mainwindow.ui”) # Load the UI

    class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
    QtGui.QMainWindow.__init__(self, parent)

    And the error:
    File “”, line 29, in setupUi
    TypeError: __init__() takes exactly 1 argument (2 given)

    Any ideas? Has anybody else experienced this? No idea how to solve it…

    1. In the __init__ function, why are you passing *parent*? That’s what its complaining about. It only expects *self*, but is getting an extra variable.

  8. Hi Shantnu,

    Thanks for the tutorial!

    But I am stuck when I do


    on Powershell

    it says no module named PyQt4.

    I am really a newbie in whole programming so I am not sure what this means. I searched on google but the subject quickly boils down to other things that I have difficulty understanding. Could you help me or guide me to some resources that I could solve this?

    Many thanks.

    1. It means PyQt isn’t on the path, or not installed correctly.

      How did you install Qt?

      I strongly recommend you install the Anaconda Python version, the link to which is at the start.

      You can have multiple versions of Python, so this won’t interfere with your existing Python.

  9. Hi Andre,

    Thanks for a great tutorial. Btw, i’ve encountered an error when execute the code and need ur advise on that.

    File “/home/pi/”, line 25, in
    window = MyApp()
    File “/home/pi/”, line 14, in __init__
    AttributeError: ‘MyApp’ object has no attribute ‘calc_tax_button’

    i’m totally newbies with programming world. currently running linux on RPI2.


      1. Hi Andre,

        Thanks for your reply.
        Yes I did include the ui file and follow the the step as well.
        The program will be execute if I bypass the self.calc_tax_button.
        Anything wrong on the button naming?

        1. Yes, it looks like a typo. Check the names of the buttons, and how you are calling the buttons in the code. There’s going to be a typo in one of them.

  10. Great stuff. I could run from the cmd command line no problem. However, if I want to add some sweet debugging sauce what IDE/DE do you recommend and how would one get started running this little program within one. I tried to simply open the py file from within QtCreator (file/open) and the code-text looks nice and coloured but all the “run” options are greyed out.

  11. AS per my previous comment I fuigured out how to get an IDE working. After following your nice how-to I followed the instructions at (although I installed LiClipse which has pyDev and eclipse bundled together). Then I copied your python code into the file I created inthe linked how to and the ui file into the same directory as my file and it ran using the “Run as” “python run” command.

  12. Hi and thank you for this great post!
    I want to have multiple UI in my program. what should i do?
    for example when a button pressed it goes to another UI (without opening new window).

    thank you agin!

  13. Like the way you explain in detail, something most stuff about linking the GUI to the code does not do well. The problem with YouTube videos is they sometimes jump around from one thing to another and get hard to follow… Thanks for your time and effort.

  14. Is there a way you have tried to convert the GUI to a Windows executable? Will py2exe work? I could do this with my old GUI tool and found it was extremely beneficial to create apps for my non-Python savvy co-engineers.

    1. There are several tools like Py2exe, each with their own quirks and problems. But I haven’t tried any, so can’t make any suggestions at the moment.

      1. I will have to try and see I guess. Thanks for putting the tutorial together! Your approach of a skeleton starting point is how I learned Python 12 years ago using PythonCard. I’ve looked at other PyQt tutorials but was hoping for the shortest path to a working GUI which is what you show here. I recently was introduced to Anaconda which has most of the tools I use but had to go shopping for a GUI solution. I think I found it in your tutorial.

  15. Hi Shantnu,
    I wanted to add my thanks for your great tutorial. I just started Mark S’s book, but was a little impatient to get a first py gui done. Everything worked flawlessly…now, I just have to learn to do it with decimal values. One quick comment, people might be interested to know that with Anaconda3, the QtDesigner is available without downloading the whole Qt suite. It took a little searching, but the executable should be in …\Anaconda3\Library\bin and is called designer-qt4.exe.
    Thanks again.

  16. Hi, can you explain how can i do a button that changes the screen, please? Im totally noob with python and pyqt.

  17. Another approach to using the “” is to auto generate a .py file from the .ui file like this:

    pyuic my_GUI.ui -o


    pyuic my_GUI.ui >

    Then create a and import as follow:-


    from PyQt4 import QtGui # Import the PyQt4 module we’ll need
    import sys # We need sys so that we can pass argv to QApplication

    import my_GUI # This file holds our MainWindow and all design related things
    # it also keeps events etc that we defined in Qt Designer

    class MyWindow(QtGui.QMainWindow, my_GUI.Ui_MainWindow):
    def __init__(self):
    # Explaining super is out of the scope of this article
    # So please google it if you’re not familar with it
    # Simple reason why we use it here is that it allows us to
    # access variables, methods etc in the file
    super(self.__class__, self).__init__()
    self.setupUi(self) # This is defined in file automatically
    # It sets up layout and widgets that are defined


    # <<<>>>
    def CalculateTax(self):
    price = int(self.price_box.toPlainText())
    tax = (self.spinBox.value())
    total_price = price + ((tax / 100) * price)
    total_price_string = “The total price with tax is: ” + str(total_price)

    def main():
    app = QtGui.QApplication(sys.argv) # A new instance of QApplication
    form = MyWindow() # We set the form to be our MyWindow (design) # Show the form
    app.exec_() # and execute the app

    if __name__ == ‘__main__’: # if we’re running file directly and not importing it
    main() # run the main function

  18. Hello, that was a great tutorial of linking QT Designer with PyQt. Very simple.
    How do I go about creating a QDialog by pressing a button? What should I do?


  19. This is code for Qt5 and pyqt5 but I have a problem. it does not work. When I start it from spyder it reports kernel die, than on second attempt it shows window but when I enter number and press button nothing happens. When I start it from the shell it opens but when i enter a number and press button it shut down itself.

    import sys
    from PyQt5.QtWidgets import QMainWindow, QApplication
    from PyQt5 import uic

    Ui_MainWindow, QtBaseClass = uic.loadUiType(“tax_calc_qt.ui”)

    class MyApp(QMainWindow):
    def __init__(self):
    super(MyApp, self).__init__()
    self.ui = Ui_MainWindow()

    def CalculateTax(self):
    price = int(self.price_box.toPlainText());
    tax = (self.tax_rate.value());
    total_price = price + ((tax / 100) * price);
    total_price_string = “The total price with tax is: ” + str(total_price);

    if __name__ == “__main__”:
    app = QApplication(sys.argv)
    window = MyApp()

  20. NameError: name ‘uic’ is not defined
    This is what I get when I enter the code to take the ui file into python. Couldn’t sort it out. Please help

  21. thanks a lot for your detailed explanation, it helped me too much, now i’ll try to read a sensor value in a raspberry pi and try to print it in the GUI.

  22. Don’t know if anyone has noted that before (searched a bit) but on the tax calculation part the tax/100 is wrong.
    To actually produce a tax you must cast the 100 as a float like this:
    total_price = price + ((tax /float(100)) * price) # Calculate tax

    Ps : Thank you for the awesome tutorial, simple and valuable!

  23. Thanks Shantnu for a very good tutorial. Very nicely written with just the right amount of reading necessary.

    Can you help me with the necessary code appending to get rid of the following warning when I run it from Spyder :

    UserWarning: To exit: use ‘exit’, ‘quit’, or Ctrl-D.
    warn(“To exit: use ‘exit’, ‘quit’, or Ctrl-D.”, stacklevel=1)
    An exception has occurred, use %tb to see the full traceback.

    SystemExit: -1


  24. Hello, I am new to using PyQt and Qt Designer. I have followed your steps but when I put python into the command prompt, it tells me it can’t find the module PyQt4. I’m not sure what I need to do to fix this.

      1. Yes, I realized this and I fixed that. I am now facing another problem. I tried to test out using resource images with my program but I can’t run it. It says it can’t find my resource file. I have seen that the solution for some has been to convert the resource file into a py file using pyrcc4 but that command is unrecognized for me. What can I do?

          1. My images resource file. I run the program and it give me the error that it can’t find images_rc.

  25. Hey Guys , I used pyqt5 and things were actually very much easier to do than with pyqt4

    Using pyqt5 with qt designer version 5.6.2 and python 2.7

    Install anaconda2 with py 2.7 to have a hassle free installation of all the components

    python 3 and above versions should be the same but I just never worked on it I can’t guarantee everything will work as I have done

    Open Qt designer from the anaconda\library\bin folder

    Design , save as .ui file

    shift+right click to open cmd in the folder wherever the file is there and in the cmd promt use

    pyuic5 -x [filename].ui -o [newname].py

    then open the .py file in spyder

    You will see that most of the objects and windows are all defined for you already

    If you see these lines at the start of the file, you are good to go
    from PyQt5 import QtCore, QtGui, QtWidgets
    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

    the setupUi function here is basically your init class and you have to give all your widget etc related conditions here

    Always keep your push button function calls (our self.calc_tax_button.clicked.connect(self.CalculateTax) in this case) at the end of the setupUi(not after it, just at the end inside it) , or even better make a new function like in this tutorial and call that function from setupUi at the end instead. If you place these calls in between the python kernel which will iterate through all the calls once before compiling will crash and won’t be able to anything as you may have objects in the connected function calls that have not been defined yet.

    Finally, all your functions will go into class Ui_MainWindow(object): identical to setupUi function.

  26. Hello VP and Sasa Ziza:
    try using this at the end of your script:
    if __name__ == “__main__”:
    import sys; app = QtWidgets.QApplication(sys.argv); MainWindow = QtWidgets.QMainWindow(); ui = Ui_MainWindow()
    ui.setupUi(MainWindow);; app.exec_()

    I have another question though. After .ui to .py conversion (with pyuic5 for pyqt5) I have issues in converting the .py to a .exe standalone application. py2exe or cxfreeze are not working properly!Thanks for your help!

  27. Thank you very much,your tutorial is really useful!!
    For Qt5, just using QtWidgets to repalce all the QtGui would be ok.

  28. hello, this was totally awesome. thanks for making it.

    i did encounter an error when trying to put in prices with change. app only takes whole integers atm. any advice on how to work it out?

  29. Can someone plzzzz tell me what is wrong w my code (PyQt5):

    import sys
    from PyQt5 import QtCore, QtWidgets, uic
    from PyQt5.QtWidgets import QMainWindow, QApplication
    qtCreatorFile = “tax.ui”

    Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

    class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
    super(MyApp, self).__init__()
    self.ui = Ui_MainWindow()

    def CalculateTax(self):
    price = int(self.price_box.toPlainText())
    tax = (self.tax_rate.value())
    total_price = price+((tax / 100) * price)
    total_price_string = “The total price with tax is: ” + str(total_price)

    if __name__ == “__main__”:
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()

    I get this error and “price_box” is what I named that box in tax.ui file

    AttributeError: ‘MyApp’ object has no attribute ‘price_box’

  30. Hi,

    I want to integrate QT generated ui file with python code used to control drone written using ROS.

    can you please suggest how to integrate these python and ui files?

    Thanks and Regards,

  31. thanks for tutorial
    sadly with this method the executable file don’t work because the new python file linked to the ui file
    it’s easy to create and view the application with this method

Leave a Reply