0

我有以下代码:

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

但每次我尝试运行它时都会出现语法错误。特别是,它告诉我缩进存在问题(意外缩进)。我无法在 IDLE 中运行它,因为 mainloop 会产生问题,而且我似乎在尝试通过终端甚至 Komodo 运行它时遇到了错误。有没有人有把握,一步一步让它运行?真的有语法错误吗?谢谢。

4

1 回答 1

2

看来您有一些缩进问题。Python 不是自由格式,所以需要注意缩进:

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()
于 2013-07-26T00:48:43.830 回答