如何添加QVBoxLayout。在Qlabel和Qpushbutton附近? pyqt5

时间:2018-03-27 16:59:16

标签: python python-3.x pyqt pyqt5

如何添加QVBoxLayout。在Qlabel和Qpushbutton附近? 我有这个如何添加QVBoxLayout来制作类似的东西

我有这段代码:

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())

这里的图片解释了我的意思: what I mean

1 个答案:

答案 0 :(得分:0)

试一试:

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())

enter image description here