PyQt5 for beginners
pip3 install PyQt5
For Linux, open Terminal and type:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pyqt5
Hello World!
And now let’s make a Hello World application. Create a Python file, open it and enter the following code:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow): # главное окно
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Hello, world") # заголовок окна
self.move(300, 300) # положение окна
self.resize(200, 200) # размер окна
self.lbl = QLabel('Hello, world!!!', self)
self.lbl.move(30, 30)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
When you run it, you should get something like this:
Change the font of the inscription
Now let’s change the font of the inscription. Now the code will become like this:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
class MainWindow(QMainWindow): # главное окно
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Hello, world") # заголовок окна
self.move(300, 300) # положение окна
self.resize(200, 200) # размер окна
self.lbl = QLabel('Hello, world!!!', self)
self.lbl.move(30, 30)
self.font = QFont() # создаём объект шрифта
self.font.setFamily("Rubik") # название шрифта
self.font.setPointSize(12) # размер шрифта
self.font.setUnderline(True) # подчёркивание
self.lbl.setFont(self.font) # задаём шрифт метке
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
The example assumes that you already have the Rubik font from Google Fonts installed. If not, you can always download it from here.
More advanced markup with XHTML
Now let’s add XHTML. For example, like this:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow): # главное окно
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Hello, world") # заголовок окна
self.move(300, 300) # положение окна
self.resize(200, 200) # размер окна
self.lbl = QLabel('<i>Hello</i>, <b>world</b>!!! <s><b>123</b></s>', self)
self.lbl.move(30, 30)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
Those with at least some knowledge of XHTML will notice that Hello is in italics, world is in bold, and 123 is both crossed out and bold.
XHTML Cheat Sheet
123 | Thumbnail |
123 | Kuriv |
123 | Underline |
| Strikeout |
| Code (monospace) |
123 | superscript text |
123 | Subscript |
123 | Text size 16 pt |
123 | red text |
123 | Text on a bright green background. |
123 | Center alignment |
By the way, I know this HTML constructor: http://blockly.ru/blockly-html/index.html . Personally, I like him. It’s just hard to insert your own tags.
More inscriptions!
And now we will make 2 inscriptions:
from PyQt5.QtWidgets import *
import sys
class MainWindow(QMainWindow): # главное окно
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi()
def setupUi(self):
self.setWindowTitle("Hello, world") # заголовок окна
self.move(300, 300) # положение окна
self.resize(200, 200) # размер окна
self.lbl = QLabel('<i>Hello</i>, <b>world</b>!!!', self)
self.lbl.move(30, 30)
self.lbl2 = QLabel('<u>Ещё одна метка</u>', self)
self.lbl2.move(50, 50)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
I also added formatting (underlining) to the second one, and removed 123 from the first one.
Window without resize()
All previous examples used this construction:
self.resize(200, 200)
But you can do without this construction, since the widgets will clear the place for themselves.
Hints
You can add a tooltip to all widgets. For example (I give only the part of the code that is important for understanding):
self.lbl.setToolTip('This is a <b>QLabel</b>')
Epilogue
That’s all. In the next part I will try to describe buttons, menus and events.
See you soon!