[英]How to add QVBoxLayout. around Qlabel and Qpushbutton ? 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())
試試吧:
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.