剪贴板
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QGuiApplication, QIcon, QFont
from PyQt5.QtCore import QTimer, Qt
class Clipboard2(QMainWindow):
def __init__(self):
super(Clipboard2, self).__init__()
self.setWindowTitle("ClipboardMe")
self.setWindowIcon(QIcon(self.style().standardIcon(QStyle.SP_DesktopIcon)))
desktop = QApplication.desktop()
self.screen_height = desktop.height()
self.screen_width = desktop.width()
self.resize(int(self.screen_width * 0.2), int(self.screen_height * 0.4))
font = QFont()
font.setPointSize(12)
self.setFont(font)
self.text = QTextEdit(self)
self.label_info = QLabel(self)
self.btn_show = QAction(QIcon(self.style().standardIcon(QStyle.SP_DesktopIcon)), "显示", self)
self.btn_show.triggered.connect(self.showNormal)
self.btn_start = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)), "运行中", self)
self.btn_start.setCheckable(True)
self.btn_start.setChecked(True)
self.btn_start.triggered.connect(self.btn_start_click)
self.btn_top = QAction(QIcon(self.style().standardIcon(QStyle.SP_TitleBarNormalButton)), "置顶", self)
self.btn_top.setCheckable(True)
self.btn_top.triggered.connect(self.btn_top_click)
self.btn_sort = QAction(QIcon(self.style().standardIcon(QStyle.SP_FileDialogDetailedView)), "排序", self)
self.btn_sort.triggered.connect(self.btn_sort_click)
self.btn_clear = QAction(QIcon(self.style().standardIcon(QStyle.SP_TrashIcon)), "清空", self)
self.btn_clear.triggered.connect(self.btn_clear_click)
self.btn_save = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogSaveButton)), "保存", self)
self.btn_save.triggered.connect(self.btn_save_click)
self.btn_close = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogCloseButton)), "退出", self)
self.btn_close.triggered.connect(sys.exit)
toolbar_top = QToolBar("工具栏", self)
toolbar_top.setMovable(False)
toolbar_top.addAction(self.btn_start)
toolbar_top.addAction(self.btn_top)
toolbar_top.addAction(self.btn_sort)
toolbar_top.addAction(self.btn_save)
toolbar_top.addAction(self.btn_clear)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setToolTip("ClipboardMe2: 运行中")
self.tray_icon.activated.connect(self.tray_icon_click)
self.tray_icon.setContextMenu(self.menu_context_menu())
self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
self.tray_icon.show()
self.addToolBar(Qt.RightToolBarArea, toolbar_top)
self.setCentralWidget(self.text)
self.timer = QTimer()
self.timer.timeout.connect(self.timer_event)
self.timer.start(100)
self.buffer = [""]
def menu_context_menu(self):
# 主菜单
menu = QMenu(self)
menu.addAction(self.btn_show)
menu.addSeparator()
menu.addAction(self.btn_start)
menu.addSeparator()
menu.addAction(self.btn_top)
menu.addSeparator()
menu.addAction(self.btn_save)
menu.addAction(self.btn_clear)
menu.addSeparator()
menu.addAction(self.btn_close)
return menu
def timer_event(self):
clipboard = QGuiApplication.clipboard()
content = clipboard.text()
if content == self.buffer[-1]:
return
if not content:
return
self.text.append(content)
self.buffer.append(content)
def btn_start_click(self):
if self.btn_start.isChecked():
self.timer.start()
self.btn_start.setText("运行中")
self.tray_icon.setToolTip("ClipboardMe2: 运行中")
self.btn_start.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
else:
self.timer.stop()
self.btn_start.setText("已停止")
self.tray_icon.setToolTip("ClipboardMe2: 已停止")
self.btn_start.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogNoButton)))
self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogNoButton)))
def btn_top_click(self):
if self.btn_top.isChecked():
self.setWindowFlags(Qt.WindowStaysOnTopHint)
w_width = int(self.screen_width * 0.2)
w_height = int(self.screen_height * 0.4)
self.resize(w_width, w_height)
# self.move(self.screen_width - w_width, int(self.screen_height * 0.3))
self.move(self.screen_width - w_width, self.pos().y())
else:
self.setWindowFlags(Qt.Widget)
self.showNormal()
def btn_save_click(self):
now = datetime.datetime.now().strftime("%Y%m%dT%H%M")
text = self.text.toPlainText()
if text:
file_path, file_type = QFileDialog.getSaveFileName(self, "另存为 ...",
f"clipboard-{now}.txt",
"Txt(*.txt);;Json(*.json)")
if file_path:
with open(file_path, "wb") as f:
f.write(text.encode())
def btn_sort_click(self):
text = self.text.toPlainText()
if text:
rows = text.splitlines()
lines = {_.strip() for _ in rows if _.strip()}
self.text.setText("\n".join(sorted(lines)))
def btn_clear_click(self):
clipboard = QGuiApplication.clipboard()
clipboard.clear()
self.text.clear()
self.buffer = [""]
def tray_icon_click(self, reason):
if reason == 2:
self.showNormal()
def closeEvent(self, e) -> None:
self.hide()
e.ignore()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
frm = Clipboard2()
frm.show()
sys.exit(app.exec_())
时间转换
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from datetime import datetime
from bson.objectid import ObjectId
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
def digital_time(timestamp: str, style: str = '%Y-%m-%d %H:%M:%S'):
timestamp = timestamp.split(".")[0].strip()
ptime = time.strptime(timestamp, style)
mtime = time.mktime(ptime)
return int(mtime)
def digital_to_str(val):
aa = time.localtime(val)
bb = time.strftime('%Y-%m-%d %H:%M:%S', aa)
return bb
def friend_time(line):
if ":" in line or "/" in line or "-" in line:
ret = digital_time(line)
return str(ret)
try:
digit = float(line)
except Exception as e:
return None
if digit > 1000000000000:
digit = digit / 1000
ret = digital_to_str(digit)
return ret
class Time2TimeWindow(QFrame):
def __init__(self):
super(Time2TimeWindow, self).__init__()
font = QFont()
font.setPointSize(12)
self.setFont(font)
self.setWindowTitle("T2T")
size = self.size()
self.resize(int(size.width() * 1.2), int(size.height()))
self.setWindowIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogHelpButton)))
self.text = QTextEdit(self)
self.text.setReadOnly(True)
self.text_time = QLineEdit(self)
self.btn_calc = QPushButton("计算", self)
self.btn_calc.clicked.connect(self.btn_calc_click)
self.btn_now = QPushButton("此刻", self)
self.btn_now.clicked.connect(self.btn_now_click)
self.btn_convert = QPushButton("转换", self)
self.btn_convert.clicked.connect(self.btn_convert_click)
layout = QGridLayout()
layout.addWidget(self.text_time, 0, 0)
layout.addWidget(self.btn_calc, 0, 2)
layout.addWidget(self.btn_now, 0, 1)
layout.addWidget(self.btn_convert, 0, 3)
layout.addWidget(self.text, 1, 0, 1, 4)
self.setLayout(layout)
def btn_calc_click(self):
line = str(self.text_time.text())
try:
txt = friend_time(line)
except Exception as e:
txt = e
self.text.setText(str(txt))
def btn_now_click(self):
self.text_time.setText(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
self.text.setText(str(time.time()))
def btn_convert_click(self):
text = str(self.text_time.text())
value = ""
if ":" in text:
try:
digital = digital_time(text)
except Exception as e:
value = str(e)
else:
value = f"{digital:0<24x}"
elif len(text) == 24:
try:
obj = ObjectId(text)
value = obj.generation_time
except Exception as e:
value = str(e)
self.text.setText(str(value))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
frm = Time2TimeWindow()
frm.show()
sys.exit(app.exec_())