2014-10-30 41 views
0

我正在創建一個GUI,其中我從用戶那裏獲得輸入(文件名)。使用python創建GUI(對文件進行操作)

然後我必須在該文件上運行一些shell腳本,並分別顯示每個腳本的輸出。

我有點卡在子進程模塊。每次運行時,都會報錯。

另一件事是,我如何創建一個全局字符串變量,因爲我無法訪問我在另一個函數中使用的變量。

CODE:

import sys 
from Tkinter import * 
from tkFileDialog import * 
import subprocess 
import os 

FileName = "" 
FilePath = "" 


def browse(): 

    Tk().withdraw() 
    FilePath = askopenfilename(filetypes = (("ApkFiles","*.apk"),("All files", "*"))) 
    print FilePath 
    Parts = FilePath.split("/") 
    FileName = Parts[-1] 
    name = str(FileName) 
    #print FileName 


def process(): 

    print FileName 
    #subprocess.call("cat ", FileName, shell=True) 
    #Content = open(self.filename,"r").readlines() 
    #print Content 
    #subprocess.call("command-name-here") 
    #output = subprocess.call(["/path/to/command", "arg1", "-arg2"]) 
    #subprocess.Popen("ls", stdout=subprocess.PIPE, shell=True) 
    #subprocess.call ("ls") 

    subprocess.call (['cp filename /Home/dinesh/Base/ApkFiles/']) 
    subprocess.call (['cd /Home/dinesh/Base'])l 
    subprocess.call (['./AppSplit 0.1 0.1 0.8']) 
    Status = subprocess.call (["AppSplit.sh" ,"filename"]) 
    #exit (Status) 
    # exit (0) 



gui = Tk() #create an object 
gui.title("Select an app to be clustered") 
gui.geometry("700x400") 

GuiLabel1 = Label(gui,text="Select an app to be clustered").grid(row=0 , column=4) 
GuiLabel2 = Label(gui,text="ApkFile").grid(row=3 ,column=3) 

bar=Entry(gui).grid(row=3, column=4) 


button1= Button(gui, text="Browse", command = browse).grid(row=3, column=5) 
button2= Button(gui, text="Cluster", command = process).grid(row=4, column=5) 

gui.mainloop() #necessary for windows 
+0

什麼是你'AppsSplit.sh'他們內心世界的關鍵字?如果可能的話,只需將你的shell腳本移植到Python。更容易維護。 – ghostdog74 2014-10-30 07:01:09

回答

0

對於subProcess.call你需要傳遞的參數爲每一個沒有在列表中一個字符串的字符串列表。

subprocess.call (['cp', filename, '/Home/dinesh/Base/ApkFiles/']) 
# or use Python 
shutil.copy(filename, '/Home/dinesh/Base/ApkFiles/') 

subprocess.call (['cd', '/Home/dinesh/Base']) 
# or use python 
os.chdir('/Home/dinesh/Base') 

subprocess.call (['./AppSplit', '0.1 0.1 0.8']) 

你可以把全局變量的函數定義之外,它的功能

globalvar1 = "This is global" 

# in the function 
def myfunc(): 
    global globalvar1 
+0

全球事情工作正常。現在我可以全局訪問文件名了。但我仍然無法將文件複製到上面提到的路徑。下面是錯誤!!!!!!! cp:不能創建常規文件'/ Home/dinesh/Base/ApkFiles /':沒有這樣的文件或目錄 Tkinter回調中的異常 回溯(最近調用最後一次): – phoenix 2014-10-30 10:41:28

+0

錯誤信息表明沒有這樣的文件或目錄 - 你需要確保該目錄存在,複製命令將不會創建目錄 – micebrain 2014-10-30 11:16:22

+0

是的,謝謝你的幫助 – phoenix 2014-10-30 14:17:56