pyqt5 控件字体居中
时间: 2023-08-20 20:04:47 浏览: 103
您可以使用QLabel控件来显示文本,并使用setAlignment()方法将文本居中。然,您可以使用setFont()方法设置控件的字体。以下是一个示例代码:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
from PyQt5.QtGui import QFont
import sys
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
label = QLabel("Hello PyQt5")
label.setAlignment(Qt.AlignCenter) # 设置文本居中
font = QFont("Arial", 12) # 设置字体
label.setFont(font)
layout.addWidget(label)
self.setLayout(layout)
self.setWindowTitle("Centered Text")
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
```
在这个示例中,我们使用QLabel控件来显示文本,并将文本居中对齐。然后,我们使用setFont()方法设置字体为Arial,并设置字体大小为12。最后,我们将QLabel添加到一个垂直布局中,并将其设置为窗口的主布局。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)