0

I have written the following code to create a gui in python and then using lambda i am calling another file. This code is showing some indent problem . roshan/pre.sh is the path of shell script from where I am calling another python file. I want to create multiple buttons like this and using a function I will call a different shell script.

There should be 5 buttons (named GRAPH1, GRAPH2, GRAPH3, GRAPH4, GRAPH5) and on each button click I wish to load another file where i will put my graphs.

For example, when I click on the GRAPH1 button another file which i have named image1.py will load and will show graphs.

#!/usr/bin/python

import Tkinter, subprocess

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

    clusterB = Tkinter.Button(self, text ="preprocessing",width=13,font = "Georgia 10 bold")
    clusterB.grid(row=5,sticky=Tkinter.W,padx=3)
    clusterB.config(command = lambda:clusters())

    def clusters():
    process = subprocess.Popen(["roshan/pre.sh"],shell=False,
        stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    while True:
        out = process.stdout.readline()
        if out == '' and process.poll() is not None:
            break
        print out

    return
if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('Results and Graphs')
    app.mainloop()
6
  • Please be more specific, what is your question? Commented Sep 2, 2015 at 7:37
  • Please edit the question to include this information (and then we can all delete our comments) Commented Sep 2, 2015 at 7:55
  • Can you post an error message if there is one? Commented Sep 2, 2015 at 8:57
  • @BobMarshall currently i am getting this error Traceback (most recent call last): File "./graph.py", line 61, in <module> app = simpleapp_tk(None) File "./graph.py", line 10, in init self.initialize() File "./graph.py", line 23, in initialize out = process.stdout.readline() NameError: global name 'process' is not defined Commented Sep 2, 2015 at 9:09
  • why are you using lambda? It's not related to the problem, but it's unnecessary extra code. Commented Sep 2, 2015 at 11:19

1 Answer 1

2

Instead of using a shell to open a python file, you can always just import it if it's in the same directory and use it just like a module.

import image1
thing = image1.function_in_image1()

You can also choose to run it directly by using os.

import os
os.system(r"C:\path\to\image1.py")

In your example, I found indenting problems. Change this:

def clusters():
process = subprocess.Popen(["roshan/pre.sh"],shell=False,
    stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
while True:
    out = process.stdout.readline()
    if out == '' and process.poll() is not None:
        break
    print out

return

to this:

def clusters():
    process = subprocess.Popen(["roshan/pre.sh"],shell=False,
        stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    while True:
        out = process.stdout.readline()
        if out == '' and process.poll() is not None:
            break
        print out

    return
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.