使用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 3.8新特征之a(chǎn)syncio REPL
我最近都在寫一些Python 3.8的新功能介紹的文章,在自己的項目中也在提前體驗新的Python版本。這篇文章主要介紹了Python 3.8新特征之a(chǎn)syncio REPL,需要的朋友可以參考下2019-05-05
Python實現(xiàn)遍歷windows所有窗口并輸出窗口標(biāo)題的方法
這篇文章主要介紹了Python實現(xiàn)遍歷windows所有窗口并輸出窗口標(biāo)題的方法,涉及Python調(diào)用及遍歷windows窗口句柄的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Python任務(wù)調(diào)度利器之APScheduler詳解
所謂的任務(wù)調(diào)度是指安排任務(wù)的執(zhí)行計劃,即何時執(zhí)行,怎么執(zhí)行等。這篇文章主要介紹了Python任務(wù)調(diào)度利器之APScheduler詳解,需要的朋友可以參考下2020-04-04
Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳)
今天小編就為大家分享一篇Python之time模塊的時間戳,時間字符串格式化與轉(zhuǎn)換方法(13位時間戳),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

