简体   繁体   中英

How to add QVBoxLayout. around Qlabel and Qpushbutton ? pyqt5

How to add QVBoxLayout. around Qlabel and Qpushbutton? I have this how can I add QVBoxLayout to make something like that

I have this code :

from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication , QMainWindow , QPushButton , 
QToolTip , QLabel
import sys

class Window (QMainWindow):
  def __init__(self):
    super().__init__()

    self.title = "pyQt5"
    self.top = 100
    self.left = 100
    self.width = 680
    self.height= 500

    button = QPushButton("print", self)
    button.move(200,200)

    lb = QLabel('Hi', self)
    lb.move(200,100)

    self.s()

  def s(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.top,self.left,self.width,self.height)
    self.show()

if __name__ == '__main__':
   App = QApplication(sys.argv)
   window = Window()
   sys.exit(App.exec())

here image explains what I mean: 我的意思是

Try it:

import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication , QMainWindow , QPushButton , 
                            QToolTip , QLabel, QVBoxLayout, QWidget)
from PyQt5.QtCore    import Qt

class Window (QMainWindow):
  def __init__(self):
    super().__init__()

    self.title = "pyQt5"
    self.top = 100
    self.left = 100
    self.width = 680
    self.height= 500

    self.main_widget = QWidget()
    self.setCentralWidget(self.main_widget)

    layout = QVBoxLayout(self.main_widget)

    button = QPushButton("print", self)
    button.setStyleSheet('background-color:blue; color:white; font-size:24px;')

    lb = QLabel('Hello', self)
    lb.setStyleSheet('background-color:green; color:white; font-size:24px;')

    layout.addStretch(1)
    layout.addWidget(lb)
    layout.addStretch(1)
    layout.addWidget(button)
    layout.addStretch(1)
    layout.setAlignment(Qt.AlignCenter)

    self.s()

  def s(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.top,self.left,self.width,self.height)
    self.show()

if __name__ == '__main__':
   App = QApplication(sys.argv)
   window = Window()
   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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM