How to remove the distance between the appearances in QVBoxLayout PyQt5
-
I'm trying to remove the distance in the container between the viewers, but setSpacing(0) doesn't help. How can I put the buttons in the container just like the picture?
from PyQt5.Qt import * import sys
class Window(QWidget):
def init(self, parent=None):
QWidget.init(self, parent)self.setStyleSheet("background-color: rgb(35,35,35)") self.setMinimumSize(1000,500) self.top_bar = QFrame() self.top_bar.setStyleSheet("background-color: rgb(35,35,35);") self.top_bar.setFrameShadow(QFrame.Raised) self.top_bar.setFixedSize(920,30) self.content = QFrame() self.content.setStyleSheet("background-color: rgb(45,45,45);") self.content.setFrameShadow(QFrame.Raised) self.left_bar = QFrame() self.left_bar.setStyleSheet("background-color: rgb(35,35,35);") self.left_bar.setFrameShadow(QFrame.Raised) self.left_bar.setFixedSize(80,500) self.menu_button = QPushButton() self.menu_button.setStyleSheet("background-color: rgb(110, 192, 255);\ border: 0px solid; font: 14pt; color: rgb(35,35,35);") self.menu_button.setText("Menu") self.page_1_button = QPushButton() self.page_1_button.setStyleSheet("QPushButton:hover { background-color: rgb(85,170,255) }\ QPushButton:!hover { background-color: transparent; border: 0px solid; font: 11pt; color: rgb(255,255,255) }") self.page_1_button.setText("Page 1") self.page_2_button = QPushButton() self.page_2_button.setStyleSheet("QPushButton:hover { background-color: rgb(85,170,255) }\ QPushButton:!hover { background-color: transparent; border: 0px solid; font: 11pt; color: rgb(255,255,255) }") self.page_2_button.setText("Page 2") self.vbox_1 = QVBoxLayout() self.vbox_1.addWidget(self.menu_button) self.vbox_1.addWidget(self.page_1_button) self.vbox_1.addWidget(self.page_2_button) self.left_bar.setLayout(self.vbox_1) self.vbox_2 = QVBoxLayout() self.vbox_2.addWidget(self.top_bar) self.vbox_2.addWidget(self.content) self.hbox = QHBoxLayout() self.hbox.addWidget(self.left_bar) self.hbox.addLayout(self.vbox_2) self.setLayout(self.hbox)
if name == "main":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
-
I already figured it out, so I'll leave it for descendants: by method addStretch(int) possible to create an empty space in a container♪ But it's very important that method. Sign in the right place.: If used before adding the items, there will be one result, and then another.
from PyQt5.Qt import * import sys
class Window(QWidget):
def init(self, parent=None):
QWidget.init(self, parent)self.setStyleSheet("background-color: rgb(35,35,35)") self.setMinimumSize(1000,500) self.top_bar = QFrame() self.top_bar.setStyleSheet("background-color: rgb(35,35,35);") self.top_bar.setFrameShadow(QFrame.Raised) self.top_bar.setFixedSize(920,50) self.content = QFrame() self.content.setStyleSheet("background-color: rgb(45,45,45);") self.content.setFrameShadow(QFrame.Raised) self.left_bar = QFrame() self.left_bar.setStyleSheet("background-color: rgb(35,35,35);") self.left_bar.setFrameShadow(QFrame.Raised) self.left_bar.setFixedSize(80,500) self.menu_button = QPushButton() self.menu_button.setStyleSheet("background-color: rgb(110, 192, 255);\ border: 0px solid; font: 14pt; color: rgb(35,35,35)") self.menu_button.setText("Menu") self.menu_button.setMinimumSize(80,50) self.page_1_button = QPushButton() self.page_1_button.setStyleSheet("QPushButton:hover { background-color: rgb(85,170,255) }\ QPushButton:!hover { background-color: transparent; border: 0px solid; font: 11pt; color: rgb(255,255,255) }") self.page_1_button.setText("Page 1") self.page_1_button.setMinimumSize(80, 50) self.page_2_button = QPushButton() self.page_2_button.setStyleSheet("QPushButton:hover { background-color: rgb(85,170,255) }\ QPushButton:!hover { background-color: transparent; border: 0px solid; font: 11pt; color: rgb(255,255,255) }") self.page_2_button.setText("Page 2") self.page_2_button.setMinimumSize(80, 50) self.vbox_1 = QVBoxLayout() self.vbox_1.setSpacing(0) self.vbox_1.setContentsMargins(0,0,0,0) self.vbox_1.addWidget(self.menu_button) self.vbox_1.addWidget(self.page_1_button) self.vbox_1.addWidget(self.page_2_button) self.vbox_1.addStretch(0) #Вот этот красавец! self.left_bar.setLayout(self.vbox_1) self.vbox_2 = QVBoxLayout() self.vbox_2.setSpacing(0) self.vbox_2.setContentsMargins(0, 0, 0, 0) self.vbox_2.addWidget(self.top_bar) self.vbox_2.addWidget(self.content) self.hbox = QHBoxLayout() self.hbox.setSpacing(0) self.hbox.setContentsMargins(0, 0, 0, 0) self.hbox.addWidget(self.left_bar) self.hbox.addLayout(self.vbox_2) self.setLayout(self.hbox)
if name == "main":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())