Last time continued
Layout management I will summarize this site roughly in Japanese.
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_())
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_())
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_())
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_())
Next time will give you a rough idea of Events and signals.
Recommended Posts