This commit is contained in:
Irony 2017-12-28 12:13:23 +08:00
parent 3984aac7e0
commit 25d62dbfa5
4 changed files with 296 additions and 295 deletions

View file

@ -24,3 +24,4 @@ encoding//\u81EA\u52A8\u66F4\u65B0/test.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/CustomPaintWidget.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/CustomWidget.py=utf-8
encoding//\u81EA\u5B9A\u4E49QWidget\u7684QSS\u6837\u5F0F/test.py=utf-8
encoding//\u8868\u683C\u590D\u5236/TableView.py=utf-8

View file

@ -1,61 +1,61 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年12月10日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: test
@description:
'''
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout
from CustomPaintWidget import CustomPaintWidget # @UnresolvedImport
from CustomWidget import CustomWidget # @UnresolvedImport
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class TestWidget(QWidget):
def __init__(self, *args, **kwargs):
super(TestWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(CustomPaintWidget(self))
layout.addWidget(CustomWidget(self))
#注意
w = CustomWidget(self)
w.setAttribute(Qt.WA_StyledBackground)#很重要
layout.addWidget(w)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet('''
CustomPaintWidget {
min-width: 100px;
min-height: 100px;
border: 1px solid green;
border-radius: 20px;
background: green;
}
CustomWidget {
min-width: 200px;
min-height: 200px;
max-width: 200px;
max-height: 200px;
border: 1px solid orange;
border-radius: 100px;
background: orange;
}
''')
w = TestWidget()
w.show()
sys.exit(app.exec_())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年12月10日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: test
@description:
'''
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout
from CustomPaintWidget import CustomPaintWidget # @UnresolvedImport
from CustomWidget import CustomWidget # @UnresolvedImport
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class TestWidget(QWidget):
def __init__(self, *args, **kwargs):
super(TestWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(CustomPaintWidget(self))
layout.addWidget(CustomWidget(self))
#注意
w = CustomWidget(self)
w.setAttribute(Qt.WA_StyledBackground)#很重要
layout.addWidget(w)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet('''
CustomPaintWidget {
min-width: 100px;
min-height: 100px;
border: 1px solid green;
border-radius: 20px;
background: green;
}
CustomWidget {
min-width: 200px;
min-height: 200px;
max-width: 200px;
max-height: 200px;
border: 1px solid orange;
border-radius: 100px;
background: orange;
}
''')
w = TestWidget()
w.show()
sys.exit(app.exec_())

View file

@ -1,107 +1,107 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年4月6日
@author: Irony."[讽刺]
@site: alyl.vip, orzorz.vip, irony.coding.me , irony.iask.in , mzone.iask.in
@email: 892768447@qq.com
@file: TableView
@description:
'''
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QTableView, QApplication, QAction, QMessageBox
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class TableView(QTableView):
def __init__(self, parent=None):
super(TableView, self).__init__(parent)
self.resize(800, 600)
self.setContextMenuPolicy(Qt.ActionsContextMenu) # 右键菜单
self.setEditTriggers(self.NoEditTriggers) # 禁止编辑
self.doubleClicked.connect(self.onDoubleClick)
self.addAction(QAction("复制", self, triggered=self.copyData))
self.myModel = QStandardItemModel() # model
self.initHeader() # 初始化表头
self.setModel(self.myModel)
self.initData() # 初始化模拟数据
def onDoubleClick(self, index):
print(index.row(), index.column(), index.data())
def keyPressEvent(self, event):
super(TableView, self).keyPressEvent(event)
# Ctrl + C
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C:
self.copyData()
def copyData(self):
count = len(self.selectedIndexes())
if count == 0:
return
if count == 1: # 只复制了一个
QApplication.clipboard().setText(
self.selectedIndexes()[0].data()) # 复制到剪贴板中
QMessageBox.information(self, "提示", "已复制一个数据")
return
rows = set()
cols = set()
for index in self.selectedIndexes(): # 得到所有选择的
rows.add(index.row())
cols.add(index.column())
# print(index.row(),index.column(),index.data())
if len(rows) == 1: # 一行
QApplication.clipboard().setText("\t".join(
[index.data() for index in self.selectedIndexes()])) # 复制
QMessageBox.information(self, "提示", "已复制一行数据")
return
if len(cols) == 1: # 一列
QApplication.clipboard().setText("\r\n".join(
[index.data() for index in self.selectedIndexes()])) # 复制
QMessageBox.information(self, "提示", "已复制一列数据")
return
mirow, marow = min(rows), max(rows) # 最(少/多)行
micol, macol = min(cols), max(cols) # 最(少/多)列
print(mirow, marow, micol, macol)
arrays = [
[
"" for _ in range(macol - micol + 1)
] for _ in range(marow - mirow + 1)
] # 创建二维数组(并排除前面的空行和空列)
print(arrays)
# 填充数据
for index in self.selectedIndexes(): # 遍历所有选择的
arrays[index.row() - mirow][index.column() - micol] = index.data()
print(arrays)
data = "" # 最后的结果
for row in arrays:
data += "\t".join(row) + "\r\n"
print(data)
QApplication.clipboard().setText(data) # 复制到剪贴板中
QMessageBox.information(self, "提示", "已复制")
def initHeader(self):
for i in range(5):
self.myModel.setHorizontalHeaderItem(
i, QStandardItem("表头" + str(i + 1)))
def initData(self):
for row in range(100):
for col in range(5):
self.myModel.setItem(
row, col, QStandardItem("row: {row},col: {col}".format(row=row + 1, col=col + 1)))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setApplicationName("TableView")
w = TableView()
w.show()
sys.exit(app.exec_())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年4月6日
@author: Irony."[讽刺]
@site: alyl.vip, orzorz.vip, irony.coding.me , irony.iask.in , mzone.iask.in
@email: 892768447@qq.com
@file: TableView
@description:
'''
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QTableView, QApplication, QAction, QMessageBox
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class TableView(QTableView):
def __init__(self, parent=None):
super(TableView, self).__init__(parent)
self.resize(800, 600)
self.setContextMenuPolicy(Qt.ActionsContextMenu) # 右键菜单
self.setEditTriggers(self.NoEditTriggers) # 禁止编辑
self.doubleClicked.connect(self.onDoubleClick)
self.addAction(QAction("复制", self, triggered=self.copyData))
self.myModel = QStandardItemModel() # model
self.initHeader() # 初始化表头
self.setModel(self.myModel)
self.initData() # 初始化模拟数据
def onDoubleClick(self, index):
print(index.row(), index.column(), index.data())
def keyPressEvent(self, event):
super(TableView, self).keyPressEvent(event)
# Ctrl + C
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C:
self.copyData()
def copyData(self):
count = len(self.selectedIndexes())
if count == 0:
return
if count == 1: # 只复制了一个
QApplication.clipboard().setText(
self.selectedIndexes()[0].data()) # 复制到剪贴板中
QMessageBox.information(self, "提示", "已复制一个数据")
return
rows = set()
cols = set()
for index in self.selectedIndexes(): # 得到所有选择的
rows.add(index.row())
cols.add(index.column())
# print(index.row(),index.column(),index.data())
if len(rows) == 1: # 一行
QApplication.clipboard().setText("\t".join(
[index.data() for index in self.selectedIndexes()])) # 复制
QMessageBox.information(self, "提示", "已复制一行数据")
return
if len(cols) == 1: # 一列
QApplication.clipboard().setText("\r\n".join(
[index.data() for index in self.selectedIndexes()])) # 复制
QMessageBox.information(self, "提示", "已复制一列数据")
return
mirow, marow = min(rows), max(rows) # 最(少/多)行
micol, macol = min(cols), max(cols) # 最(少/多)列
print(mirow, marow, micol, macol)
arrays = [
[
"" for _ in range(macol - micol + 1)
] for _ in range(marow - mirow + 1)
] # 创建二维数组(并排除前面的空行和空列)
print(arrays)
# 填充数据
for index in self.selectedIndexes(): # 遍历所有选择的
arrays[index.row() - mirow][index.column() - micol] = index.data()
print(arrays)
data = "" # 最后的结果
for row in arrays:
data += "\t".join(row) + "\r\n"
print(data)
QApplication.clipboard().setText(data) # 复制到剪贴板中
QMessageBox.information(self, "提示", "已复制")
def initHeader(self):
for i in range(5):
self.myModel.setHorizontalHeaderItem(
i, QStandardItem("表头" + str(i + 1)))
def initData(self):
for row in range(100):
for col in range(5):
self.myModel.setItem(
row, col, QStandardItem("row: {row},col: {col}".format(row=row + 1, col=col + 1)))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
app.setApplicationName("TableView")
w = TableView()
w.show()
sys.exit(app.exec_())

View file

@ -1,127 +1,127 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年4月5日
@author: Irony."[讽刺]
@site: alyl.vip, orzorz.vip, irony.coding.me , irony.iask.in , mzone.iask.in
@email: 892768447@qq.com
@file: widgets.WidgetCode
@description:
'''
from random import sample
import string
from PyQt5.QtCore import Qt, qrand, QPointF, QPoint, QBasicTimer
from PyQt5.QtGui import QPainter, QBrush, QPen, QPalette, QFontMetrics
from PyQt5.QtWidgets import QLabel
__version__ = "0.0.1"
DEF_NOISYPOINTCOUNT = 60 # 噪点数量
COLORLIST = ("black", "gray", "red", "green", "blue", "magenta")
TCOLORLIST = (Qt.black, Qt.gray, Qt.red, Qt.green, Qt.blue, Qt.magenta)
QTCOLORLIST = (Qt.darkGray, Qt.darkRed, Qt.darkGreen, Qt.darkBlue, Qt.darkMagenta)
HTML = "<html><body>{html}</body></html>"
FONT = "<font color=\"{color}\">{word}</font>"
WORDS = list(string.ascii_letters + string.digits)
SINETABLE = (0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38)
class WidgetCode(QLabel):
def __init__(self, *args, **kwargs):
super(WidgetCode, self).__init__(*args, **kwargs)
self._sensitive = False # 是否大小写敏感
self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.setBackgroundRole(QPalette.Midlight)
self.setAutoFillBackground(True)
# 字体
newFont = self.font()
newFont.setPointSize(16)
newFont.setFamily("Kristen ITC")
newFont.setBold(True)
self.setFont(newFont)
self.reset()
# 定时器
self.step = 0
self.timer = QBasicTimer()
self.timer.start(60, self)
def reset(self):
self._code = "".join(sample(WORDS, 4)) # 随机4个字符
self.setText(self._code)
def check(self, code):
return self._code == str(code) if self._sensitive else self._code.lower() == str(code).lower()
def setSensitive(self, sensitive):
self._sensitive = sensitive
# def setText(self, text):
# text = text if (text and len(text) == 4) else "".join(sample(WORDS, 4)) # 随机4个字符
# self._code = str(text)
# html = "".join([FONT.format(color=COLORLIST[qrand() % 6], word=t) for t in text])
# super(WidgetCode, self).setText(HTML.format(html=html))
def mouseReleaseEvent(self, event):
super(WidgetCode, self).mouseReleaseEvent(event)
self.reset()
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
self.step += 1
return self.update()
return super(WidgetCode, self).timerEvent(event)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# 背景白色
painter.fillRect(event.rect(), QBrush(Qt.white))
# 绘制边缘虚线框
painter.setPen(Qt.DashLine)
painter.setBrush(Qt.NoBrush)
painter.drawRect(self.rect())
# 随机画条线
for _ in range(3):
painter.setPen(QPen(QTCOLORLIST[qrand() % 5], 1, Qt.SolidLine))
painter.setBrush(Qt.NoBrush)
painter.drawLine(QPoint(0, qrand() % self.height()),
QPoint(self.width(), qrand() % self.height()))
painter.drawLine(QPoint(qrand() % self.width(), 0),
QPoint(qrand() % self.width(), self.height()))
# 绘制噪点
painter.setPen(Qt.DotLine)
painter.setBrush(Qt.NoBrush)
for _ in range(self.width()): # 绘制噪点
painter.drawPoint(QPointF(qrand() % self.width(), qrand() % self.height()))
# super(WidgetCode, self).paintEvent(event) # 绘制文字
# 绘制跳动文字
metrics = QFontMetrics(self.font())
x = (self.width() - metrics.width(self.text())) / 2
y = (self.height() + metrics.ascent() - metrics.descent()) / 2
for i, ch in enumerate(self.text()):
index = (self.step + i) % 16
painter.setPen(TCOLORLIST[qrand() % 6])
painter.drawText(x, y - ((SINETABLE[index] * metrics.height()) / 400), ch)
x += metrics.width(ch)
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
from PyQt5.QtGui import QFontDatabase
from PyQt5.QtWidgets import QLineEdit
app = QApplication(sys.argv)
app.setApplicationName("Validate Code")
QFontDatabase.addApplicationFont("itckrist.ttf")
w = QWidget()
layout = QHBoxLayout(w)
cwidget = WidgetCode(w, minimumHeight=35, minimumWidth=80)
layout.addWidget(cwidget)
lineEdit = QLineEdit(w, maxLength=4, placeholderText="请输入验证码并按回车验证",
returnPressed=lambda:print(cwidget.check(lineEdit.text())))
layout.addWidget(lineEdit)
w.show()
sys.exit(app.exec_())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年4月5日
@author: Irony."[讽刺]
@site: alyl.vip, orzorz.vip, irony.coding.me , irony.iask.in , mzone.iask.in
@email: 892768447@qq.com
@file: widgets.WidgetCode
@description:
'''
from random import sample
import string
from PyQt5.QtCore import Qt, qrand, QPointF, QPoint, QBasicTimer
from PyQt5.QtGui import QPainter, QBrush, QPen, QPalette, QFontMetrics
from PyQt5.QtWidgets import QLabel
__version__ = "0.0.1"
DEF_NOISYPOINTCOUNT = 60 # 噪点数量
COLORLIST = ("black", "gray", "red", "green", "blue", "magenta")
TCOLORLIST = (Qt.black, Qt.gray, Qt.red, Qt.green, Qt.blue, Qt.magenta)
QTCOLORLIST = (Qt.darkGray, Qt.darkRed, Qt.darkGreen, Qt.darkBlue, Qt.darkMagenta)
HTML = "<html><body>{html}</body></html>"
FONT = "<font color=\"{color}\">{word}</font>"
WORDS = list(string.ascii_letters + string.digits)
SINETABLE = (0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38)
class WidgetCode(QLabel):
def __init__(self, *args, **kwargs):
super(WidgetCode, self).__init__(*args, **kwargs)
self._sensitive = False # 是否大小写敏感
self.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.setBackgroundRole(QPalette.Midlight)
self.setAutoFillBackground(True)
# 字体
newFont = self.font()
newFont.setPointSize(16)
newFont.setFamily("Kristen ITC")
newFont.setBold(True)
self.setFont(newFont)
self.reset()
# 定时器
self.step = 0
self.timer = QBasicTimer()
self.timer.start(60, self)
def reset(self):
self._code = "".join(sample(WORDS, 4)) # 随机4个字符
self.setText(self._code)
def check(self, code):
return self._code == str(code) if self._sensitive else self._code.lower() == str(code).lower()
def setSensitive(self, sensitive):
self._sensitive = sensitive
# def setText(self, text):
# text = text if (text and len(text) == 4) else "".join(sample(WORDS, 4)) # 随机4个字符
# self._code = str(text)
# html = "".join([FONT.format(color=COLORLIST[qrand() % 6], word=t) for t in text])
# super(WidgetCode, self).setText(HTML.format(html=html))
def mouseReleaseEvent(self, event):
super(WidgetCode, self).mouseReleaseEvent(event)
self.reset()
def timerEvent(self, event):
if event.timerId() == self.timer.timerId():
self.step += 1
return self.update()
return super(WidgetCode, self).timerEvent(event)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# 背景白色
painter.fillRect(event.rect(), QBrush(Qt.white))
# 绘制边缘虚线框
painter.setPen(Qt.DashLine)
painter.setBrush(Qt.NoBrush)
painter.drawRect(self.rect())
# 随机画条线
for _ in range(3):
painter.setPen(QPen(QTCOLORLIST[qrand() % 5], 1, Qt.SolidLine))
painter.setBrush(Qt.NoBrush)
painter.drawLine(QPoint(0, qrand() % self.height()),
QPoint(self.width(), qrand() % self.height()))
painter.drawLine(QPoint(qrand() % self.width(), 0),
QPoint(qrand() % self.width(), self.height()))
# 绘制噪点
painter.setPen(Qt.DotLine)
painter.setBrush(Qt.NoBrush)
for _ in range(self.width()): # 绘制噪点
painter.drawPoint(QPointF(qrand() % self.width(), qrand() % self.height()))
# super(WidgetCode, self).paintEvent(event) # 绘制文字
# 绘制跳动文字
metrics = QFontMetrics(self.font())
x = (self.width() - metrics.width(self.text())) / 2
y = (self.height() + metrics.ascent() - metrics.descent()) / 2
for i, ch in enumerate(self.text()):
index = (self.step + i) % 16
painter.setPen(TCOLORLIST[qrand() % 6])
painter.drawText(x, y - ((SINETABLE[index] * metrics.height()) / 400), ch)
x += metrics.width(ch)
if __name__ == "__main__":
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout
from PyQt5.QtGui import QFontDatabase
from PyQt5.QtWidgets import QLineEdit
app = QApplication(sys.argv)
app.setApplicationName("Validate Code")
QFontDatabase.addApplicationFont("itckrist.ttf")
w = QWidget()
layout = QHBoxLayout(w)
cwidget = WidgetCode(w, minimumHeight=35, minimumWidth=80)
layout.addWidget(cwidget)
lineEdit = QLineEdit(w, maxLength=4, placeholderText="请输入验证码并按回车验证",
returnPressed=lambda:print(cwidget.check(lineEdit.text())))
layout.addWidget(lineEdit)
w.show()
sys.exit(app.exec_())