0

I have the following code:

from Tkinter import *

    class App(Frame):
        def __init__(self, master):
            Frame.__init__(self, master)
            self.grid()
            self.create_widgets()

        def create_widgets(self):
            self.entryLabel = Label(self, text="Please enter a list of numbers (no commas):")
            self.entryLabel.grid(row=0, column=0, columnspan=2)

            self.listEntry = Entry(self)
            self.listEntry.grid(row=0, column=2, sticky=E)

            self.entryLabel = Label(self, text="Please enter an index value:")
            self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)

            self.indexEntry = Entry(self)
            self.indexEntry.grid(row=1, column=2)

            self.runBttn = Button(self, text="Run Function", command=self.psiFunction)
            self.runBttn.grid(row=2, column=0, sticky=W)

            self.answerLabel = Label(self, text="Output List:")
            self.answerLabel.grid(row=2, column=1, sticky=W)

        def psiFunction(self):
            j = int(self.indexEntry.get())
            valueList = list(self.listEntry.get())
            x = map(int, valueList)
            if x[0] != 0:
                x.insert(0, 0)
            rtn = []
            for n2 in range(0, len(x) * j - 2):
                n = n2 / j
                r = n2 - n * j
                rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
            self.answer = Label(self, text=rtn)
            self.answer.grid(row=2, column=2, sticky=W)


    if __name__ == "__main__":
        root = Tk()
        app = App(root)
        root.mainloop()

but every time I try to run it it gives me syntax errors. In particular, it tells me that there are issues with the indentation (unexpected indentation). I can't run it in IDLE since the mainloop will create problems, and I seem to run into errors trying to run it through the terminal and even Komodo. Does anyone have a surefire, step by step way to get it to run? Are there really syntax errors? Thanks.

5
  • And what are the syntax errors? Commented Jul 26, 2013 at 0:37
  • @mbdavis Edited. They're indentation errors. Commented Jul 26, 2013 at 0:43
  • @Stopwatch Class and if name == "main": should be at main level, so unindent everything below the import one level Commented Jul 26, 2013 at 0:46
  • @mbdavis Thanks so much, I see that's what jh314 did as well. Commented Jul 26, 2013 at 0:51
  • @Stopwatch no problems! Commented Jul 26, 2013 at 1:03

1 Answer 1

2

It appears that you have some indentation issues. Python isn't free form, so you need to pay attention to indentation:

from Tkinter import *

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.entryLabel = Label(self, text="Please enter a list of numbers (no commas):")
        self.entryLabel.grid(row=0, column=0, columnspan=2)

        self.listEntry = Entry(self)
        self.listEntry.grid(row=0, column=2, sticky=E)

        self.entryLabel = Label(self, text="Please enter an index value:")
        self.entryLabel.grid(row=1, column=0, columnspan=2, sticky=E)

        self.indexEntry = Entry(self)
        self.indexEntry.grid(row=1, column=2)

        self.runBttn = Button(self, text="Run Function", command=self.psiFunction)
        self.runBttn.grid(row=2, column=0, sticky=W)

        self.answerLabel = Label(self, text="Output List:")
        self.answerLabel.grid(row=2, column=1, sticky=W)

    def psiFunction(self):
        j = int(self.indexEntry.get())
        valueList = list(self.listEntry.get())
        x = map(int, valueList)
        if x[0] != 0:
            x.insert(0, 0)
        rtn = []
        for n2 in range(0, len(x) * j - 2):
            n = n2 / j
            r = n2 - n * j
            rtn.append(j * x[n] + r * (x[n + 1] - x[n]))
        self.answer = Label(self, text=rtn)
        self.answer.grid(row=2, column=2, sticky=W)


if __name__ == "__main__":
    root = Tk()
    app = App(root)
    root.mainloop()
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.