重启窗口Widget
This commit is contained in:
parent
3f70190500
commit
9169cb89cb
8 changed files with 328 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
eclipse.preferences.version=1
|
||||
encoding//PyQtChart\u7EC3\u4E60/ChartView/ChartView.py=utf-8
|
||||
encoding//PyQtChart\u7EC3\u4E60/ChartView/ChatWidget.py=utf-8
|
||||
encoding//PyQtChart\u7EC3\u4E60/charts/bar/BarStack.py=utf-8
|
||||
encoding//PyQtChart\u7EC3\u4E60/charts/line/LineStack.py=utf-8
|
||||
encoding//PyQtChart\u7EC3\u4E60/test/LineChart.py=utf-8
|
||||
encoding//PyQtChart\u7EC3\u4E60/test/LineChart\u81EA\u5B9A\u4E49xy\u8F74.py=utf-8
|
||||
|
@ -19,6 +20,7 @@ encoding//\u68A6\u5E7B\u6811/DreamTree.py=utf-8
|
|||
encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebEngineView.py=utf-8
|
||||
encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebView.py=utf-8
|
||||
encoding//\u7A0B\u5E8F\u91CD\u542F/AutoRestart.py=utf-8
|
||||
encoding//\u7A97\u53E3\u91CD\u542F/RestartMainWindow.py=utf-8
|
||||
encoding//\u81EA\u52A8\u66F4\u65B0/mylibs/testlibs.py=utf-8
|
||||
encoding//\u81EA\u52A8\u66F4\u65B0/test.py=utf-8
|
||||
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/CustomPaintWidget.py=utf-8
|
||||
|
|
254
PyQtChart练习/charts/bar/BarStack.py
Normal file
254
PyQtChart练习/charts/bar/BarStack.py
Normal file
|
@ -0,0 +1,254 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Created on 2017年12月28日
|
||||
@author: Irony."[讽刺]
|
||||
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: charts.bar.BarStack
|
||||
@description: like http://echarts.baidu.com/demo.html#bar-stack
|
||||
'''
|
||||
|
||||
import sys
|
||||
|
||||
from PyQt5.QtChart import QChartView, QChart, QLineSeries, QLegend,\
|
||||
QCategoryAxis, QBarSeries, QBarSet, QBarCategoryAxis
|
||||
from PyQt5.QtCore import Qt, QPointF, QRectF, QPoint
|
||||
from PyQt5.QtGui import QPainter, QPen
|
||||
from PyQt5.QtWidgets import QApplication, QGraphicsLineItem, QWidget, \
|
||||
QHBoxLayout, QLabel, QVBoxLayout, QGraphicsProxyWidget
|
||||
from random import randint
|
||||
|
||||
|
||||
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
|
||||
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
|
||||
__Version__ = "Version 1.0"
|
||||
|
||||
|
||||
class ToolTipItem(QWidget):
|
||||
|
||||
def __init__(self, color, text, parent=None):
|
||||
super(ToolTipItem, self).__init__(parent)
|
||||
layout = QHBoxLayout(self)
|
||||
layout.setContentsMargins(0, 0, 0, 0)
|
||||
clabel = QLabel(self)
|
||||
clabel.setMinimumSize(12, 12)
|
||||
clabel.setMaximumSize(12, 12)
|
||||
clabel.setStyleSheet("border-radius:6px;background: rgba(%s,%s,%s,%s);" % (
|
||||
color.red(), color.green(), color.blue(), color.alpha()))
|
||||
layout.addWidget(clabel)
|
||||
self.textLabel = QLabel(text, self, styleSheet="color:white;")
|
||||
layout.addWidget(self.textLabel)
|
||||
|
||||
def setText(self, text):
|
||||
self.textLabel.setText(text)
|
||||
|
||||
|
||||
class ToolTipWidget(QWidget):
|
||||
|
||||
Cache = {}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ToolTipWidget, self).__init__(*args, **kwargs)
|
||||
self.setAttribute(Qt.WA_StyledBackground, True)
|
||||
self.setStyleSheet(
|
||||
"ToolTipWidget{background: rgba(50, 50, 50, 100);}")
|
||||
layout = QVBoxLayout(self)
|
||||
self.titleLabel = QLabel(self, styleSheet="color:white;")
|
||||
layout.addWidget(self.titleLabel)
|
||||
|
||||
def updateUi(self, title, points):
|
||||
self.titleLabel.setText(title)
|
||||
for serie, point in points:
|
||||
if serie not in self.Cache:
|
||||
item = ToolTipItem(
|
||||
serie.color(),
|
||||
(serie.name() or "-") + ":" + str(point.y()), self)
|
||||
self.layout().addWidget(item)
|
||||
self.Cache[serie] = item
|
||||
else:
|
||||
self.Cache[serie].setText(
|
||||
(serie.name() or "-") + ":" + str(point.y()))
|
||||
|
||||
|
||||
class GraphicsProxyWidget(QGraphicsProxyWidget):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(GraphicsProxyWidget, self).__init__(*args, **kwargs)
|
||||
self.setZValue(999)
|
||||
self.tipWidget = ToolTipWidget()
|
||||
self.setWidget(self.tipWidget)
|
||||
self.hide()
|
||||
|
||||
def width(self):
|
||||
return self.size().width()
|
||||
|
||||
def height(self):
|
||||
return self.size().height()
|
||||
|
||||
def show(self, title, points, pos):
|
||||
self.setGeometry(QRectF(pos, self.size()))
|
||||
self.tipWidget.updateUi(title, points)
|
||||
super(GraphicsProxyWidget, self).show()
|
||||
|
||||
|
||||
class ChartView(QChartView):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ChartView, self).__init__(*args, **kwargs)
|
||||
self.resize(800, 600)
|
||||
self.setRenderHint(QPainter.Antialiasing) # 抗锯齿
|
||||
self.initChart()
|
||||
|
||||
# 提示widget
|
||||
self.toolTipWidget = GraphicsProxyWidget(self._chart)
|
||||
|
||||
# line
|
||||
self.lineItem = QGraphicsLineItem(self._chart)
|
||||
pen = QPen(Qt.gray)
|
||||
pen.setWidth(1)
|
||||
self.lineItem.setPen(pen)
|
||||
self.lineItem.setZValue(998)
|
||||
self.lineItem.hide()
|
||||
'''
|
||||
# 一些固定计算,减少mouseMoveEvent中的计算量
|
||||
# 获取x和y轴的最小最大值
|
||||
axisX, axisY = self._chart.axisX(), self._chart.axisY()
|
||||
self.min_x, self.max_x = axisX.min(), axisX.max()
|
||||
self.min_y, self.max_y = axisY.min(), axisY.max()
|
||||
# 坐标系中左上角顶点
|
||||
self.point_top = self._chart.mapToPosition(
|
||||
QPointF(self.min_x, self.max_y))
|
||||
# 坐标原点坐标
|
||||
self.point_bottom = self._chart.mapToPosition(
|
||||
QPointF(self.min_x, self.min_y))
|
||||
self.step_x = (self.max_x - self.min_x) / (axisX.tickCount() - 1)
|
||||
|
||||
def mouseMoveEvent(self, event):
|
||||
super(ChartView, self).mouseMoveEvent(event)
|
||||
pos = event.pos()
|
||||
# 把鼠标位置所在点转换为对应的xy值
|
||||
x = self._chart.mapToValue(pos).x()
|
||||
y = self._chart.mapToValue(pos).y()
|
||||
index = round((x - self.min_x) / self.step_x)
|
||||
# 得到在坐标系中的所有series的类型和点
|
||||
points = [(serie, serie.at(index))
|
||||
for serie in self._chart.series() if self.min_x <= x <= self.max_x and self.min_y <= y <= self.max_y]
|
||||
if points:
|
||||
pos_x = self._chart.mapToPosition(
|
||||
QPointF(index * self.step_x + self.min_x, self.min_y))
|
||||
self.lineItem.setLine(pos_x.x(), self.point_top.y(),
|
||||
pos_x.x(), self.point_bottom.y())
|
||||
self.lineItem.show()
|
||||
try:
|
||||
title = self.category[index]
|
||||
except:
|
||||
title = ""
|
||||
t_width = self.toolTipWidget.width()
|
||||
t_height = self.toolTipWidget.height()
|
||||
# 如果鼠标位置离右侧的距离小于tip宽度
|
||||
x = pos.x() - t_width if self.width() - \
|
||||
pos.x() - 20 < t_width else pos.x()
|
||||
# 如果鼠标位置离底部的高度小于tip高度
|
||||
y = pos.y() - t_height if self.height() - \
|
||||
pos.y() - 20 < t_height else pos.y()
|
||||
self.toolTipWidget.show(
|
||||
title, points, QPoint(x, y))
|
||||
else:
|
||||
self.toolTipWidget.hide()
|
||||
self.lineItem.hide()
|
||||
'''
|
||||
|
||||
def handleMarkerClicked(self):
|
||||
marker = self.sender() # 信号发送者
|
||||
if not marker:
|
||||
return
|
||||
visible = not marker.series().isVisible()
|
||||
# # 隐藏或显示series
|
||||
marker.series().setVisible(visible)
|
||||
marker.setVisible(True) # 要保证marker一直显示
|
||||
# 透明度
|
||||
alpha = 1.0 if visible else 0.4
|
||||
# 设置label的透明度
|
||||
brush = marker.labelBrush()
|
||||
color = brush.color()
|
||||
color.setAlphaF(alpha)
|
||||
brush.setColor(color)
|
||||
marker.setLabelBrush(brush)
|
||||
# 设置marker的透明度
|
||||
brush = marker.brush()
|
||||
color = brush.color()
|
||||
color.setAlphaF(alpha)
|
||||
brush.setColor(color)
|
||||
marker.setBrush(brush)
|
||||
# 设置画笔透明度
|
||||
pen = marker.pen()
|
||||
color = pen.color()
|
||||
color.setAlphaF(alpha)
|
||||
pen.setColor(color)
|
||||
marker.setPen(pen)
|
||||
|
||||
def handleMarkerHovered(self, status):
|
||||
# 设置series的画笔宽度
|
||||
marker = self.sender() # 信号发送者
|
||||
if not marker:
|
||||
return
|
||||
series = marker.series()
|
||||
if not series:
|
||||
return
|
||||
pen = series.pen()
|
||||
if not pen:
|
||||
return
|
||||
pen.setWidth(pen.width() + (1 if status else -1))
|
||||
series.setPen(pen)
|
||||
|
||||
def handleSeriesHoverd(self, status,index,barset):
|
||||
return print(status,index,barset)
|
||||
# # 设置series的画笔宽度
|
||||
# series = self.sender() # 信号发送者
|
||||
# pen = series.pen()
|
||||
# if not pen:
|
||||
# return
|
||||
# pen.setWidth(pen.width() + (1 if state else -1))
|
||||
# series.setPen(pen)
|
||||
|
||||
def initChart(self):
|
||||
self._chart = QChart(title="柱状图堆叠")
|
||||
self._chart.setAcceptHoverEvents(True)
|
||||
# Series动画
|
||||
self._chart.setAnimationOptions(QChart.SeriesAnimations)
|
||||
categories = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
|
||||
names = ["邮件营销","联盟广告","视频广告","直接访问","搜索引擎"]
|
||||
series = QBarSeries(self._chart)
|
||||
for name in names:
|
||||
bar = QBarSet(name)
|
||||
#随机数据
|
||||
for _ in range(7):
|
||||
bar.append(randint(0,10))
|
||||
series.append(bar)
|
||||
# series.hovered.connect(self.handleSeriesHoverd) # 鼠标悬停
|
||||
self._chart.addSeries(series)
|
||||
self._chart.createDefaultAxes() # 创建默认的轴
|
||||
# x轴
|
||||
axis_x = QBarCategoryAxis(self._chart)
|
||||
axis_x.append(categories)
|
||||
self._chart.setAxisX(axis_x, series)
|
||||
# chart的图例
|
||||
legend = self._chart.legend()
|
||||
# 设置图例由Series来决定样式
|
||||
legend.setMarkerShape(QLegend.MarkerShapeFromSeries)
|
||||
# 遍历图例上的标记并绑定信号
|
||||
for marker in legend.markers():
|
||||
# 点击事件
|
||||
marker.clicked.connect(self.handleMarkerClicked)
|
||||
# 鼠标悬停事件
|
||||
marker.hovered.connect(self.handleMarkerHovered)
|
||||
self.setChart(self._chart)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
view = ChartView()
|
||||
view.show()
|
||||
sys.exit(app.exec_())
|
5
PyQtChart练习/charts/bar/README.md
Normal file
5
PyQtChart练习/charts/bar/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
# 折线图
|
||||
|
||||
### [1.柱状图堆叠](BarStack.py)
|
||||
like [bar-stack](http://echarts.baidu.com/demo.html#bar-stack)
|
||||
![柱状图堆叠](ScreenShot/BarStack.gif)
|
BIN
PyQtChart练习/charts/bar/ScreenShot/BarStack.gif
Normal file
BIN
PyQtChart练习/charts/bar/ScreenShot/BarStack.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 327 KiB |
|
@ -16,6 +16,7 @@
|
|||
- [1.11 浏览器获取Cookie](浏览器获取Cookie/)
|
||||
- [1.12 全局热键](全局热键/)
|
||||
- [1.13 图片加载](图片加载/)
|
||||
- [1.4 窗口重启](窗口重启/)
|
||||
|
||||
### [2.QGraphicsView练习](QGraphicsView练习/)
|
||||
- [2.1 世界地图](QGraphicsView练习/世界地图)
|
||||
|
|
4
窗口重启/README.md
Normal file
4
窗口重启/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# 重启窗口Widget
|
||||
|
||||
# 截图
|
||||
![截图](ScreenShot/1.gif)
|
62
窗口重启/RestartMainWindow.py
Normal file
62
窗口重启/RestartMainWindow.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Created on 2018年1月17日
|
||||
@author: Irony."[讽刺]
|
||||
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: RestartMainWindow
|
||||
@description:
|
||||
'''
|
||||
from PyQt5.QtCore import pyqtSignal
|
||||
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QPushButton, QLineEdit,\
|
||||
QMessageBox
|
||||
|
||||
|
||||
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
|
||||
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
|
||||
__Version__ = "Version 1.0"
|
||||
|
||||
|
||||
class RestartMainWindow(QWidget):
|
||||
|
||||
restarted = pyqtSignal(QWidget, str)
|
||||
_Self = None # 很重要,保留窗口引用
|
||||
|
||||
def __init__(self, path, *args, **kwargs):
|
||||
super(RestartMainWindow, self).__init__(*args, **kwargs)
|
||||
RestartMainWindow._Self = self
|
||||
layout = QVBoxLayout(self)
|
||||
layout.addWidget(QLabel("当前工作目录:" + path, self))
|
||||
self.dirEdit = QLineEdit(
|
||||
self, placeholderText="请输入要切换的目录", returnPressed=self.onChangeDir)
|
||||
layout.addWidget(self.dirEdit)
|
||||
layout.addWidget(QPushButton(
|
||||
"点我切换工作目录", self, clicked=self.onChangeDir))
|
||||
self.restarted.connect(RestartMainWindow.onRestart)
|
||||
|
||||
def onChangeDir(self):
|
||||
path = self.dirEdit.text().strip()
|
||||
if path and QMessageBox.question(self, "提示", "确认要切换到{0}目录吗?".format(path)) == QMessageBox.Yes:
|
||||
self.hide() # 先隐藏
|
||||
self.restarted.emit(self, path)
|
||||
else:
|
||||
self.dirEdit.setFocus()
|
||||
|
||||
@classmethod
|
||||
def onRestart(cls, widget, path):
|
||||
w = RestartMainWindow(path)
|
||||
w.show()
|
||||
widget.close()
|
||||
widget.deleteLater()
|
||||
del widget
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
app = QApplication(sys.argv)
|
||||
w = RestartMainWindow("test")
|
||||
w.show()
|
||||
sys.exit(app.exec_())
|
BIN
窗口重启/ScreenShot/1.gif
Normal file
BIN
窗口重启/ScreenShot/1.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 74 KiB |
Loading…
Reference in a new issue