学习python图形执行命令,根据网上的资料,自己写了一个,linux图形界面绝对是没有命令好用的,不过,发这个文章是在于学习不在于使用,写得不好的地方,欢迎各位提出意见
[root@localhost ~]# cat cmd.py
#-*- encoding=UTF-8 -*-
import Tkinter
from Tkinter import *
from FileDialog import *
import os
root = Tk()
import tkFont
root.title('图形执行命令工具')
root.geometry('800x400')
root.resizable(False,False)#固定窗口大小
f = tkFont.Font(family = 'Courier New', size = 10)#字体大小
#路径函数
def cmd_path():
fd = FileDialog(root, title = '命令路径')
file_path = fd.go()
if file_path:
path_entry.insert(0, file_path)#把路径插入对话框里
#清屏函数(这里每一个对话框都清除)
def cmd_clear():
path_entry.delete(0, END)
param_entry.delete(0, END)
log_text.delete(1.0, END)
#命令运行函数
def cmd_run():
cmd_entry = Entry(root,border = 0)
cmd_entry.grid()
_path = path_entry.get() #获取命令路径
_cmd = cmd_entry.get() #获取命令
if not _path: #在这里做一个判断,如果没有选择路径就显示以下
log_text.insert(END, '请选择正确的路径!\n', 'n')
else:
_value = param_entry.get()#获取参数
all_cmd = _path + ' ' + _cmd + ' ' + _value#获取所有的命令
log_text.insert(END, all_cmd + '\n', 'c') #插入命令
cmd_response = os.popen(all_cmd).readlines() #运行命令并获取返回内容
log_text.insert(END, ''.join(cmd_response), 'l') #将返回内容写入到下面的文本框
del cmd_response
path_label = Label(root,text = '命令路径(绝对路径):', font = f)#创目路径tag
path_label.grid(row = 0, column = 0, sticky = W) #使用grid布局方式
path_entry = Entry(root, font = f, width = 60, border = 2) #创建输入框
path_entry.grid(row = 0, column = 1, sticky = W)
path_btn = Button(root,text = '选择命令', font = f, width = 20, command = cmd_path)#使用上面定义的函数cmd_path执行按钮
path_btn.grid(row = 0, column = 2, sticky = W)
param_label = Label(root, text = '命令参数:', font = f)#参数tag
param_label.grid(row = 2, column = 0, sticky = W)
param_entry = Entry(root, font = f, width = 60, border = 2)#创建输入框
param_entry.grid(row = 2, column = 1, sticky = W)
run_btn = Button(root,text = '运行命令',font = f,command = cmd_run,width = 20)#使用上面定义的函数cmd_run执行按钮
run_btn.grid(row = 1, column = 2, sticky = W)
clear_btn = Button(root,text = '清屏', font = f, command = cmd_clear, width = 20)#使用上面定义的函数cmd_clear执行按钮
clear_btn.grid(row = 2, column = 2, sticky = W)
log_text = Text(root,font = f,width = 105, border = 2,) #文本框
log_text.grid(columnspan = 3, sticky = W)
root.mainloop()