[GUI with Python] PyQt5-Layout management-

Last time continued

Layout management I will summarize this site roughly in Japanese.

[Absolute position]

Absolute_positioning.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        #Label name setting
        lbl1 = QLabel('Zetcode', self)
        #Label x=15,y=Move to 10
        lbl1.move(15, 10)

        lbl2 = QLabel('tutorials', self)
        lbl2.move(35, 40)
        
        lbl3 = QLabel('for programmers', self)
        lbl3.move(55, 70)        
        
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Absolute')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
absolute.png

[Box layout]

Box_layout.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-


import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, 
    QHBoxLayout, QVBoxLayout, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        #Create OK and Cancel buttons
        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")
        
        #Create a horizontal box
        hbox = QHBoxLayout()
        #Keep the size of the button unchanged
        #The button moves to the right because there is a horizontally stretchable space on the left side of the button.
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)
        
        #Create a vertical box
        vbox = QVBoxLayout()
        #Create a space that stretches vertically
        vbox.addStretch(1)
        #The button moves to the lower right
        vbox.addLayout(hbox)
        
        #Add the layout set above to the screen
        self.setLayout(vbox)    
        
        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
box_layout.png

[Button arrangement in a grid pattern]

QGridLayout.py


#!/usr/bin/python3
# -*- coding: utf-8 -*-


import sys
from PyQt5.QtWidgets import (QWidget, QGridLayout, 
    QPushButton, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        #Creating an object for the application screen
        grid = QGridLayout()
        self.setLayout(grid)
        
        #Button label
        names = ['Cls', 'Bck', '', 'Close',
                 '7', '8', '9', '/',
                '4', '5', '6', '*',
                 '1', '2', '3', '-',
                '0', '.', '=', '+']
        
        #Button position setting
        positions = [(i,j) for i in range(5) for j in range(4)]
        
        #Add button to screen
        for position, name in zip(positions, names):
            
            #Skip elements without button labels
            if name == '':
                continue
            #Set button label
            button = QPushButton(name)
            # *Place the button at the position of position
            grid.addWidget(button, *position)
            
        self.move(300, 150)
        self.setWindowTitle('Calculator')
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
qgrid.png

[Text box layout]

Review_example


#!/usr/bin/python3
# -*- coding: utf-8 -*-


import sys
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit, 
    QTextEdit, QGridLayout, QApplication)


class Example(QWidget):
    
    def __init__(self):
        super().__init__()
        
        self.initUI()
        
        
    def initUI(self):
        
        title = QLabel('Title')
        author = QLabel('Author')
        review = QLabel('Review')

        titleEdit = QLineEdit()
        authorEdit = QLineEdit()
        reviewEdit = QTextEdit()
        
        #Create a grid layout to free up space for each widget
        grid = QGridLayout()
        grid.setSpacing(10)
        
        #Label positioning
        grid.addWidget(title, 1, 0)
        #Position setting of input field
        grid.addWidget(titleEdit, 1, 1)

        grid.addWidget(author, 2, 0)
        grid.addWidget(authorEdit, 2, 1)

        grid.addWidget(review, 3, 0)
        grid.addWidget(reviewEdit, 3, 1, 5, 1)
        
        self.setLayout(grid) 
        
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')    
        self.show()
        
        
if __name__ == '__main__':
    
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
review.png

Next time will give you a rough idea of Events and signals.

Recommended Posts

[GUI with Python] PyQt5-Layout management-
YouTube video management with Python 3
Password management with python: keyring
[GUI with Python] PyQt5 -Preparation-
[GUI with Python] PyQt5 -Paint-
[GUI with Python] PyQt5 -Widget II-
[GUI with Python] PyQt5-The first step-
[GUI with Python] PyQt5-Drag and drop-
[GUI with Python] PyQt5 -Custom Widget-
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Python installation and package management with pip
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel table creation with Python [Progress management table]
Happy GUI construction with electron and python
Excel with Python
Microcomputer with Python
Cast with python
I made a GUI application with Python + PyQt5
GUI image cropping tool made with Python + Tkinter
GUI automation with Python x Windows App Driver
Simple sales tool creation with Python GUI: Quote creation
Open a file dialog with a python GUI (tkinter.filedialog)
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
1.1 Getting Started with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
Voice analysis with python
Think yaml with python