python logo

qwidget set position


Python hosting: Host, run, and code Python in the cloud!

PyQt5 offers multiple layout strategies including grid layouts, horizontal layouts, and absolute positioning. The choice of layout should be influenced by your application’s requirements and your personal preference.

Recommended course:
Develop GUI Apps using PyQt5

PyQt5 Absolute Positioning

Absolute positioning in PyQt5 provides full control over widget placement, requiring you to define the position of each widget explicitly.

Widgets can be positioned absolutely with the move(x,y) method.

PyQt5 Absolute Positioning

All PyQt5 widgets inherit from qwidget.

PyQt5 Widget Positioning Example

The example below demonstrates positioning widgets at specified coordinates using the move() method. The widgets are then added to a PyQt5 window (QMainWindow) with certain properties set within the initUI() method.

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
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
from PyQt5.QtGui import QIcon

class App(QMainWindow):

def __init__(self):
super().__init__()
self.title = 'PyQt absolute positioning - pythonspot.com'
self.left = 10
self.top = 10
self.width = 440
self.height = 280
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

label = QLabel('Python', self)
label.move(50,50)

label2 = QLabel('PyQt5', self)
label2.move(100,100)

label3 = QLabel('Examples', self)
label3.move(150,150)

label4 = QLabel('pythonspot.com', self)
label4.move(200,200)

self.show()

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Access More Tutorials:
Download PyQt5 Examples

Previous Topic | Next Topic






Leave a Reply: