制作中!

This commit is contained in:
liyp 2022-03-16 21:59:09 +08:00
commit 68a958a69b
4 changed files with 240 additions and 0 deletions

0
README.md Normal file
View file

96
start.py Normal file
View file

@ -0,0 +1,96 @@
import sys
from PyQt5 import QtCore
from PyQt5.QtGui import QFont, QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from qt_material import apply_stylesheet
# import
class MainWin(QWidget):
def __init__(self):
super(MainWin, self).__init__()
self.setWindowTitle('上传至阿里云cos')
self.resize(400, 500)
self.setWindowIcon(QIcon('./images/icon.png'))
layout = QVBoxLayout()
self.config_btn = QPushButton('配置')
# self.config_btn
self.config_btn.setFixedSize(60, 40)
self.config_btn.clicked.connect(self.configAction)
layout.addStretch()
layout.addWidget(self.config_btn, 0, Qt.AlignCenter)
layout.addStretch()
# layout.addLayout(tip_widget)
layout.addStretch()
self.setLayout(layout)
def configAction(self):
dialog = QDialog()
dialog.setMinimumWidth(500)
dialog.setWindowTitle("配置oss信息")
access_key_id_label = QLabel('access_key_id:')
access_key_id_text = QLineEdit()
access_key_secret_label = QLabel('access_key_secret:')
access_key_secret_text = QLineEdit()
access_key_secret_text.setEchoMode(QLineEdit.Password)
bucket_name_label = QLabel('bucket_name')
bucket_name_text = QLineEdit()
bucket_name_text.setPlaceholderText('设置存储空间名')
endpoint_label = QLabel('endpoint')
endpoint_text = QLineEdit()
endpoint_text.setPlaceholderText('地域节点(如oss-cn-beijing.aliyuncs.com)')
upload_path_label = QLabel('upload_path')
upload_path_text = QLineEdit()
upload_path_text.setPlaceholderText('上传路径(如images/)')
upload_domain_label = QLabel('upload_domain')
upload_domain_text = QLineEdit()
upload_domain_text.setPlaceholderText('绑定域名(需要带https://)')
dialog_layout = QGridLayout()
font = QFont()
font.setFamily("Arial")
font.setPointSize(50)
dialog.setFont(font)
dialog_layout.addWidget(access_key_id_label, 0, 0)
dialog_layout.addWidget(access_key_id_text, 0, 1)
dialog_layout.addWidget(access_key_secret_label, 1, 0)
dialog_layout.addWidget(access_key_secret_text, 1, 1)
dialog_layout.addWidget(bucket_name_label, 2, 0)
dialog_layout.addWidget(bucket_name_text, 2, 1)
dialog_layout.addWidget(endpoint_label, 3, 0)
dialog_layout.addWidget(endpoint_text, 3, 1)
dialog_layout.addWidget(upload_path_label, 4, 0)
dialog_layout.addWidget(upload_path_text, 4, 1)
dialog_layout.addWidget(upload_domain_label, 5, 0)
dialog_layout.addWidget(upload_domain_text, 5, 1)
save_btn = QPushButton('保存配置')
content = [access_key_id_text.text(), access_key_secret_text.text(), bucket_name_text.text(),
endpoint_text.text(), upload_path_text.text(), upload_domain_text.text()]
save_btn.clicked.connect(lambda: self.save_oss_config(content))
dialog_layout.addWidget(save_btn, 6, 1)
dialog.setLayout(dialog_layout)
# 设置窗口的属性为ApplicationModal模态用户只有关闭弹窗后才能关闭主界面
dialog.setWindowModality(QtCore.Qt.ApplicationModal)
dialog.exec_()
def save_oss_config(self, content):
print(content)
for i in content:
print(i)
# pass
if __name__ == '__main__':
app = QApplication(sys.argv)
main = MainWin()
apply_stylesheet(app, theme='dark_yellow.xml')
app.setWindowIcon(QIcon('./images/icon.svg'))
main.show()
sys.exit(app.exec_())

52
tool/upload.py Normal file
View file

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import json
import oss2
# 以下代码展示了文件上传的高级用法,如断点续传、分片上传等。
# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
# 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
#
# 以杭州区域为例Endpoint可以是
# http://oss-cn-hangzhou.aliyuncs.com
# https://oss-cn-hangzhou.aliyuncs.com
# 分别以HTTP、HTTPS协议访问。
config = {}
with open('../QtPicUpload/config.json', 'r') as file:
config = json.load(file)
# print(str(config))
access_key_id = config['OSS_ACCESS_KEY_ID']
# print(access_key_id)
access_key_secret = config['OSS_ACCESS_KEY_SECRET']
# print(access_key_secret)
bucket_name = config['OSS_BUCKET']
endpoint = config['OSS_ENDPOINT']
upload_path = config['UPLOAD_PATH'] if config['UPLOAD_PATH'][-1] == '/' else config['UPLOAD_PATH'] + '/'
# 在域名后面添加 /
upload_domain = config['UPLOAD_DOMAIN'] if config['UPLOAD_DOMAIN'][-1] == '/' else config['UPLOAD_DOMAIN'] + '/'
# print(upload_domain)
# 确认上面的参数都填写正确了
try:
for param in (access_key_id, access_key_secret, bucket_name, endpoint, upload_path, upload_domain):
assert '' != param, '请配置上传参数!'
except Exception as e:
print(e)
def upload(filepath):
print(filepath)
# 创建Bucket对象所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
# 必须以二进制的方式打开文件。
# 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
with open(filepath, 'rb') as fileobj:
# 填写Object完整路径。Object完整路径中不能包含Bucket名称。
upload_name = upload_path + filepath.split('/')[-1]
bucket.put_object(upload_name, fileobj)
return upload_domain + upload_name
# print(upload('/home/liyp/Pictures/壁纸/web-illust_71411811_20181116_203823.png'))

92
tool/uploadTip.py Normal file
View file

@ -0,0 +1,92 @@
import sys
import time
import os
import qt_material
from PyQt5.QtCore import QSize
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import QPixmap
class UploadTip(QWidget):
def __init__(self):
super(UploadTip, self).__init__()
self.pic_show = QLabel()
# self.resize(300,500)
self.pic_show.setMaximumSize(300, 400)
self.pic_show.setPixmap(
QPixmap('../images/icon.png'))
# .scaled(self.pic_show.size(), aspectRatioMode=QtCore.Qt.KeepAspectRatio)
self.pic_tip = QLabel('Ctrl + V 粘贴图片')
self.pic_tip.setAlignment(QtCore.Qt.AlignCenter)
self.tip_layout = QVBoxLayout()
self.tip_layout.addStretch()
self.tip_layout.addWidget(self.pic_show)
self.tip_layout.addStretch()
self.tip_layout.addWidget(self.pic_tip)
self.tip_layout.addStretch()
# layout.setSpacing(0)
# 美化风格
# self.setStyleSheet(qt_material.apply_stylesheet(self,''))
self.createActions()
self.setLayout(self.tip_layout)
def createActions(self):
pastAction = QAction(self)
pastAction.setShortcut("Ctrl+V")
pastAction.triggered.connect(self.pasteData)
self.addAction((pastAction)) # Activate QAction
def setImage(self, path):
image = QPixmap(path)
if image.width() > image.height():
scale = self.pic_show.width() / image.width()
# print('比例:', scale)
# width=scale*clipboard.pixmap().width()*scale
height = image.height() * scale
# print('转换后高度:', height)
self.pic_show.setPixmap(image.scaled(QSize(self.pic_show.width(), int(height)))) # 用于粘贴图片
else:
scale = self.pic_show.height() / image.height()
# print('比例:', scale)
width = image.width() * scale
# print('转换后宽度:', width)
self.pic_show.setPixmap(image.scaled(QSize(int(width), self.pic_show.height())))
self.pic_show.setAlignment(QtCore.Qt.AlignCenter)
# self.pic_show.repaint()
# pass
def pasteData(self):
clipboard = QApplication.clipboard()
mimeData = clipboard.mimeData()
if mimeData.hasImage():
# 根据时间设置图片文件名
file_name = time.strftime('%Y-%m-%d-%H%M%S', time.localtime()) + '.png'
# 将图片保存到指定位置
clipboard.pixmap().save(file_name, 'PNG')
restore_path = '../'
self.setImage(restore_path + file_name)
os.remove(restore_path + file_name)
elif mimeData.hasText():
path = clipboard.text()
self.setImage(path)
print("pasted from clipboard")
# if __name__ == '__main__':
# app = QApplication(sys.argv)
# qt_material.apply_stylesheet(app, theme='light_teal.xml')
# main = UploadTip()
# app.setWindowIcon(QIcon('./images/icon.svg'))
# main.show()
# sys.exit(app.exec_())