提示:这里可以添加本文要记录的大概内容:
使用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_())
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)