python實現圖形界面執行linux命令

   學習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()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章