使用python Tkinter制作一个可以生成重复文本片段的小工具
2017-05-26 23:46
1106 查看
作用:
使用python Tkinter制作一个图形界面,可以快速生成重复文本小片段
代码:
使用方法:
1. 文本中的参数使用3个括号,如(((A)))
2. 文本只有2个参数,A、B
3. 输入文本之后,点击generate即可生成目标文本
截图示例:
参考:
http://blog.csdn.net/hzliyaya/article/details/9326107
http://www.tkdocs.com/tutorial/text.html
http://blog.csdn.net/wangyiyan315/article/details/16821381
https://stackoverflow.com/questions/14946963/tkinter-grid-how-to-position-widgets-so-they-are-not-stuck-together
http://www.jianshu.com/p/276d6d69f0b0
使用python Tkinter制作一个图形界面,可以快速生成重复文本小片段
代码:
from Tkinter import * from tkSimpleDialog import askstring from tkFileDialog import asksaveasfilename from tkMessageBox import askokcancel import re class Quitter(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack() widget = Button(self, text='Quit', command=self.quit) widget.pack(expand=YES, fill=BOTH, side=LEFT) def quit(self): ans = askokcancel('Verify exit', "Really quit?") if ans: Frame.quit(self) class ScrolledText(Frame): def __init__(self, parent=None, text='', file=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.makewidgets() self.settext(text, file) def makewidgets(self): sbar = Scrollbar(self) text = Text(self, relief=SUNKEN) sbar.config(command=text.yview) text.config(yscrollcommand=sbar.set) sbar.pack(side=RIGHT, fill=Y) text.pack(side=LEFT, expand=YES, fill=BOTH) self.text = text def settext(self, text='', file=None): if file: text = open(file, 'r').read() self.text.delete('1.0', END) self.text.insert('1.0', text) self.text.mark_set(INSERT, '1.0') self.text.focus() def gettext(self): return self.text.get('1.0', END+'-1c') class SimpleEditor(ScrolledText): def __init__(self, parent=None, file=None): Frame.__init__(self, parent) frm = Frame(parent) frm.pack(fill=X) Button(frm, text='Save', command=self.onSave).pack(side=LEFT) Button(frm, text='Cut', command=self.onCut).pack(side=LEFT) Button(frm, text='Paste', command=self.onPaste).pack(side=LEFT) Button(frm, text='Find', command=self.onFind).pack(side=LEFT) Button(frm, text='Generate', command=self.onGenerate).pack(side=LEFT) Quitter(frm).pack(side=LEFT) # # InputText(frm).pack(side=RIGHT, padx=20) self.widget_label_a = Label(frm, text="parameter A").pack(side=LEFT) self.widget_parameter_a = Text(frm, height=1, width=5) self.widget_parameter_a.insert("1.0", '0') self.widget_parameter_a.pack(side=LEFT) widget_label_b = Label(frm, text="parameter B").pack(side=LEFT) self.widget_parameter_b = Text(frm, height=1, width=5) self.widget_parameter_b.insert("1.0", '0') self.widget_parameter_b.pack(side=LEFT) # set frame properties ScrolledText.__init__(self, parent, file=file) self.text.config(font=('courier', 9, 'normal')) # bind ctrl-a keys to select all text self.text.bind_class("Text","<Control-a>", self.selectAll) def onSave(self): filename = asksaveasfilename() if filename: alltext = self.gettext() open(filename, 'w').write(alltext) def onCut(self): text = self.text.get(SEL_FIRST, SEL_LAST) self.text.delete(SEL_FIRST, SEL_LAST) self.clipboard_clear() self.clipboard_append(text) def onPaste(self): try: text = self.selection_get(selection='CLIPBOARD') self.text.insert(INSERT, text) except TclError: pass def onFind(self): target = askstring('SimpleEditor', 'Search String?') if target: where = self.text.search(target, INSERT, END) if where: print where pastit = where + ('+%dc' % len(target)) #self.text.tag_remove(SEL, '1.0', END) self.text.tag_add(SEL, where, pastit) self.text.mark_set(INSERT, pastit) self.text.see(INSERT) self.text.focus() def selectAll(self,event): event.widget.tag_add("sel","1.0","end") def onGenerate(self,times=1,text='',file=None): times_a = int(self.widget_parameter_a.get("1.0", END)) times_b = int(self.widget_parameter_b.get("1.0", END)) input = self.gettext() if file: text = open(file, 'r').read() self.text.delete('1.0', END) for i in range(times_a): sub_part = re.compile(r'\(\(\([A-Z]\)\)\)') sub_text = sub_part.sub(str(i), input) self.text.insert(END, sub_text) for j in range(times_b): sub_part = re.compile(r'\(\(\([A-Z]\)\)\)') sub_text = sub_part.sub(str(j), input) self.text.insert(END, sub_text) self.text.focus() if __name__ == '__main__': try: SimpleEditor(file=sys.argv[1]).mainloop() except IndexError: SimpleEditor().mainloop()
使用方法:
1. 文本中的参数使用3个括号,如(((A)))
2. 文本只有2个参数,A、B
3. 输入文本之后,点击generate即可生成目标文本
截图示例:
参考:
http://blog.csdn.net/hzliyaya/article/details/9326107
http://www.tkdocs.com/tutorial/text.html
http://blog.csdn.net/wangyiyan315/article/details/16821381
https://stackoverflow.com/questions/14946963/tkinter-grid-how-to-position-widgets-so-they-are-not-stuck-together
http://www.jianshu.com/p/276d6d69f0b0
相关文章推荐
- 使用Python实现一个文本对比报告生成工具
- pngCanvas 是一个使用纯Python代码的生成png图像的工具
- Python3基础 生成一个可以使用for循环可迭代的类,具有__iter__和__next__的方法的类
- XNA Game Studio是一套有着强大功能和简单界面的游戏制作平台,游戏开发商和游戏玩家都可以使用这套工具开发针对Windows XP以及Xbox360的游戏,XNA Game Studio分为两种版本,一种是面向初学者的EXPRESS版本,还有一种
- 一个截屏工具制作的全过程记录——如何使用“拿来主义”
- Python我入门了,使用Python生成文本还是挺方便的
- 相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。
- XNA Game Studio是一套有着强大功能和简单界面的游戏制作平台,游戏开发商和游戏玩家都可以使用这套工具开发针对Windows XP以及Xbox360的游戏,XNA Game Studio分为两种版本,一种是面向初学者的EXPRESS版本,还有一种是面向专业用户的专业版。用户使用EXPRESS版本开发游戏完全免费,并且可以随意在PC上发行,不过用该工具开发的游戏若是在360上网络发行,就需要交纳99美元的年费。
- 一个截屏工具制作的全过程记录——如何使用“拿来主义”
- 使用python实现一个通用协议测试工具
- 一个墙纸还可以改变使用裁剪工具改变图像
- 一个文件重复生成的小工具[附源码和可执行文件] 推荐
- FCKeditor 一个开源的web页面文本编辑工具,在vs 2005中的使用方法
- Q:目前我计划写一个应用程序调用WinDriver生成的DLL档案,请问若WinDriver三十天试用期结束后,该DLL档案是否可以正常使用?
- c#2.0开发的一个文本字符串替换工具,控制台工具,可以批量替换
- wget用法wget是一个从网络上自动下载文件的自由工具。它支持HTTP,HTTPS和FTP协议,可以使用HTTP代理.
- 黄聪:C#代码生成工具:文本模板初体验 使用T4批量修改实体框架(Entity Framework)的类名
- 使用python制作简单的日志以及日志分析工具
- Js 日期选择,可以的一个页面中重复使用本JS日历,兼容IE及火狐等主流浏览器,而且界面简洁、美观,操作体验也不错。
- Python:在Linux下创建一个脚本并且自动修改权限,可以选择路径。批量生成脚本 。