简体   繁体   English

如何添加QVBoxLayout。 Qlabel和Qpushbutton周围? pyqt5

[英]How to add QVBoxLayout. around Qlabel and Qpushbutton ? pyqt5

How to add QVBoxLayout. 如何添加QVBoxLayout。 around Qlabel and Qpushbutton? Qlabel和Qpushbutton周围? I have this how can I add QVBoxLayout to make something like that 我有这个如何添加QVBoxLayout来制作类似的东西

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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