前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python实现图形界面执行linux命

python实现图形界面执行linux命

作者头像
py3study
发布2020-01-07 16:42:11
4K0
发布2020-01-07 16:42:11
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
[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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档