Python+PyQt5開發(fā)一個截圖工具
制作一個簡單的電腦截圖應(yīng)用,可以使用 Python 結(jié)合 PyQt
或 Tkinter
開發(fā)一個圖形化界面程序,同時使用 Pillow
或 pyautogui
來實現(xiàn)截圖功能。以下是一個使用 Python 和 PyQt5
的示例代碼,展示如何手動寫代碼實現(xiàn)一個截圖工具。
實現(xiàn)步驟
安裝依賴庫:
pip install PyQt5 pyautogui pillow
代碼實現(xiàn):
import sys import pyautogui from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog, QLabel from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt from PIL import Image class ScreenshotApp(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("截圖工具") self.setGeometry(300, 300, 400, 300) # 添加按鈕 self.screenshot_button = QPushButton("截圖", self) self.screenshot_button.setGeometry(150, 50, 100, 50) self.screenshot_button.clicked.connect(self.take_screenshot) self.save_button = QPushButton("保存截圖", self) self.save_button.setGeometry(150, 150, 100, 50) self.save_button.clicked.connect(self.save_screenshot) self.save_button.setEnabled(False) # 禁用按鈕,直到截圖完成 # 顯示截圖的標(biāo)簽 self.screenshot_label = QLabel(self) self.screenshot_label.setGeometry(50, 220, 300, 50) self.screenshot_label.setAlignment(Qt.AlignCenter) # 存儲截圖 self.screenshot = None def take_screenshot(self): # 截取全屏 screenshot = pyautogui.screenshot() self.screenshot = screenshot self.save_button.setEnabled(True) # 啟用保存按鈕 # 將截圖顯示在 GUI 中 screenshot.save("temp.png") # 暫時保存為臨時文件 pixmap = QPixmap("temp.png") self.screenshot_label.setPixmap(pixmap.scaled(300, 50, Qt.KeepAspectRatio)) self.screenshot_label.setText("截圖完成,請保存!") def save_screenshot(self): if self.screenshot: # 選擇保存路徑 file_path, _ = QFileDialog.getSaveFileName(self, "保存截圖", "", "PNG Files (*.png);;All Files (*)") if file_path: self.screenshot.save(file_path) self.screenshot_label.setText(f"截圖已保存到: {file_path}") else: self.screenshot_label.setText("沒有可保存的截圖!") # 主程序運行 if __name__ == "__main__": app = QApplication(sys.argv) window = ScreenshotApp() window.show() sys.exit(app.exec_())
功能說明
1.界面布局:
- 一個截圖按鈕用于截圖。
- 一個保存按鈕用于保存截圖。
- 標(biāo)簽顯示截圖狀態(tài)或截圖縮略圖。
2.功能點:
- 使用
pyautogui.screenshot()
實現(xiàn)屏幕截圖。 - 使用
QFileDialog
提供保存截圖的對話框。 - 截圖后可在 GUI 中顯示縮略圖。
擴展功能
如果需要更復(fù)雜的功能,可以進一步開發(fā):
- 區(qū)域截圖:讓用戶用鼠標(biāo)拖拽選擇截圖區(qū)域。
- 熱鍵功能:使用
keyboard
庫監(jiān)聽快捷鍵。 - 截圖編輯:增加文字標(biāo)注、畫框等功能。
- 多語言支持:為國際用戶提供多語言界面。
這段代碼可以直接運行,作為一個入門項目,適合學(xué)習(xí)界面編程和基本功能開發(fā)!
到此這篇關(guān)于Python+PyQt5開發(fā)一個截圖工具的文章就介紹到這了,更多相關(guān)Python截圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
matplotlib階梯圖的實現(xiàn)(step())
這篇文章主要介紹了matplotlib階梯圖的實現(xiàn)(step()),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03python數(shù)學(xué)建模是加深Numpy和Pandas學(xué)習(xí)
這篇文章主要介紹了python數(shù)學(xué)建模是加深Numpy和Pandas學(xué)習(xí),緊接上一篇學(xué)習(xí)內(nèi)容展開Numpy更多相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-07-07手動安裝Anaconda環(huán)境變量的實現(xiàn)教程
這篇文章主要介紹了手動安裝Anaconda環(huán)境變量的實現(xiàn)教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01解決Linux系統(tǒng)中python matplotlib畫圖的中文顯示問題
這篇文章主要介紹了解決Linux系統(tǒng)中python matplotlib畫圖的中文顯示問題,需要的朋友可以參考下2017-06-06django 微信網(wǎng)頁授權(quán)登陸的實現(xiàn)
這篇文章主要介紹了django 微信網(wǎng)頁授權(quán)登陸的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python使用epoll實現(xiàn)服務(wù)端的方法
今天小編就為大家分享一篇python使用epoll實現(xiàn)服務(wù)端的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10