In this part we are going to talk about some basics functionalities in PyQt5, we go through installation process, creating QPushButton, Signals and working with Qt Designer.
In this Python PyQt5 lesson we want to learn about Python PyQt5 QtQuick & QML, QtQuick is a feature of PyQt5 by using that you can separate your main design from logic. and QtQuick has its own language that is called QML. QML is Qt Markup Language, it is a language similar to JavaScript but it is not JavaScript. So when you want to work with QtQuick you need to create two files in Pycharm IDE, the first one is a QML file and the second one is a Python file.
Create a file with the .qml extension, i want to call it main.qml and add this code.
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import QtQuick.Window2.2
import QtQuick2.3
Window{
visible:true
width:700
height:400
color:"black"
title:"Geekscoders.com"
Text{
id:mytext
text:"Geekscoders.com - PyQt5 QML Lesson"
color:"white"
font.pixelSize:35
font.bold:true
anchors.centerIn:parent
}
Image{
id:image
source:'python.png'
}
TextInput{
id:textinput
text:"Hello"
color:"white"
font.pixelSize:16;font.bold:true
x:200
y:50
focus:true
maximumLength:12
font.capitalization:Font.AllUppercase
}
}
So in above code first we have our root element that is our window and in the window you can add your other widgets like Text, Image, InputField and etc. also we have some attributes for the window like width and height, window title and color of the window.
Now it is time to load our qml file in python language.
main.py
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fromPyQt5.QtQml importQQmlApplicationEngine
fromPyQt5.QtWidgets importQApplication
importsys
fromPyQt5.QtGui importQIcon
defrun_qml():
app=QApplication(sys.argv)
engine=QQmlApplicationEngine()
app.setWindowIcon(QIcon('python.png'))
engine.load('main.qml')
ifnotengine.rootObjects():
return-1
returnapp.exec_()
if__name__=="__main__":
sys.exit(run_qml())
At the top these are the imports.
1
2
3
4
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtWidgets import QApplication
import sys
from PyQt5.QtGui import QIcon
You need to create the object for QApplication and QQmlApplicationEngine.
1
2
app=QApplication(sys.argv)
engine=QQmlApplicationEngine()
In here we have set the icon for the window, make sure that you have added an icon to your working directory.
1
app.setWindowIcon(QIcon('python.png'))
This is for creating text, it is just a basic text with the color and font size.
1
2
3
4
5
6
7
8
9
10
Text{
id:mytext
text:"Geekscoders.com - PyQt5 QML Lesson"
color:"white"
font.pixelSize:35
font.bold:true
anchors.centerIn:parent
}
Using this code you can add image in the window.
1
2
3
4
5
Image{
id:image
source:'python.png'
}
And this code is for adding TextInput
1
2
3
4
5
6
7
8
9
10
11
12
13
14
TextInput{
id:textinput
text:"Hello"
color:"white"
font.pixelSize:16;font.bold:true
x:200
y:50
focus:true
maximumLength:12
font.capitalization:Font.AllUppercase
}
Run the complete code and this is the result.
PyQt5 QML – Working with QtQuick & QML in PyQt5
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.