剪贴板

image.png

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import datetime
  4. from PyQt5.QtWidgets import *
  5. from PyQt5.QtGui import QGuiApplication, QIcon, QFont
  6. from PyQt5.QtCore import QTimer, Qt
  7. class Clipboard2(QMainWindow):
  8. def __init__(self):
  9. super(Clipboard2, self).__init__()
  10. self.setWindowTitle("ClipboardMe")
  11. self.setWindowIcon(QIcon(self.style().standardIcon(QStyle.SP_DesktopIcon)))
  12. desktop = QApplication.desktop()
  13. self.screen_height = desktop.height()
  14. self.screen_width = desktop.width()
  15. self.resize(int(self.screen_width * 0.2), int(self.screen_height * 0.4))
  16. font = QFont()
  17. font.setPointSize(12)
  18. self.setFont(font)
  19. self.text = QTextEdit(self)
  20. self.label_info = QLabel(self)
  21. self.btn_show = QAction(QIcon(self.style().standardIcon(QStyle.SP_DesktopIcon)), "显示", self)
  22. self.btn_show.triggered.connect(self.showNormal)
  23. self.btn_start = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)), "运行中", self)
  24. self.btn_start.setCheckable(True)
  25. self.btn_start.setChecked(True)
  26. self.btn_start.triggered.connect(self.btn_start_click)
  27. self.btn_top = QAction(QIcon(self.style().standardIcon(QStyle.SP_TitleBarNormalButton)), "置顶", self)
  28. self.btn_top.setCheckable(True)
  29. self.btn_top.triggered.connect(self.btn_top_click)
  30. self.btn_sort = QAction(QIcon(self.style().standardIcon(QStyle.SP_FileDialogDetailedView)), "排序", self)
  31. self.btn_sort.triggered.connect(self.btn_sort_click)
  32. self.btn_clear = QAction(QIcon(self.style().standardIcon(QStyle.SP_TrashIcon)), "清空", self)
  33. self.btn_clear.triggered.connect(self.btn_clear_click)
  34. self.btn_save = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogSaveButton)), "保存", self)
  35. self.btn_save.triggered.connect(self.btn_save_click)
  36. self.btn_close = QAction(QIcon(self.style().standardIcon(QStyle.SP_DialogCloseButton)), "退出", self)
  37. self.btn_close.triggered.connect(sys.exit)
  38. toolbar_top = QToolBar("工具栏", self)
  39. toolbar_top.setMovable(False)
  40. toolbar_top.addAction(self.btn_start)
  41. toolbar_top.addAction(self.btn_top)
  42. toolbar_top.addAction(self.btn_sort)
  43. toolbar_top.addAction(self.btn_save)
  44. toolbar_top.addAction(self.btn_clear)
  45. self.tray_icon = QSystemTrayIcon(self)
  46. self.tray_icon.setToolTip("ClipboardMe2: 运行中")
  47. self.tray_icon.activated.connect(self.tray_icon_click)
  48. self.tray_icon.setContextMenu(self.menu_context_menu())
  49. self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
  50. self.tray_icon.show()
  51. self.addToolBar(Qt.RightToolBarArea, toolbar_top)
  52. self.setCentralWidget(self.text)
  53. self.timer = QTimer()
  54. self.timer.timeout.connect(self.timer_event)
  55. self.timer.start(100)
  56. self.buffer = [""]
  57. def menu_context_menu(self):
  58. # 主菜单
  59. menu = QMenu(self)
  60. menu.addAction(self.btn_show)
  61. menu.addSeparator()
  62. menu.addAction(self.btn_start)
  63. menu.addSeparator()
  64. menu.addAction(self.btn_top)
  65. menu.addSeparator()
  66. menu.addAction(self.btn_save)
  67. menu.addAction(self.btn_clear)
  68. menu.addSeparator()
  69. menu.addAction(self.btn_close)
  70. return menu
  71. def timer_event(self):
  72. clipboard = QGuiApplication.clipboard()
  73. content = clipboard.text()
  74. if content == self.buffer[-1]:
  75. return
  76. if not content:
  77. return
  78. self.text.append(content)
  79. self.buffer.append(content)
  80. def btn_start_click(self):
  81. if self.btn_start.isChecked():
  82. self.timer.start()
  83. self.btn_start.setText("运行中")
  84. self.tray_icon.setToolTip("ClipboardMe2: 运行中")
  85. self.btn_start.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
  86. self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogYesButton)))
  87. else:
  88. self.timer.stop()
  89. self.btn_start.setText("已停止")
  90. self.tray_icon.setToolTip("ClipboardMe2: 已停止")
  91. self.btn_start.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogNoButton)))
  92. self.tray_icon.setIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogNoButton)))
  93. def btn_top_click(self):
  94. if self.btn_top.isChecked():
  95. self.setWindowFlags(Qt.WindowStaysOnTopHint)
  96. w_width = int(self.screen_width * 0.2)
  97. w_height = int(self.screen_height * 0.4)
  98. self.resize(w_width, w_height)
  99. # self.move(self.screen_width - w_width, int(self.screen_height * 0.3))
  100. self.move(self.screen_width - w_width, self.pos().y())
  101. else:
  102. self.setWindowFlags(Qt.Widget)
  103. self.showNormal()
  104. def btn_save_click(self):
  105. now = datetime.datetime.now().strftime("%Y%m%dT%H%M")
  106. text = self.text.toPlainText()
  107. if text:
  108. file_path, file_type = QFileDialog.getSaveFileName(self, "另存为 ...",
  109. f"clipboard-{now}.txt",
  110. "Txt(*.txt);;Json(*.json)")
  111. if file_path:
  112. with open(file_path, "wb") as f:
  113. f.write(text.encode())
  114. def btn_sort_click(self):
  115. text = self.text.toPlainText()
  116. if text:
  117. rows = text.splitlines()
  118. lines = {_.strip() for _ in rows if _.strip()}
  119. self.text.setText("\n".join(sorted(lines)))
  120. def btn_clear_click(self):
  121. clipboard = QGuiApplication.clipboard()
  122. clipboard.clear()
  123. self.text.clear()
  124. self.buffer = [""]
  125. def tray_icon_click(self, reason):
  126. if reason == 2:
  127. self.showNormal()
  128. def closeEvent(self, e) -> None:
  129. self.hide()
  130. e.ignore()
  131. if __name__ == '__main__':
  132. import sys
  133. app = QApplication(sys.argv)
  134. frm = Clipboard2()
  135. frm.show()
  136. sys.exit(app.exec_())

时间转换

image.png

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import time
  4. from datetime import datetime
  5. from bson.objectid import ObjectId
  6. from PyQt5.QtWidgets import *
  7. from PyQt5.QtGui import *
  8. def digital_time(timestamp: str, style: str = '%Y-%m-%d %H:%M:%S'):
  9. timestamp = timestamp.split(".")[0].strip()
  10. ptime = time.strptime(timestamp, style)
  11. mtime = time.mktime(ptime)
  12. return int(mtime)
  13. def digital_to_str(val):
  14. aa = time.localtime(val)
  15. bb = time.strftime('%Y-%m-%d %H:%M:%S', aa)
  16. return bb
  17. def friend_time(line):
  18. if ":" in line or "/" in line or "-" in line:
  19. ret = digital_time(line)
  20. return str(ret)
  21. try:
  22. digit = float(line)
  23. except Exception as e:
  24. return None
  25. if digit > 1000000000000:
  26. digit = digit / 1000
  27. ret = digital_to_str(digit)
  28. return ret
  29. class Time2TimeWindow(QFrame):
  30. def __init__(self):
  31. super(Time2TimeWindow, self).__init__()
  32. font = QFont()
  33. font.setPointSize(12)
  34. self.setFont(font)
  35. self.setWindowTitle("T2T")
  36. size = self.size()
  37. self.resize(int(size.width() * 1.2), int(size.height()))
  38. self.setWindowIcon(QIcon(self.style().standardIcon(QStyle.SP_DialogHelpButton)))
  39. self.text = QTextEdit(self)
  40. self.text.setReadOnly(True)
  41. self.text_time = QLineEdit(self)
  42. self.btn_calc = QPushButton("计算", self)
  43. self.btn_calc.clicked.connect(self.btn_calc_click)
  44. self.btn_now = QPushButton("此刻", self)
  45. self.btn_now.clicked.connect(self.btn_now_click)
  46. self.btn_convert = QPushButton("转换", self)
  47. self.btn_convert.clicked.connect(self.btn_convert_click)
  48. layout = QGridLayout()
  49. layout.addWidget(self.text_time, 0, 0)
  50. layout.addWidget(self.btn_calc, 0, 2)
  51. layout.addWidget(self.btn_now, 0, 1)
  52. layout.addWidget(self.btn_convert, 0, 3)
  53. layout.addWidget(self.text, 1, 0, 1, 4)
  54. self.setLayout(layout)
  55. def btn_calc_click(self):
  56. line = str(self.text_time.text())
  57. try:
  58. txt = friend_time(line)
  59. except Exception as e:
  60. txt = e
  61. self.text.setText(str(txt))
  62. def btn_now_click(self):
  63. self.text_time.setText(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
  64. self.text.setText(str(time.time()))
  65. def btn_convert_click(self):
  66. text = str(self.text_time.text())
  67. value = ""
  68. if ":" in text:
  69. try:
  70. digital = digital_time(text)
  71. except Exception as e:
  72. value = str(e)
  73. else:
  74. value = f"{digital:0<24x}"
  75. elif len(text) == 24:
  76. try:
  77. obj = ObjectId(text)
  78. value = obj.generation_time
  79. except Exception as e:
  80. value = str(e)
  81. self.text.setText(str(value))
  82. if __name__ == '__main__':
  83. import sys
  84. app = QApplication(sys.argv)
  85. frm = Time2TimeWindow()
  86. frm.show()
  87. sys.exit(app.exec_())