python实现按下特定键截屏

python实现按下特定键截屏,第1张

前言

提示:这里可以添加本文要记录的大概内容:

使用pyqt选择文件路径,pyautogui实现截全屏


提示:以下是本篇文章正文内容,下面案例可供参考

一、界面图

二、程序

cut_screen.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'cut_screen.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(332, 106)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(130, 60, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 0, 301, 31))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "选择文件"))
        self.label.setText(_translate("MainWindow", "请点击按钮选择图片保存路径"))



start.py

from PyQt5 import QtWidgets
from sys import argv,exit
from PyQt5.QtWidgets import QFileDialog
from cut_screen import Ui_MainWindow
import keyboard
import pyautogui
import time

ui = None
filepath=''
num=0
time=time.time()
flag=False

def get_filepath():
    global flag
    global filepath
    filepath = QFileDialog.getExistingDirectory(None, "请选择文件夹路径", "./")
    filepath=filepath + '/'
    if filepath == "":
        # print("\n取消选择")
        return

    # print("\n你选择的文件夹为:")
    # print(filepath)
    flag=True
    ui.set_lable(filepath)


def set_lable(filepath):
    ui.label.setText(filepath)


def save_imgs(x):
    global num
    if not flag:
        ui.label.setText('请先选择文件路径')
        return
    if x.event_type == 'down' and (x.name == 's' or x.name == 'S'):
        im = pyautogui.screenshot()
        file=filepath+str(int(time))+str(num)+'.png'
        im.save(file)
        print(file)
        num+=1

keyboard.hook(save_imgs)

if __name__ == "__main__":
    app = QtWidgets.QApplication(argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()  # Ui_Dialog为通过ui生成的just.py文件中的类名称
    ui.setupUi(MainWindow)
    ui.pushButton.clicked.connect(get_filepath)
    MainWindow.show()  # 显示窗口
    exit(app.exec_())

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/797308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存