-
Notifications
You must be signed in to change notification settings - Fork 0
/
guistarter.py
126 lines (103 loc) · 4.95 KB
/
guistarter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import traceback
from tkinter import Tk
from tkinter import StringVar
from tkinter import scrolledtext
from tkinter import filedialog
from tkinter.font import Font
from tkinter import ttk
from tkinter import messagebox
import mytool
import imagemapping
class Application_ui(ttk.Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.master.title('图片转Base64')
self.master.geometry('406x150')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = ttk.Style()
self.data_url = StringVar(value='')
self.Text1 = ttk.Entry(self.top, textvariable=self.data_url, font=('微软雅黑',9))
self.Text1.place(relx=0.040, rely=0.070, relwidth=0.500, relheight=0.180)
self.style.configure('Command1.TButton',font=('微软雅黑',9))
self.Command1 = ttk.Button(self.top, text='上传', command=self.doupload, style='Command1.TButton')
self.Command1.place(relx=0.571, rely=0.070, relwidth=0.120, relheight=0.180)
self.style.configure('Command2.TButton',font=('微软雅黑',9))
self.Command2 = ttk.Button(self.top, text='转换', command=self.dotrans, style='Command2.TButton')
self.Command2.place(relx=0.730, rely=0.070, relwidth=0.230, relheight=0.180)
#下拉
self.Combo1List = ['不压缩','压缩至webp','压缩至png']
self.Combo1 = ttk.Combobox(self.top,state="readonly", values=self.Combo1List, font=('微软雅黑',9))
self.Combo1.current(0)
self.Combo1.place(relx=0.040, rely=0.300, relwidth=0.350, relheight=0.150)
# 选择框
self.auto_compress = StringVar(value='1')
self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9))
self.Check1 = ttk.Checkbutton(self.top, text='小于60k不压缩', variable=self.auto_compress, style='Check1.TCheckbutton')
self.Check1.place(relx=0.420, rely=0.300, relwidth=0.300, relheight=0.150)
self.with_md = StringVar(value='1')
self.style.configure('Check1.TCheckbutton',font=('微软雅黑',9))
self.Check1 = ttk.Checkbutton(self.top, text='md格式', variable=self.with_md, style='Check1.TCheckbutton')
self.Check1.place(relx=0.760, rely=0.300, relwidth=0.200, relheight=0.150)
self.copytext = StringVar(value='点击复制')
self.style.configure('Command2.TButton',font=('微软雅黑',9))
self.Command3 = ttk.Button(self.top, textvariable =self.copytext, command=self.docopy, style='Command3.TButton')
self.Command3.place(relx=0.040, rely=0.500, relwidth=0.920, relheight=0.430)
class Application(Application_ui):
#这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
def __init__(self, master=None):
Application_ui.__init__(self, master)
result = ""
def doupload(self, event=None):
try:
local_file_path = filedialog.askopenfilename(title='上传', filetypes=[('image', imagemapping.selectfiletypes), ('All Files', '*')])
if local_file_path=="":
return
self.copytext.set("making...")
checkdata = self.Combo1.get()
checkindex = self.Combo1List.index(checkdata)
ifauto = int(self.auto_compress.get())
with_md = int(self.with_md.get())
self.result,showlen = mytool.work_file(local_file_path,checkindex,ifauto,with_md)
if len(self.result)>50:
addToClipBoard(self.result)
self.copytext.set("点击复制, "+showlen)
else:
self.copytext.set("fail!")
except:
messagebox.showinfo(title='error',message= traceback.format_exc())
def dotrans(self, event=None):
try:
self.copytext.set("making...")
dataurl = self.data_url.get()
checkdata = self.Combo1.get()
checkindex = self.Combo1List.index(checkdata)
ifauto = int(self.auto_compress.get())
with_md = int(self.with_md.get())
if len(dataurl)==0:
return
self.result,showlen = mytool.work_url(dataurl,checkindex,ifauto,with_md)
if len(self.result)>50:
addToClipBoard(self.result)
self.copytext.set("点击复制, "+showlen)
else:
self.copytext.set("fail!")
except:
messagebox.showinfo(title='error',message= traceback.format_exc())
def docopy(self, even=None):
addToClipBoard(self.result)
def addToClipBoard(text):
# command = 'echo ' + text + '| clip'
# os.system(command)
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append(text)
r.update()
r.destroy()
if __name__ == "__main__":
top = Tk()
Application(top).mainloop()
# top.destroy()