怎么在pyqt5做的gui界面中加入漂浮字体,用python编写
时间: 2024-09-13 19:13:40 浏览: 48
在PyQt5中加入漂浮字体,通常指的是在GUI界面上添加文本标签,并使其在窗口上自由漂浮,可以通过创建`QLabel`小部件并设置其样式来实现。下面是一个简单的示例代码,展示如何在PyQt5创建的GUI界面中添加并设置漂浮字体:
```python
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个QLabel实例
self.label = QLabel('漂浮字体示例', self)
# 设置字体样式和大小
self.label.setFont(QFont('Arial', 16))
# 设置标签的对齐方式
self.label.setAlignment(Qt.AlignCenter)
# 设置标签的背景色和字体颜色
self.label.setStyleSheet("background-color: rgba(255, 255, 255, 128); color: black;")
# 设置标签的移动性
self.label.setMouseTracking(True)
self.label.installEventFilter(self)
def eventFilter(self, source, event):
# 当鼠标按下时,记录标签的位置
if event.type() == event.MouseButtonPress:
self.oldPos = event.globalPos() - self.label.rect().topLeft()
# 当鼠标拖动时,移动标签
elif event.type() == event.MouseMove:
self.label.move(event.globalPos() - self.oldPos)
return True
return super().eventFilter(source, event)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
在这个示例中,我们创建了一个`QLabel`对象,并设置了字体样式、大小、对齐方式以及背景色和字体颜色。我们还使用了`eventFilter`方法来处理鼠标事件,使得`QLabel`可以通过鼠标拖动来改变位置,从而达到漂浮的效果。
阅读全文
相关推荐
















