使用Python編寫(xiě)一個(gè)桌面便簽應(yīng)用
ChatGPT的編程能力也不差,本次我就一步一步提要求,讓ChatGPT根據(jù)我的要求,編寫(xiě)出一個(gè)可用的,可打包運(yùn)行的桌面便簽。
代碼
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)托盤(pán)圖標(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() # 判斷點(diǎn)擊的按鈕 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() # 點(diǎn)擊托盤(pán)圖標(biāo)恢復(fù)窗口顯示 def closeEvent(self, event): self.hide() # 窗口關(guān)閉時(shí)隱藏而不是退出應(yīng)用程序 self.tray_icon.show() # 顯示系統(tǒng)托盤(pán)圖標(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)托盤(pán)是否可用 if not QSystemTrayIcon.isSystemTrayAvailable(): QMessageBox.critical(None, "錯(cuò)誤", "系統(tǒng)托盤(pán)不可用!") sys.exit(1) # 調(diào)試:檢查圖標(biāo)是否加載成功 if not window.tray_icon.isSystemTrayAvailable(): QMessageBox.critical(None, "錯(cuò)誤", "無(wú)法加載系統(tǒng)托盤(pán)圖標(biāo)!") sys.exit(1) window.tray_icon.show() sys.exit(app.exec_())
運(yùn)行
便簽屬性
1、半透明、圓角、最小化、系統(tǒng)托盤(pán)
2、按住Ctrl不放滾動(dòng)鼠標(biāo)可改變文字大小
3、系統(tǒng)托盤(pán)鼠標(biāo)右鍵完全退出
4、便簽輸入的文字實(shí)時(shí)更新至bianqian.txt
打包成exe
這么點(diǎn)東西打包成exe居然有34.9M這么大!這絕對(duì)不是Python的問(wèn)題,是我技術(shù)的問(wèn)題。
以上就是使用Python編寫(xiě)一個(gè)桌面便簽應(yīng)用的詳細(xì)內(nèi)容,更多關(guān)于Python桌面便簽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python基于socketserver實(shí)現(xiàn)并發(fā),驗(yàn)證客戶端的合法性
TCP協(xié)議的socket一次只能和一個(gè)客戶端通信, 而socketsever可以時(shí)間和多個(gè)客戶端通信。本文將講解socketserver的具體使用2021-05-05python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換
用python處理地理位置是非常常見(jiàn)的需求,下面這篇文章主要給大家介紹了關(guān)于python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Android申請(qǐng)相機(jī)權(quán)限和讀寫(xiě)權(quán)限實(shí)例
大家好,本篇文章主要講的是Android申請(qǐng)相機(jī)權(quán)限和讀寫(xiě)權(quán)限實(shí)例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Python構(gòu)建網(wǎng)頁(yè)爬蟲(chóng)原理分析
這篇文章主要給大家講解了構(gòu)建網(wǎng)頁(yè)爬蟲(chóng)的技術(shù)原理以及實(shí)現(xiàn)的邏輯關(guān)系,有興趣的朋友閱讀下吧。2017-12-12用Python編寫(xiě)一個(gè)鼠標(biāo)自動(dòng)點(diǎn)擊程序詳細(xì)過(guò)程
這篇文章主要給大家介紹了關(guān)于如何使用Python編寫(xiě)一個(gè)鼠標(biāo)自動(dòng)點(diǎn)擊程序,通過(guò)使用pyautogui庫(kù),可以實(shí)現(xiàn)模擬鼠標(biāo)點(diǎn)擊的功能,文章還介紹了如何使用time庫(kù)和threading庫(kù)來(lái)控制點(diǎn)擊間隔時(shí)間和實(shí)現(xiàn)多線程,需要的朋友可以參考下2024-11-11