使用Python实现一个七牛GUI小程序客户端

经常使用有道云笔记的MarkDown功能来写文章或者记录日志难免会向文章中插入图片,可是有道云笔记的这功能是收费的,因此用python开发一个GUI小工具可以可以帮我把图片上传到七牛cdn,然后我就可以把这图片插入到markdown中。

原文链接:http://xiaoqiangge.com/aritcle/1538974186596.html

效果图如下:

image

这小程序是基于tkinter开发,在python2.7上测试通过,首先需要安装python2-tkinter,如果机器上已经安装了tkinter可以忽略,如下:

yum -y install python2-tkinter.x86_64

下面是主程序代码,如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
上传参考:https://www.cnblogs.com/liwenchi/p/7290283.html
模拟键盘复制、剪切:https://zhidao.baidu.com/question/500622172439661004.html
'''
import time
import json
import hashlib
import os.path
from qiniu import Auth, put_file, etag
import qiniu.config
from Tkinter import *
import tkFileDialog as filedialog

###########################################################

#需要填写你的 Access Key 和 Secret Key
access_key = 'xxxxx'
secret_key = 'yyyy'
#需要填写CDN服务域名URL,
cdn_url = 'http://xxx.xxx.com/'
#要上传的空间
bucket_name = 'xxxx'
#构建鉴权对象
q = Auth(access_key, secret_key)

############################################################
def copy(event=None):
    resultText.select_range(0,END)
    # print 'copy'
    resultText.event_generate("<<Copy>>")
def upload():
    filePath = browseText.get()
    # print filePath
    extension = file_extension(filePath)
    # print extension
    fileName = int(time.time())
    # print fileName
    # #上传到七牛后保存的文件名
    key = str(fileName)+extension
    # print key
    # #生成上传 Token,可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)
    # print token
    ret, info = put_file(token, key, filePath)
    # print info.text_body
    result = cdn_url + json.loads(info.text_body)['key']

    resultText.delete(0, END)
    resultText.insert(0, result)

def file_extension(filePath):
  return os.path.splitext(filePath)[1]

def browse():
    filePath = filedialog.askopenfilename(title='选择图片')
    browseText.delete(0, END)
    browseText.insert(0, filePath)

# 初始化Tk()
myWindow = Tk()
# 设置标题
myWindow.title('七牛云图片上传小工具')
# 设置窗口大小
width = 750
height = 200
# 获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
screenwidth = myWindow.winfo_screenwidth()
screenheight = myWindow.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
myWindow.geometry(alignstr)
# 设置窗口是否可变长、宽,True:可变,False:不可变
myWindow.resizable(width=False, height=False)


frame_root = Frame(myWindow)
frame_root.pack(pady = 10)

frame_r1 = Frame(frame_root)
frame_r2 = Frame(frame_root)
frame_r1.pack()
frame_r2.pack(side = LEFT, pady = 10)

frame_r1_c1 = Frame(frame_r1)
frame_r1_c2 = Frame(frame_r1)
frame_r1_c3 = Frame(frame_r1)
frame_r1_c4 = Frame(frame_r1)
frame_r1_c1.pack(side = LEFT)
frame_r1_c2.pack(side = LEFT)
frame_r1_c3.pack(side = LEFT)
frame_r1_c4.pack(side = LEFT)

frame_r2_c1 = Frame(frame_r2)
frame_r2_c2 = Frame(frame_r2)
frame_r2_c3 = Frame(frame_r2)
frame_r2_c1.pack(side = LEFT)
frame_r2_c2.pack(side = LEFT)
frame_r2_c3.pack(side = LEFT)

Label(frame_r1_c1, text = '上传文件:').pack()
browseText = Entry(frame_r1_c2, width = 70)
browseText.pack()
Button(frame_r1_c3, text = '浏览...', command = browse).pack()
Button(frame_r1_c4, text = '上传图片', command = upload).pack()


Label(frame_r2_c1, text = '上传结果:').pack()
resultText = Entry(frame_r2_c2, width = 70)
resultText.pack()
Button(frame_r2_c3, text = '一键复制', command = copy).pack()

myWindow.mainloop()

最后通过python命令启动即可。

完。

原文链接:http://xiaoqiangge.com/aritcle/1538974186596.html

如有疑问扫描下面微信二维码添加好友

image

# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'aaa.ui' # # Created by: PyQt5 UI code generator 5.11.3 # # WARNING! All changes made in this file will be lost! import sys, os from PyQt5 import QtCore, QtWidgets, QtGui from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5 import QtCore, QtGui, QtWidgets class U_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1204, 836) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.tableWidget = QtWidgets.QTableWidget(self.centralwidget) self.tableWidget.setGeometry(QtCore.QRect(80, 240, 1041, 371)) self.tableWidget.setAutoScrollMargin(9) self.tableWidget.setRowCount(9) self.tableWidget.setColumnCount(8) self.tableWidget.setObjectName("tableWidget") item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(0, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(1, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(2, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(3, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(4, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(5, item) item = QtWidgets.QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(6, item) item = Q
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值