使用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)用的詳細(xì)內(nèi)容,更多關(guān)于Python桌面便簽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python基于socketserver實現(xiàn)并發(fā),驗證客戶端的合法性
TCP協(xié)議的socket一次只能和一個客戶端通信, 而socketsever可以時間和多個客戶端通信。本文將講解socketserver的具體使用2021-05-05python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換
用python處理地理位置是非常常見的需求,下面這篇文章主要給大家介紹了關(guān)于python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Android申請相機權(quán)限和讀寫權(quán)限實例
大家好,本篇文章主要講的是Android申請相機權(quán)限和讀寫權(quán)限實例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02用Python編寫一個鼠標(biāo)自動點擊程序詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于如何使用Python編寫一個鼠標(biāo)自動點擊程序,通過使用pyautogui庫,可以實現(xiàn)模擬鼠標(biāo)點擊的功能,文章還介紹了如何使用time庫和threading庫來控制點擊間隔時間和實現(xiàn)多線程,需要的朋友可以參考下2024-11-11