代码让qpushbutton的文字在竖直方向居中
要让QPushButton的文字在竖直方向居中,可以使用QSS(Qt Style Sheets)来设置QPushButton的样式。
可以使用以下代码将QPushButton的文字在竖直方向居中:
from PyQt5.QtWidgets import QApplication, QPushButton
from PyQt5.QtCore import Qt
app = QApplication([])
button = QPushButton("Button")
button.setStyleSheet("QPushButton { text-align: center; }")
button.setAlignment(Qt.AlignVCenter)
button.show()
app.exec_()
在这个例子中,我们创建了一个QPushButton,并使用QSS设置了按钮的样式。text-align: center;将按钮的文字居中对齐。然后使用setAlignment(Qt.AlignVCenter)将按钮的文字在竖直方向上居中对齐。
运行这段代码,你将会看到一个有居中文字的按钮
原文地址: https://www.cveoy.top/t/topic/iRIe 著作权归作者所有。请勿转载和采集!