怎么美化setWindowTitle
时间: 2023-11-27 15:50:35 浏览: 39
您可以使用以下代码来设置窗口标题的样式:
```python
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MyMainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题的样式
font = QFont('Roboto', 16) # 设置字体和大小
self.setWindowTitle('My App') # 设置标题
self.setWindowIcon(QIcon('icon.png')) # 设置图标
self.setStyleSheet('background-color: white;') # 设置背景色
self.statusBar().showMessage('Ready') # 设置状态栏
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyMainWindow()
window.show()
sys.exit(app.exec_())
```
您可以根据需要更改字体、大小、背景色和状态栏信息。
相关问题
pyside2 窗口标题美化
可以通过修改窗口的样式表来美化窗口标题。以下是一个简单的示例:
```python
from PySide2.QtWidgets import QApplication, QWidget
from PySide2.QtCore import Qt
app = QApplication([])
win = QWidget()
win.setWindowTitle("My App")
# 设置样式表
style = """
QWidget {{
background-color: #333;
color: white;
}}
QHeaderView::section {{
background-color: #555;
color: white;
}}
QLabel {{
color: white;
}}
"""
win.setStyleSheet(style)
win.show()
app.exec_()
```
在上面的代码中,我们使用了样式表来设置窗口和其它控件的颜色和样式。你可以根据需要自行修改样式表的内容来美化窗口标题。
QMessageBox 样式美化
可以通过修改样式表来美化 QMessageBox 的外观。下面是一个简单的例子:
```python
from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
app = QApplication([])
app.setStyle('Fusion')
# 设置QMessageBox的样式
app.setStyleSheet("""
QMessageBox {
background-color: #F0F0F0;
border: 2px solid darkgray;
border-radius: 10px;
}
QMessageBox QLabel {
color: darkgray;
}
QMessageBox QPushButton {
background-color: #F0F0F0;
border: 2px solid darkgray;
border-radius: 10px;
padding: 5px;
min-width: 80px;
}
QMessageBox QPushButton:hover {
background-color: gray;
color: white;
}
""")
# 创建QMessageBox
msg_box = QMessageBox()
msg_box.setWindowTitle('提示')
msg_box.setWindowIcon(QIcon('icon.png'))
msg_box.setText('这是一条提示信息')
msg_box.setIcon(QMessageBox.Information)
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
msg_box.setDefaultButton(QMessageBox.Ok)
# 设置按钮的对齐方式
msg_box.button(QMessageBox.Ok).setTextAlignment(Qt.AlignCenter)
msg_box.button(QMessageBox.Cancel).setTextAlignment(Qt.AlignCenter)
# 显示QMessageBox
ret = msg_box.exec_()
if ret == QMessageBox.Ok:
print('你点击了确定按钮')
else:
print('你点击了取消按钮')
```
在上面的例子中,我们使用了 Qt 的样式表来美化 QMessageBox 的外观。我们设置了背景颜色、边框、边框圆角和按钮的样式。此外,我们还设置了按钮的对齐方式,使其在消息框中居中显示。
运行上面的代码,你会看到一个漂亮的消息框。你可以根据自己的需要进一步修改样式表,以达到你想要的效果。