最近在学习wxPython时,发现img2py工具只能处理单个图标,就自己写了一个简单的小工具,把文件夹下所有的图标文件转化到py文件里, 话不多说,直接上代码:

 # -*- coding: utf-8 -*-

 # --------------------------------------------------------------------------------------------------------------------------------
# Class: Img2Python
# Platform: WINNT
# Comments: Convert all ico to python file for the selected folder
# User: JustDoIT
# Date: 6th Dec, 2016
# -------------------------------------------------------------------------------------------------------------------------------- import wx, os, sys, subprocess
import img
from wx.tools.img2py import img2py # --------------------------------------------------------------------------------------------------------------------------------
# Class: Img2Python
# --------------------------------------------------------------------------------------------------------------------------------
class Img2Python(wx.Frame): def __init__(self, *args, **kwargs):
super(Img2Python, self).__init__(*args, **kwargs) self.Size = wx.Size(550, 435)
self.SetIcon(img.app.getIcon()) self.InitUI() self.Centre()
self.Show() def InitUI(self): panel = wx.Panel(self) sizer = wx.GridBagSizer(5, 5) text1 = wx.StaticText(panel, label="Image to python")
text1.SetFont(wx.Font(16, wx.DEFAULT, wx.ITALIC, wx.NORMAL))
sizer.Add(text1, pos=(0, 0), span = (1, 4) ,flag = wx.LEFT | wx.TOP | wx.RIGHT, border=5) icon = wx.StaticBitmap(panel, bitmap = img.conf32.getBitmap())
sizer.Add(icon, pos=(0, 4), flag = wx.TOP | wx.RIGHT | wx.ALIGN_RIGHT, border=5) line = wx.StaticLine(panel)
sizer.Add(line, pos=(1, 0), span=(1, 5), flag=wx.EXPAND | wx.BOTTOM, border=0) # -------------------------------------------------------------------------------------------------------------------------------
self.txtdir = wx.TextCtrl(panel, value = 'Select directory ...', style = wx.TE_LEFT | wx.TE_READONLY)
self.btndir = wx.Button(panel, wx.ID_HOME, label = 'Browser') sizer.Add(self.txtdir, pos = (2, 0), span = (1, 4), flag = wx.LEFT | wx.TOP | wx.EXPAND, border = 5)
sizer.Add(self.btndir, pos = (2, 4),flag = wx.TOP | wx.EXPAND, border = 5)
self.btndir.Bind(wx.EVT_BUTTON, self.OnButton) # Attribute choice -------------------------------------------------------------------------------------------------------------
attbox = wx.BoxSizer(wx.HORIZONTAL)
nflag = wx.CheckBox(panel, label = '-n' )
mflag = wx.CheckBox(panel, label = '-m' )
iflag = wx.CheckBox(panel, label = '-i' )
fflag = wx.CheckBox(panel, label = '-f' ) attbox.Add(nflag, flag = wx.LEFT | wx.RIGHT, border = 5)
attbox.Add(mflag, flag = wx.LEFT | wx.RIGHT, border = 15)
attbox.Add(iflag, flag = wx.LEFT | wx.RIGHT, border = 15)
attbox.Add(fflag, flag = wx.LEFT | wx.RIGHT, border = 15) sizer.Add(attbox, pos = (3, 0), span = (1, 5), flag = wx.TOP | wx.BOTTOM | wx.EXPAND, border = 0 ) # ------------------------------------------------------------------------------------------------------------------------------
sb = wx.StaticBox(panel, label = 'File Log')
sboxsizer = wx.StaticBoxSizer(sb, wx.VERTICAL) self.outstr = wx.TextCtrl(panel, size = (525, 160), style = wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.VSCROLL) sboxsizer.Add(self.outstr, proportion = 1, flag = wx.EXPAND) sizer.Add(sboxsizer, pos = (4, 0), span =(1, 5), flag = wx.LEFT | wx.EXPAND | wx.BOTTOM, border = 5) line2 = wx.StaticLine(panel)
sizer.Add(line2, pos=(6, 0), span=(1, 5), flag = wx.BOTTOM | wx.EXPAND, border = 5) # ------------------------------------------------------------------------------------------------------------------------------
btnhelp = wx.Button(panel, 301, label = 'Open')
btnok = wx.Button(panel, 302, label = 'OK')
btncancel = wx.Button(panel, 303, label = 'Cancel') sizer.Add(btnhelp, pos=(7, 0), flag=wx.EXPAND|wx.BOTTOM, border=10)
sizer.Add(btnok, pos=(7, 3), flag=wx.EXPAND|wx.BOTTOM, border=10)
sizer.Add(btncancel, pos=(7, 4), flag=wx.EXPAND|wx.BOTTOM, border=10) btnhelp.Bind(wx.EVT_BUTTON, self.OnButton)
btnok.Bind(wx.EVT_BUTTON, self.OnButton)
btncancel.Bind(wx.EVT_BUTTON, self.OnButton) # ------------------------------------------------------------------------------------------------------------------------------
sizer.AddGrowableCol(0)
sizer.AddGrowableRow(4) panel.SetSizer(sizer) def OnButton(self, e): eid = e.GetId() if eid == wx.ID_HOME: dlg = wx.DirDialog(self, 'Select forder', style = wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.txtdir.SetValue(dlg.GetPath())
print self.txtdir dlg.Destroy() elif eid == 301: if os.path.isdir(self.txtdir.GetValue()):
subprocess.Popen('explorer ' + self.txtdir.GetValue())
else:
wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR) elif eid == 302:
if os.path.isdir(self.txtdir.GetValue()):
self.forderprocess()
else:
wx.MessageBox('Pls select correct image folder !', 'Error', wx.OK | wx.ICON_ERROR)
elif eid == 303:
self.Close()
else: e.Skip() def forderprocess(self):
folder = self.txtdir.GetValue()
listext = ('.png', '.ico', '.icon', '.gif') pyfile = folder + '\img.py' if os.path.isdir(folder): self.outstr.AppendText('Starting.................................................................................\n\n') for name in os.listdir(folder):
(x, ext) = os.path.splitext(name) if ext in listext:
img = os.path.join(folder, name) fs = open('log.txt', 'w')
temp = sys.stdout
sys.stdout = fs self.outstr.AppendText('Name :' + x) if os.path.isfile(pyfile):
ret = img2py(img, pyfile, append = True)
else:
ret = img2py(img, pyfile, append = True) sys.stdout = temp
fs.close()
str = open('log.txt','r').read()
self.outstr.AppendText(str + '\n') self.outstr.AppendText('Done ....................................................................................\n') # --------------------------------------------------------------------------------------------------------------------------------
# Class: APP
# --------------------------------------------------------------------------------------------------------------------------------
class App(wx.App): def OnInit(self):
defaultstyle = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.FRAME_NO_TASKBAR
Img2Python(None, title = 'Image To Python', style = defaultstyle)
return True # --------------------------------------------------------------------------------------------------------------------------------
# Function main()
# --------------------------------------------------------------------------------------------------------------------------------
def main():
App().MainLoop() if __name__ == '__main__':
main()

效果图:

wxPython tools img2py的更多相关文章

  1. 将图片文件转换为.py文件

    最近用wxpython写了一个脚本,其中要给窗体设置图标文件,需要单独的一个ico文件,这样就比较影响美观,另外打包的时候还要将图标文件一起打包很繁琐.这时候看到wxpython文件有一个工具img2 ...

  2. python下wxpython程序国际化的实践(中文英文切换)

    一.什么是python的国际化(I18N) 有关I18N,百度上解释一大堆,个人比较喜欢这个说法. i18n是 Internationalization 这个英文的简写,因为International ...

  3. python---基础知识回顾(九)图形用户界面-------WxPython

    主要使用wxPython(最成熟的跨平台python GUI工具包) wxPython手册 前戏:基础了解 import wx class MyFrame(wx.Frame): #创建自定义Frame ...

  4. wxwidget wxpython 可视化开发工具

    wxwidget官方建议的工具集合:http://wiki.wxwidgets.org/Tools 支持wxpython可视化开发工具 wxFormBuilder wxGlade wxDesigner ...

  5. 离线安装wxpython

    离线安装wxpython 前言 由于工作环境,我的工作机是在离线环境下的,没法连接外网.但是自己又想学习一下wxpython,只好自己手动离线安装,本来以为很简单的,但是实际上...一言难尽. 基本环 ...

  6. openpyxl,xlrd,win32com,wxpython,logging

    目录 一. openpyxl常用操作总结 二. xlrd常用操作总结 三. win32com常用操作总结 四. 自定义异常 五. 判断中文 六. Excel数字字母转换 七. 使用wxpython进行 ...

  7. 基于WxPython的GUI框架toolkit-frame介绍

    源码下载地址:https://download.csdn.net/download/zy0412326/12154342 源码下载地址:https://pan.baidu.com/s/1-s2WaQm ...

  8. 解决 Could not find com.android.tools.build:gradle 问题

    今天拉同事最新的代码,编译时老是报如下错误: Error:Could not find com.android.tools.build:gradle:2.2.0.Searched in the fol ...

  9. 免费的精品: Productivity Power Tools 动画演示

    Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率.它的出现一定程度上弥补和完善了 Visual Studio 自身的不足, ...

随机推荐

  1. j2ee的十三个规范

    转载 今天在做连接oracle数据库的时候,感受到了什么是规范.平时听到别人说学习j2ee一定要学习他的十三个规范,大概的知道每个规范是做什么的,每个“接口”是做什么的.          很早就听过 ...

  2. POJ 2031 Building a Space Station (最小生成树)

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

  3. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

  4. java选项及系统属性

    java选项 -d32 使用 32 位数据模型 (如果可用) -d64 使用 64 位数据模型 (如果可用) -server 选择 "server" VM 默认 VM 是 serv ...

  5. 在MVC项目中使用RDLC报表

    原文地址:http://www.cnblogs.com/wuhuacong/p/4109833.html RDLC是一个不错的报表,有着比较不错的设计模式和展现效果,在我的Winform开发里面,使用 ...

  6. 在VS.NET中根据条件设置不同的MainForm

    在VS.NET中有时候需要根据不同的条件设置不同的MainForm,例如:在程序第一次运行时候打开设置基本信息或服务器信息窗口等. 1.找到VS.NET中设置MainForm的窗口: 2.在编辑窗口中 ...

  7. C#中动态加载和卸载DLL

    在C++中加载和卸载DLL是一件很容易的事,LoadLibrary和FreeLibrary让你能够轻易的在程序中加载DLL,然后在任何地方卸载.在C#中我们也能使用Assembly.LoadFile实 ...

  8. 备份spfile 中的一个误区

    某书载在备份控制文件的时候,也会自动的备份初始化参数文件,抱着愚钝的 完事亲力亲为的态度,做了如下的小验证. RMAN> list backup of controlfile; specific ...

  9. MEF 编程指南(一):在应用中托管 MEF

    在应用程序中托管(Hosing) MEF 涉及到创建组合容器(CompositionContainer) 实例,添加可组合部件(Composable Parts),包括应用程序宿主(Host)本身并进 ...

  10. Educational Codeforces Round 1 C. Nearest vectors 极角排序

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/problem/ ...