Как сократить повторящиюйся код с PyQt5?
У меня в программе имеется функция, которая создает шесть зон и добавляет в каждую зону по одному виджету, но вот кода у меня много и он повторяющийся.
Можете, пожалуйста, помочь с исправлением? Пробовал работать с eval и циклом, но там ошибка возникает. Вот собственно код Python(это всё в функции):
import sys
from Plates.FirstProgram.FormBuilder import *
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, \
QGridLayout, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QLineEdit
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5.QtGui import QFont, QIcon, QPixmap
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_all_forms()
self.create_layers()
def create_all_forms(self):
self.image_area = QLabel(self)
self.general_image = QPixmap('Images/settings_image.png')
self.image_area.setPixmap(self.general_image)
self.text_width_size = QLabel(self)
self.text_width_size.setText('Enter width of array.')
self.text_height_size = QLabel(self)
self.text_height_size.setText('Enter height of array.')
self.textbox_for_width = QLineEdit(self)
self.textbox_for_height = QLineEdit(self)
self.create_button = QPushButton("Create")
def create_layers(self):
layout = QVBoxLayout(self)
zero_image_layer = QHBoxLayout() # вот здесь начало
zero_image_layer.addWidget(self.image_area)
layout.addLayout(zero_image_layer)
first_layer = QHBoxLayout()
first_layer.addWidget(self.text_width_size)
layout.addLayout(first_layer)
second_layer = QHBoxLayout()
second_layer.addWidget(self.textbox_for_width)
layout.addLayout(second_layer)
third_layer = QHBoxLayout()
third_layer.addWidget(self.text_height_size)
layout.addLayout(third_layer)
forth_layer = QHBoxLayout()
forth_layer.addWidget(self.textbox_for_height)
layout.addLayout(forth_layer)
fifth_layer = QHBoxLayout()
fifth_layer.addWidget(self.create_button)
layout.addLayout(fifth_layer) # и здесь конец
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
self.setFixedSize(250, 250)
self.setWindowTitle('Plates')
def create_and_connect_grid(self):
self.grid = QGridLayout(self.centralWidget)
self.formBuilder = FormBuilder()
self.grid.addWidget(self.formBuilder, 0, 0, Qt.AlignBottom)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
я не понял о каких eval и циклах вы говорите. А заменить код в функции на более короткий код можно так:
import sys
#? from Plates.FirstProgram.FormBuilder import *
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, \
QGridLayout, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, \
QLineEdit, QFormLayout
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5.QtGui import QFont, QIcon, QPixmap
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_all_forms()
self.create_layers()
def create_all_forms(self):
self.image_area = QLabel(self, alignment=Qt.AlignCenter)
self.general_image = QPixmap('lena-2.png').scaled( # Images/settings_image.png
250, 250, Qt.KeepAspectRatio, Qt.FastTransformation
)
self.image_area.setPixmap(self.general_image)
self.text_width_size = QLabel(self)
self.text_width_size.setText('Введите ширину массива.')
self.text_height_size = QLabel(self)
self.text_height_size.setText('Введите высоту массива.')
self.textbox_for_width = QLineEdit()
self.textbox_for_height = QLineEdit()
self.create_button = QPushButton("Создать")
def create_layers(self): # !!!
layout = QFormLayout(self)
layout.addRow(self.image_area)
layout.addRow(self.text_width_size, self.textbox_for_width)
layout.addRow(self.text_height_size, self.textbox_for_height)
layout.addRow(self.create_button)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
self.build_skeleton_with_basic_settings()
self.create_and_connect_grid()
def build_skeleton_with_basic_settings(self):
#? self.setFixedSize(250, 250)
self.setWindowTitle('Plates')
def create_and_connect_grid(self):
self.grid = QGridLayout(self.centralWidget)
self.formBuilder = FormBuilder()
self.grid.addWidget(self.formBuilder, 0, 0, Qt.AlignBottom)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
