PyQt5 and PyCharm

To learn how to build a desktop application, I'm trying PyQt5. On Python wiki, the definition of PyQt is: "PyQt is one of the most popular Python bindings for the Qt cross-platform C++ framework..."
One of the most useful tools is Qt Designer that let us build our GUI with simple Drag'n Drop. For this app I'm not using Qt Designer because I have to understand to code under the hood. Another new experience for me is that I'm using PyCharm Community Edition as a editor.

So, after install PyCharm


I can create a new project with File->New Project and I call it PyQt5_Widget_1


When I press "Create", PyCharm build also a Virtual Environment (as selected in the previous setting).
Then, pressing File->Settings, I can install PyQt5 pressing "+":


and searching PyQt5 in the search field:


Then press "Install Package".
So now we have all that we need to create our first widget.

Right-click on project name->new->python file


Since I'm building a widget with a simple label, I call my file "label" (very original, uh?).
So finally we are in the editor and we can start to write some code lines.

First of all we have to import some packages:

import sys
from PyQt5.QtWidgets import *

Then we can create our class:

class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Window with labels")
self.setGeometry(50, 50, 350, 350)
self.UI()

MyWindow class inherits from QWidget class but I set some stuff I ser with "self".
setWindowTitle is clear;
setGeometry is a method that set position and dimension of our window. It works with these parameters:
QWidget.setGeometry(xpos, ypos, width, height)
self.UI is a function that build our window and show it:
def UI(self):
text1=QLabel("First Label", self)
text2=QLabel("Second Label", self)
text1.move(100,50)
text2.move(200,50)
self.show()
Here, we can create our labels. With text1=QLabel("First Label", self) I create a label that show "First Label" and with tex1.move(100,50) I set the label position in my window.
Then with self.show(), I set my window visible.

The last piece of code is very important:
def main():
app = QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())


if __name__ == '__main__':
main()
The first instruction initializes PyQt5. Passing in sys.argv is required, as QT can be configured from the command line. Then we initialize our window as a MyWindow object. The line sys.exit(app.exec_()): exec() call starts the event-loop and "freeze" or "block" our window until we quit application. So, without this line, the script will exit with "Process finished with exit code 0" without showing our window.

Here the complete code:

import sys
from PyQt5.QtWidgets import *


class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("My Window with labels")
self.setGeometry(50, 50, 350, 350)
self.UI()


def UI(self):
text1=QLabel("First Label", self)
text2=QLabel("Second Label", self)
text1.move(100,50)
text2.move(200,50)
self.show()

def main():
app = QApplication(sys.argv)
window = MyWindow()
sys.exit(app.exec_())


if __name__ == '__main__':
main()
This is all. Hope it will help someone to understand how easy could be use this tool.
I use PyCharm for the first time for this mini app and I'm sure that I've used only a minimal part of potentials of this editor. Give it a try!
As I always say, feel free to leave a message if you have some suggestions or if you find any mistake.


Commenti

AddToAny

Post popolari in questo blog

Something about me

Tkinter, how to show a gif