Python Tkinter don create Button



我正在用Tkinter开发reStructuredText编辑器。如果我运行下面的代码,我会得到缩进错误。。

from Tkinter import *
from tkFileDialog import asksaveasfile as savefile
class RSTkinter:
    def __init__(self):
        self.pencere_load()
        self.araclar()
    def pencere_load(self):
        pencere.resizable(width=FALSE,height=FALSE)
        pencere.title("RSTkinter")
    def araclar(self):
        h1 = Button(text=u"Başlıklar",command=self.h1p)
        h1.place(rely=0.0)
        ..
        ..
        topic = Button(text="Topic",command=self.topic_p) # ..topic:: başlık nt içerik
        topic.place(rely=0.0,relx=0.63)
    def topic_p(self):
        topic = Toplevel()
        topic.title("RSTkinter - Not")
        topic.resizable(width=FALSE,height=FALSE)
        topic.geometry("200x140")
        topic_b_l = Label(topic,text=u"Topic başlığı: ")
        topic_b_l.place(relx=0.0,rely=0.0)
        self.topic_b = Text(topic)
        self.topic_b.place(relx=0.3,rely=0.0,width=130)
        topic_i_l = Label(topic,text=u"Topiç içeriği")
        topic_i_l.place(relx=0.0,rely=0.4)
        self.topic_i = Text(topic)
        self.topic_i.place(relx=0.3,rely=0.5,width=130)
        topic_y = Button(topic,text=u"Ekle",command=self.topic_yap)
        topic_y.place(relx=0.0,rely=0.2)
    def topic_yap(self):
        return self.metin.insert(END,"n.. topic:: %s nt%s"%(self.topic_b.get(1.0,END)),self.topic_i.get(1.0,END)))
pencere = Tk()
rst = RSTkinter()
mainloop()

完全错误:

File "RSTkinter15.py", line 85
topic = Button(text="Topic",command=self.topic_p) #.. ^
IndentationError: unexpected indent

我该怎么办?

如果错误消息告诉您有缩进错误,那么可以放心地假设这是真的。调试时,一个很好的经验法则是始终相信编译器/解释器告诉你的内容。

很可能您混合了空格和制表符——这是python的弱点之一。仔细检查该行和前一行是否使用了相同的空格和制表符组合。

例如,当几个人在一段代码上编程或用几个编辑器(使用不同的默认设置)编程时,这通常是一个问题:一个使用(双倍/四倍)间距,另一个使用制表符。为了解决这个问题,我总是使用编辑器的内置替换功能(使用正则表达式):

'    ' => 't'   # search four spaces, replace with tab
'  ' => 't'     # search two spaces, replace with tab

"全部替换"非常有用,但要小心:您不想更改太多!按此顺序(否则,您将把所有四倍间距更改为两个制表符)。

当我编写代码时,修复标识的最佳方法是转到问题上方的行,然后单击该行的末尾,这样你的curser就在那里了。然后按回车键,下一行就会出现正确的缩进。然后我所做的就是不断按下Delete按钮,将代码从下面移动到python shell为我制作的新缩进中。

最新更新