Как построить рамку вокруг нескольких форм в PyQt5?
Имеется программа с label и формами qlineedit для ввода текста.
Ниже слева представлена фотография программы и справа фотография программы, с подрисованной в Paint'е рамкой (собственно нужно сделать как на правой картинке):
Я знаю как добавить рамку для простого label с текстом, но вот как добавить её для label + qlineedit не знаю... Помогите, пожалуйста. Ниже представляю код программы:
import sys
from PyQt5.QtWidgets import QMainWindow, QWidget, QGridLayout, QApplication
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, \
QRadioButton, QButtonGroup, QMessageBox, QApplication, QLineEdit, QFormLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QIcon, QPixmap
WINDOW_HEIGHT = 450
WINDOW_WIDTH = 300
WINDOW_NAME = 'КВУР'
WINDOW_ICON_NAME = 'Images\\main_logo.png'
MAIN_FONT = 'Times'
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.heading_label = QLabel()
self.entering_label = QLabel()
self.first_value = QLineEdit()
self.second_value = QLineEdit()
self.third_value = QLineEdit()
self.find_button = QPushButton('Найти')
self.clear_button = QPushButton('Очистить')
def fill_forms(self):
self.heading_label.setText('Решение квадратного уравнения')
self.heading_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE, QFont.Bold))
self.entering_label.setText('Введите коэффициенты:')
self.entering_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
self.first_value.setText('Значение 1')
self.first_value.setFixedSize(265, 20)
self.second_value.setText('Значение 2')
self.second_value.setFixedSize(265, 20)
self.third_value.setText('Значение 3')
self.third_value.setFixedSize(265, 20)
def create_layers(self):
main_layout = QVBoxLayout(self)
heading_label = QHBoxLayout()
heading_label.addWidget(self.heading_label)
main_layout.addLayout(heading_label)
entering_label = QHBoxLayout()
entering_label.addWidget(self.entering_label)
main_layout.addLayout(entering_label)
first_qline_edit = QHBoxLayout()
first_qline_edit.addWidget(self.first_value)
main_layout.addLayout(first_qline_edit)
second_qline_edit = QHBoxLayout()
second_qline_edit.addWidget(self.second_value)
main_layout.addLayout(second_qline_edit)
third_qline_edit = QHBoxLayout()
third_qline_edit.addWidget(self.third_value)
main_layout.addLayout(third_qline_edit)
line = QFormLayout()
line.addRow(self.find_button, self.clear_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(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Как вариант:
Виджет QGroupBox предоставляет рамку группового окна с заголовком.
Больше https://doc.qt.io/qt-5/qgroupbox.html#details
import sys
from PyQt5.QtWidgets import QMainWindow, QWidget, QGridLayout, QGroupBox
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, \
QRadioButton, QButtonGroup, QMessageBox, QApplication, QLineEdit, QFormLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QIcon, QPixmap
WINDOW_HEIGHT = 450
WINDOW_WIDTH = 350
WINDOW_NAME = 'КВУР'
WINDOW_ICON_NAME = 'lena-2.png' # 'Images\\main_logo.png'
MAIN_FONT = 'Times'
FONT_HEADER_SIZE = 13
FONT_BODY_SIZE = 8
class FormBuilder(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.create_all_forms()
self.fill_forms()
self.create_layers()
def create_all_forms(self):
self.heading_label = QLabel('Решение квадратного уравнения')
self.entering_label = QLabel('Введите коэффициенты:')
self.first_value = QLineEdit(placeholderText='Значение a')
self.second_value = QLineEdit(placeholderText='Значение b')
self.third_value = QLineEdit(placeholderText='Значение c')
self.find_button = QPushButton('Найти')
self.clear_button = QPushButton('Очистить')
def fill_forms(self):
self.heading_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE, QFont.Bold))
self.heading_label.setStyleSheet('border: 3px solid green; border-radius: 5px;')
self.entering_label.setFont(QFont(MAIN_FONT, FONT_HEADER_SIZE))
def create_layers(self):
main_layout = QVBoxLayout(self)
main_layout.addWidget(self.heading_label)
self.groupBox = QGroupBox(self.entering_label.text())
self.groupBox.setStyleSheet('''
QGroupBox {
margin-top: 2ex;
}
QGroupBox:enabled {
border: 3px solid green;
border-radius: 5px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 3ex;
}
''')
groupBoxLayout = QVBoxLayout(self.groupBox)
groupBoxLayout.addWidget(self.first_value)
groupBoxLayout.addWidget(self.second_value)
groupBoxLayout.addWidget(self.third_value)
main_layout.addWidget(self.groupBox)
line = QHBoxLayout()
line.addWidget(self.find_button)
line.addWidget(self.clear_button)
main_layout.addLayout(line)
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(WINDOW_WIDTH, WINDOW_HEIGHT)
self.setWindowTitle(WINDOW_NAME)
self.setWindowIcon(QIcon(WINDOW_ICON_NAME))
def create_and_connect_grid(self):
self.main_grid = QGridLayout(self.centralWidget)
form_builder_object = FormBuilder()
self.main_grid.addWidget(form_builder_object, 0, 0, Qt.AlignTop)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())


