0

I'm developing reStructuredText editor with Tkinter. If i run the below code, i'm getting IndentationError..

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

Full error:

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

How can I do?

user19911303
  • 449
  • 2
  • 9
  • 34

3 Answers3

1

If the error message is telling you that you have an indentation error, it's probably safe to assume that is true. A good rule of thumb when debugging is to always believe what the compiler/interpreter is telling you.

Most likely you've mixed spaces and tabs -- one of python's weaknesses. Double-check that you're using the same combination of spaces and tabs on that line and the one before it.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Just to add to this, most editors will have an option to replace all tabs with spaces and vice versa. Much quicker than doing it manually. – gozzilli Feb 22 '13 at 09:00
0

This is often a problem when, for example, programming with several people on one piece of code or programming with several editors (with different default settings): one uses (double/quadruple) spacing, the other tabs. To solve the problem, I always use the built-in replace function of editors (with regular expressions):

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

'Replace All' is then very useful, but be careful: you do not want to change too much! And in this order (otherwise you will change all the quadruple spacings to two tabs).

Nander Speerstra
  • 1,496
  • 6
  • 24
  • 29
-1

When I write code the best way to fix identation is to goto the line above the problem and click the end of the line so your curser is there. Then press enter and the correct indentation is put for you on the next line. All I do then is keep pressing Delete button to move the code from underneath to the new indentation that the python shell made for me.

Ken Roberts
  • 47
  • 1
  • 4