Tkinter开发Symfony命令辅助工具(1)

 

一、Python Tkinter

在Python GUI开发中,有多种库可供选择,如:Tkinter、WxPython、PyQT、pyGTK、Jython、MFC、PythonCard、Dabo、AnyGUI、WPY、IronPython。本文将采用Tkinter开发一款PHP框架Symfony的命令辅助工具。

 

Tkinter是Tk图形用户界面工具包标准的Python接口,Tk是一个轻量级的跨平台图形用户界面(GUI)开发工具,Tk和Tkinter可以运行在大多数的Unix平台、Windows、和Macintosh系统。在开发小型应用的时候,Tkinter以其简洁高效实乃不二之选。

 

二、工具说明及最终界面

在采用PHP框架Symfony开发时,经常需要在命令行运行生成项目、模块和清除缓存等命令,于是在初学Python之际,采用Tkinter开发此工具以练手,最终界面如下图:

 

工具说明:“项目路径”--可手动输入或者通过“选择”输入项目根目录,“命令”--手动输入symfony命令,“参数”--输入命令参数,“运行”--运行命令,“清除”--清除所有输入框的内容,在最后的文本框中将显示命令运行结果。

 

三、源码(tools.py)

# -*- coding: utf-8 -*- #载入Tkinter from Tkinter import * #载入文件选择框 from FileDialog import * #字体设置 import tkFont import os #运行按钮执行函数 def runTool(): #获取项目路径 path_value = path_entry.get() #获取命令 cmd_value = cmd_entry.get() if not path_value: log_text.insert(END, '请选择项目路径!/n', 'n') else: #获取参数 param_value = param_entry.get() #完整命令 sym_cmd = path_value + 'symfony ' + cmd_value + ' ' + param_value log_text.insert(END, sym_cmd + '/n', 'c') #运行命令并获取返回内容 cmd_response = os.popen(sym_cmd).readlines() #将返回内容写入文本框 log_text.insert(END, ''.join(cmd_response), 'l') del cmd_response #清除按钮执行函数 def cls(): path_entry.delete(0, END) cmd_entry.delete(0, END) param_entry.delete(0, END) log_text.delete(1.0, END) #选择按钮执行函数 def choice(): fd = FileDialog(root, title = '项目路径') file_path = fd.go() if file_path: path_entry.insert(0, file_path) #初始化Tk root = Tk() root.title('Symfony Tool') #设置字体 ft = tkFont.Font(family = 'Courier New', size = 10) ft1 = tkFont.Font(family = 'Courier New', size = 8) #创建项目路径标签 path_label = Label(root, text = '项目路径: ', font = ft) #采用grid布局方式,下同 path_label.grid(row = 0, column = 0, sticky = W) #创建项目路径输入框 path_entry = Entry(root, font = ft, width = 60, border = 2) path_entry.grid(row = 0, column = 1, sticky = W) #创建选择按钮 choice_btn = Button(root, text = '选择', font = ft, width = 20, command = choice) choice_btn.grid(row = 0, column = 2, sticky = W) #创建命令标签 cmd_label = Label(root, text = '命 令: ', font = ft) cmd_label.grid(row = 1, column = 0, sticky = W) #创建命令输入框 cmd_entry = Entry(root, font = ft, width = 60, border = 2) cmd_entry.grid(row = 1, column = 1, sticky = W) #创建运行按钮 run_btn = Button(root, text = '运行', font = ft, command = runTool, width = 20) run_btn.grid(row = 1, column = 2, sticky = W) #参数标签 param_label = Label(root, text = '参 数: ', font = ft) param_label.grid(row = 2, column = 0, sticky = W) #参数输入框 param_entry = Entry(root, font = ft, width = 60, border = 2) param_entry.grid(row = 2, column = 1, sticky = W) #清除按钮 cls_btn = Button(root, text = '清除', font = ft, command = cls, width = 20) cls_btn.grid(row = 2, column = 2, sticky = W) #文本框 log_text = Text(root, font = ft, width = 91, border = 2,) #创建tag指定文本属性 log_text.tag_config('n', foreground = 'red', background = '#000000') log_text.tag_config('c', background = 'yellow', foreground = 'red') log_text.tag_config('l', foreground = '#FFFFFF', background = 'blue') log_text.grid(columnspan = 3, sticky = W) #Copyright cpy_label = Label(root, font = ft1, text = 'Symfony Tool V0.1, Author: Caleng Tan') cpy_label.grid(columnspan = 3, sticky = W) root.mainloop() 

四、直接运行搞定(>python tools.py),另推荐Tkinter教程(英文):http://www.tutorialspoint.com/python/python_gui_programming.htm,中文教程可下载附件《Tkinter编程代码实例》

 

 

你可能感兴趣的:(Python,工具,path,cmd,python,border,command)