成功执行Tkinter代码(Python 2.7)

时间:2013-07-26 00:34:21

标签: python tkinter

我有以下代码:

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运行它。有没有人有一个确定的,一步一步让它运行?真的有语法错误吗?感谢。

1 个答案:

答案 0 :(得分: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()