类似Pycharm的Run功能的悬浮功能框如何用pyside6进行开发
时间: 2024-02-17 20:03:24 浏览: 153
pycharm安装教程.pdf
使用Pyside6实现类似Pycharm的Run功能的悬浮功能框,可以按照以下步骤进行:
1. 导入Pyside6的相关模块,例如:
```python
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit, QWidget
from PySide6.QtGui import QMouseEvent, QCursor
from PySide6.QtCore import Qt
```
2. 创建一个主窗口,用于显示程序界面:
```python
app = QApplication([])
main_window = QMainWindow()
main_window.show()
```
3. 创建一个按钮或菜单项,用于触发悬浮功能框的显示:
```python
button = QPushButton('Run', main_window)
button.clicked.connect(show_floating_window)
```
其中,`show_floating_window`是一个回调函数,用于创建并显示悬浮功能框。
4. 在回调函数中,创建一个新的QWidget窗口作为悬浮功能框,添加需要显示的控件,例如文本框、按钮等:
```python
def show_floating_window():
floating_window = QWidget(main_window, Qt.WindowFlags(Qt.FramelessWindowHint))
floating_window.setWindowFlag(Qt.WindowStaysOnTopHint)
floating_window.setFixedSize(300, 200)
text_edit = QTextEdit(floating_window)
text_edit.setFixedSize(280, 150)
text_edit.move(10, 10)
close_button = QPushButton('Close', floating_window)
close_button.clicked.connect(floating_window.close)
close_button.move(100, 170)
floating_window.show()
```
其中,`Qt.WindowFlags(Qt.FramelessWindowHint)`用于去除窗口的边框,`Qt.WindowStaysOnTopHint`用于保持悬浮功能框在最前端显示。
5. 为新的QWidget窗口添加鼠标事件处理函数,用于实现拖动功能:
```python
def mousePressEvent(event: QMouseEvent):
global offset_x, offset_y
offset_x = event.globalX() - floating_window.pos().x()
offset_y = event.globalY() - floating_window.pos().y()
def mouseMoveEvent(event: QMouseEvent):
floating_window.move(event.globalX() - offset_x, event.globalY() - offset_y)
```
其中,`mousePressEvent`用于记录鼠标按下时的相对位置,`mouseMoveEvent`用于计算鼠标移动后的相对位置,实现窗口的移动。
6. 最后,将新的QWidget窗口设置为悬浮窗口,在程序结束前保持运行:
```python
floating_window.setWindowFlag(Qt.WindowType.Window, True)
floating_window.setAttribute(Qt.WA_TranslucentBackground, True)
floating_window.show()
app.exec_()
```
完整的代码示例:
```python
from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit, QWidget
from PySide6.QtGui import QMouseEvent, QCursor
from PySide6.QtCore import Qt
def show_floating_window():
floating_window = QWidget(main_window, Qt.WindowFlags(Qt.FramelessWindowHint))
floating_window.setWindowFlag(Qt.WindowStaysOnTopHint)
floating_window.setFixedSize(300, 200)
text_edit = QTextEdit(floating_window)
text_edit.setFixedSize(280, 150)
text_edit.move(10, 10)
close_button = QPushButton('Close', floating_window)
close_button.clicked.connect(floating_window.close)
close_button.move(100, 170)
floating_window.show()
floating_window.mousePressEvent = mousePressEvent
floating_window.mouseMoveEvent = mouseMoveEvent
floating_window.setWindowFlag(Qt.WindowType.Window, True)
floating_window.setAttribute(Qt.WA_TranslucentBackground, True)
floating_window.show()
app.exec_()
def mousePressEvent(event: QMouseEvent):
global offset_x, offset_y
offset_x = event.globalX() - floating_window.pos().x()
offset_y = event.globalY() - floating_window.pos().y()
def mouseMoveEvent(event: QMouseEvent):
floating_window.move(event.globalX() - offset_x, event.globalY() - offset_y)
app = QApplication([])
main_window = QMainWindow()
main_window.show()
button = QPushButton('Run', main_window)
button.clicked.connect(show_floating_window)
floating_window = None
offset_x, offset_y = 0, 0
app.exec_()
```
注意,在Windows系统下,需要安装Microsoft Visual C++ Redistributable for Visual Studio 2019才能正确运行Pyside6程序。
阅读全文