【问题标题】:Python Tkinter don't create Button [duplicate]Python Tkinter不创建按钮[重复]
【发布时间】:2013-02-06 13:24:54
【问题描述】:

我正在使用 Tkinter 开发 reStructuredText 编辑器。如果我运行下面的代码,我会得到 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()

完全错误:

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

我该怎么办?

【问题讨论】:

  • 你一直在混合制表符和空格吗? link

标签: python tkinter restructuredtext


【解决方案1】:

这通常是一个问题,例如,与几个人一起编写一段代码或使用多个编辑器(具有不同的默认设置)进行编程:一个使用(双/四)间距,其他选项卡。为了解决这个问题,我总是使用编辑器的内置替换功能(带正则表达式):

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

'Replace All' 非常有用,但要小心:你不想改变太多!并按此顺序(否则您会将所有四倍间距更改为两个制表符)。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这些都与报告的缩进错误无关。
    • @BryanOakley 我重新编辑了这个答案,它现在是主题。
    【解决方案3】:

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

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

    【讨论】:

    • 只是补充一点,大多数编辑器都可以选择用空格替换所有制表符,反之亦然。比手动操作要快得多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2016-07-31
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多