I'm trying to set up a GUI which will include multiple pages in PyQt5. The window will have a minimum size of 800x600, but is customisable beyond that. I want most, but not all, elements on the window to scale along with it. I have a solution already, but I feel that it is not very elegant.
Here is an example of the window at 800x600:
And here's another example after scaling
I've tried using QVBoxLayout, but with that system, I can't manage to keep the layout as it is here (not to say that it's impossible), rather the widgets all become centred and of the same width. At a later date, I might also be looking at adding widgets to the side that will be at the same y-value as some of the existing widgets, which is another thing that I'm not sure on how to do with the box layout.
Here's the relevant code:
class CreatePage(QWidget):
def __init__(self, parent=None):
super().__init__()
self.initUI()
def initUI(self):
self.homeBtn = QPushButton("Home", self)
self.homeBtn.move(10, 10)
self.frontLabel = QLabel("Front", self)
self.frontLabel.setFont(QFont("Decorative", 20))
self.frontEdit = QTextEdit(self)
self.frontEdit.setFont(QFont("Decorative", 11))
self.backLabel = QLabel("Back", self)
self.backLabel.setFont(QFont("Decorative", 20))
self.backEdit = QTextEdit(self)
self.backEdit.setFont(QFont("Decorative", 11))
def paintEvent(self, e):
qp = QPainter()
qp.setFont(QFont("Decorative", 20))
size = self.size()
h = size.height()
w = size.width()
frontW = qp.fontMetrics().width("Front")
self.frontLabel.move((w/2) - (frontW/2) , h/15)
#I use fontMetrics to determine the width of the text
#I then use this information to centre the text
self.frontEdit.move(50, h/15 + 40)
self.frontEdit.resize(w-100, h/3)
backW = qp.fontMetrics().width("Back")
self.backLabel.move((w/2) - (backW/2), h/2)
self.backEdit.move(50, h/2 + 40)
self.backEdit.resize(w-100, h/3)
Apologies for any general sloppiness, I am new to PyQt and to GUI programming on the whole. Anyway, the formulas I've used for resizing and moving widgets are quite arbitrary. Does anyone know a better way of achieving this effect?
Try it:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class CreatePage(QWidget):
def __init__(self, parent=None):
super().__init__()
self.initUI()
def initUI(self):
self.homeBtn = QPushButton("Home")
self.frontLabel = QLabel("Front")
self.frontLabel.setFont(QFont("Decorative", 20))
self.frontEdit = QTextEdit(placeholderText="frontEdit")
self.frontEdit.setFont(QFont("Decorative", 11))
self.backLabel = QLabel("Back")
self.backLabel.setFont(QFont("Decorative", 20))
self.backEdit = QTextEdit(placeholderText="backEdit")
self.backEdit.setFont(QFont("Decorative", 11))
grid = QGridLayout()
grid.addWidget(self.homeBtn, 0, 0, alignment=Qt.AlignTop | Qt.AlignLeft)
grid.addWidget(self.frontLabel, 1, 0, alignment=Qt.AlignCenter)
grid.addWidget(self.frontEdit, 2, 0)
grid.addWidget(self.backLabel, 3, 0, alignment=Qt.AlignCenter)
grid.addWidget(self.backEdit, 4, 0)
self.setLayout(grid)
if __name__=="__main__":
app = QApplication(sys.argv)
myapp = CreatePage()
myapp.show()
sys.exit(app.exec_())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.