使用Python編寫一個桌面便簽應(yīng)用
ChatGPT的編程能力也不差,本次我就一步一步提要求,讓ChatGPT根據(jù)我的要求,編寫出一個可用的,可打包運行的桌面便簽。

代碼
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction, QSystemTrayIcon, QMessageBox, QTextEdit
from PyQt5.QtCore import Qt, QPoint, QRect, QSize
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen, QIcon, QFont, QCursor
class RoundedWindow(QMainWindow):
def __init__(self, radius):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setGeometry(700, 400, 400, 300)
self.radius = radius
self.draggable = False
self.drag_position = QPoint()
self.default_font_size = 13
self.current_font_size = self.default_font_size
self.resizing = False
# 創(chuàng)建系統(tǒng)托盤圖標(biāo)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon("noteIcon.png"))
self.tray_icon.activated.connect(self.handleTrayIconActivated)
# 創(chuàng)建鼠標(biāo)右鍵菜單
self.tray_menu = QMenu(self)
exit_action = QAction("退出", self)
exit_action.triggered.connect(QApplication.instance().quit)
self.tray_menu.addAction(exit_action)
self.tray_icon.setContextMenu(self.tray_menu)
# 創(chuàng)建文本編輯框
self.text_edit = QTextEdit(self)
self.text_edit.setGeometry(10, 40, self.width() - 20, self.height() - 50)
self.text_edit.setStyleSheet("background-color: transparent; border: none; color: white;")
self.text_edit.setFont(QFont("Arial", self.current_font_size))
self.text_edit.textChanged.connect(self.saveTextToFile)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setBrush(QColor(0, 0, 0, 150)) # 設(shè)置半透明背景顏色
painter.drawRoundedRect(self.rect(), self.radius, self.radius)
# 繪制紅色關(guān)閉按鈕
close_button = QRect(10, 10, 20, 20)
painter.setBrush(QColor(255, 0, 0))
painter.setPen(Qt.NoPen)
painter.drawEllipse(close_button)
# 繪制黃色最小化按鈕
minimize_button = QRect(40, 10, 20, 20)
painter.setBrush(QColor(255, 255, 0))
painter.setPen(Qt.NoPen)
painter.drawEllipse(minimize_button)
# 繪制灰色最大化按鈕
maximize_button = QRect(70, 10, 20, 20)
painter.setBrush(QColor(128, 128, 128))
painter.setPen(Qt.NoPen)
painter.drawEllipse(maximize_button)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.draggable = True
self.drag_position = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
# 判斷點擊的按鈕
pos = event.pos()
if QRect(10, 10, 20, 20).contains(pos):
self.close() # 關(guān)閉當(dāng)前窗口
elif QRect(40, 10, 20, 20).contains(pos):
self.hide() # 最小化當(dāng)前窗口
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton and self.draggable:
self.move(event.globalPos() - self.drag_position)
event.accept()
# 檢查是否在窗口右下角,設(shè)置鼠標(biāo)形狀
if self.isInBottomRightCorner(event.pos()):
self.setCursor(Qt.SizeFDiagCursor)
else:
self.setCursor(Qt.ArrowCursor)
# 檢查是否正在調(diào)整窗口大小
if self.resizing:
self.resizeWindow(event.globalPos())
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.draggable = False
self.resizing = False
event.accept()
def handleTrayIconActivated(self, reason):
if reason == QSystemTrayIcon.Trigger:
self.showNormal() # 點擊托盤圖標(biāo)恢復(fù)窗口顯示
def closeEvent(self, event):
self.hide() # 窗口關(guān)閉時隱藏而不是退出應(yīng)用程序
self.tray_icon.show() # 顯示系統(tǒng)托盤圖標(biāo)
event.ignore() # 忽略窗口關(guān)閉事件
def saveTextToFile(self):
text = self.text_edit.toPlainText()
with open("bianqian.txt", "w") as file:
file.write(text)
def isInBottomRightCorner(self, pos):
window_rect = self.rect()
corner_rect = QRect(window_rect.bottomRight() - QPoint(20, 20), QSize(20, 20))
return corner_rect.contains(pos)
def resizeWindow(self, pos):
new_size = QSize(pos.x() - self.geometry().left(), pos.y() - self.geometry().top())
self.resize(new_size)
def wheelEvent(self, event):
if event.modifiers() == Qt.ControlModifier:
delta = event.angleDelta().y()
if delta > 0:
self.increaseFontSize()
else:
self.decreaseFontSize()
def increaseFontSize(self):
self.current_font_size += 1
self.text_edit.setFont(QFont("Arial", self.current_font_size))
def decreaseFontSize(self):
if self.current_font_size > 1:
self.current_font_size -= 1
self.text_edit.setFont(QFont("Arial", self.current_font_size))
if __name__ == '__main__':
app = QApplication(sys.argv)
radius = 15 # 修改圓角的值
window = RoundedWindow(radius)
window.show()
# 調(diào)試:檢查系統(tǒng)托盤是否可用
if not QSystemTrayIcon.isSystemTrayAvailable():
QMessageBox.critical(None, "錯誤", "系統(tǒng)托盤不可用!")
sys.exit(1)
# 調(diào)試:檢查圖標(biāo)是否加載成功
if not window.tray_icon.isSystemTrayAvailable():
QMessageBox.critical(None, "錯誤", "無法加載系統(tǒng)托盤圖標(biāo)!")
sys.exit(1)
window.tray_icon.show()
sys.exit(app.exec_())運行

便簽屬性
1、半透明、圓角、最小化、系統(tǒng)托盤
2、按住Ctrl不放滾動鼠標(biāo)可改變文字大小
3、系統(tǒng)托盤鼠標(biāo)右鍵完全退出
4、便簽輸入的文字實時更新至bianqian.txt
打包成exe
這么點東西打包成exe居然有34.9M這么大!這絕對不是Python的問題,是我技術(shù)的問題。

以上就是使用Python編寫一個桌面便簽應(yīng)用的詳細內(nèi)容,更多關(guān)于Python桌面便簽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python opencv實現(xiàn)圖像配準(zhǔn)與比較
這篇文章主要為大家詳細介紹了python opencv實現(xiàn)圖像配準(zhǔn)與比較,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-02-02
使用python提取PowerPoint幻燈片中表格并保存到文本及Excel文件
owerPoint作為廣泛使用的演示工具,常被用于展示各類數(shù)據(jù)報告和分析結(jié)果,其中,表格以其直觀性和結(jié)構(gòu)性成為闡述數(shù)據(jù)關(guān)系的不二之選,本文將介紹如何使用Python來提取PowerPoint幻燈片中的表格,并將表格數(shù)據(jù)寫入文本文件以及Excel文件,需要的朋友可以參考下2024-06-06
pygame游戲之旅 調(diào)用按鈕實現(xiàn)游戲開始功能
這篇文章主要為大家詳細介紹了pygame游戲之旅的第12篇,教大家調(diào)用按鈕實現(xiàn)游戲開始功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11

