小强哥博客

小强哥,小强哥博客,技术大咖

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

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

效果图如下:

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命令启动即可。

完。

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

image