2013-02-21 131 views
0

我正在开发与Tkinter reStructuredText编辑器。如果我运行下面的代码,我得到IndentationError ..Python Tkinter不创建按钮

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 \n\t 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 \n\t%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 

我该怎么办?

+0

你是否在混合标签和空格? [链接](http://stackoverflow.com/search?q= [python] IndentationError) – 2013-02-21 13:48:30

回答

-1

当我写代码来解决identation最好的办法是转到上面的问题的行,然后单击行的末尾,以便你的光标是存在的。然后按回车键,并在下一行放置正确的缩进。我所做的就是不断按下Delete按钮将代码从下面移动到python shell为我制作的新缩进处。

+0

没有任何事情与报告的缩进错误有关。 – 2013-02-21 11:54:37

+0

@BryanOakley我重申了这个答案它现在是主题。 – 2013-02-27 18:04:31

1

如果错误消息告诉你,你有一个压痕错误,它可能是安全的假设这是真的。调试时的经验法则是始终相信编译器/解释器告诉你什么。

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

+0

只是为了补充一点,大多数编辑可以选择用空格替换所有的制表符,反之亦然。比手动操作快得多。 – gozzilli 2013-02-22 09:00:47

0

这通常是一个问题时,例如,编程与几个人在一张代码或编程与多个编辑器(具有不同的默认设置):一个用途(双层/四合一)间距,其他标签。为了解决这个问题,我总是使用内置的替换编辑功能(使用正则表达式):

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

“全部替换”,然后是非常有用的,但要小心:你不想改变太多!按照这个顺序(否则你会将所有的四倍间距改为两个制表符)。