2012-02-07 44 views
0

我写了这个代码我想进入到文本框中的值传递给一个Python程序

app = Tk() 
app.title('Myapp') 
app.geometry('260x100+50+50') 

labelText =StringVar() 
labelText.set('Insert the version you want to check') 
label1 = Label(app, textvar=labelText, height=4) 
label1.pack() 

stringadacercare = StringVar(None) 
lastringa = Entry(app, textvar=stringadacercare) 
lastringa.place(x=30, y=40, width=200) 


progressbar = ttk.Progressbar(orient=HORIZONTAL, length=260, mode='determinate') 
progressbar.pack(side='bottom') 
progressbar.start() 

app.mainloop() 

我想通过文本框采取的输入进入这个程序,而不是的raw_input所以结合这进行测试V

import csv 
import re 
import os 
testV = raw_input('Insert the version you want to find: ') 
fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|') 
for row in fileobj: 

for x in row: 
    with open(x) as f: 
     for line in f: 

      if re.match(testV, line): 
       print 'The version match: '+ line 
      else: 
       print 'wrong version'  
    filesize= os.path.getsize(x) 
print 'The file size is :'+ str(filesize) +' bytes'; 

有人能帮我吗? 提前谢谢!!!!!

+0

对不起nassio,看着你的gui后,我意识到我的代码不是一个好主意。我删了它。 – joaquin 2012-02-07 17:38:23

+0

@Niall你可以更具体一些吗?没有复选标记.... – nassio 2012-02-07 20:58:41

+0

快速浏览http://stackoverflow.com/faq#howtoask。如果你接受答案,如果给别人更多的动力来帮助你:) – 2012-02-07 21:10:01

回答

0

你确定你要使用第二个脚本作为一个单独的程序吗?如果必须,您可以使用子进程模块将变量作为命令行参数传递。

安装使用按钮和回调像这样的GUI:

import subprocess 
from Tkinter import * 
import ttk 

def btnCallback(): 
    #this is the path to your other script 
    cmdpath = '/path/to/test.py' 

    #extract the textbox variable 
    var = stringadacercare.get() 

    progressbar.start() 

    #pass the variable to the command line 
    proc = subprocess.popen([cmdpath,var]) 

    #block while the subprocess executes 
    proc.wait() 

    progressbar.stop() 

app = Tk() 
app.title('Myapp') 
app.geometry('260x100+50+50') 

labelText =StringVar() 
labelText.set('Insert the version you want to check') 
label1 = Label(app, textvar=labelText, height=4) 
label1.pack() 

stringadacercare = StringVar(None) 
lastringa = Entry(app, textvar=stringadacercare) 
lastringa.place(x=30, y=40, width=200) 

progressbar = ttk.Progressbar(orient=HORIZONTAL, length=260, mode='determinate') 
progressbar.pack(side='bottom') 

#use a button to execute the subprocess 
buttonOK = Button(app,text='GO',command=btnCallback) 
buttonOK.pack(side='bottom') 

app.mainloop() 

重构你的测试脚本接受命令行参数。你也可以将它导入你的gui app并调用main函数,直接传递变量。

import csv 
import re 
import os 

#need the sys module to get the command line args 
import sys 

def main(testV): 
    fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|') 

    for row in fileobj: 
     for x in row: 
      with open(x) as f: 
       for line in f: 
        if re.match(testV, line): 
         print 'The version match: '+ line 
        else: 
         print 'wrong version' 
         filesize= os.path.getsize(x) 
         print 'The file size is :'+ str(filesize) +' bytes' 

if __name__=='__main__': 
    #get the first command line argument and pass it to main 
    testV = sys.argv[1] 
    main(testV) 

如果你想使用的测试程序作为一个模块,而不是通过命令行可以添加一个import语句为您的测试模块,并从按钮的回调调用它。

... 
import mytestmodule 

def btnCallback(): 
    #extract the textbox variable 
    var = stringadacercare.get() 

    progressbar.start() 

    #call the main function from the test module 
    mytestmodule.main(var) 

    progressbar.stop() 

... 
+0

太好了!!!我喜欢按钮的想法好多了,无论如何,请你解释一下为什么我应该把变量传递给命令行?和其他的事情,我打算做一个标准的窗口安装应用程序,因此我想知道如果你的代码将工作到一个标准的窗口(无蟒蛇)...无论如何非常感谢你! – nassio 2012-02-07 20:24:19

+0

您不必通过命令行传递它。首选的方法是将第二个例程用作模块。我选择显示命令行路由,因为它与您所执行的最接近。要将第二个例程作为[module](http://docs.python.org/tutorial/modules.html)使用,可以将它导入到GUI脚本中,并调用main函数,直接传递该变量。 – tharen 2012-02-07 21:16:59

+0

当你准备好了,你可以将它与[py2exe](http://www.py2exe.org/) – tharen 2012-02-07 21:25:33

相关问题