Python缩进错误:

4

我已经尝试过notepad++和eclipse,但即使是这样,在第18行仍然显示缩进错误。 我不知道为什么会出现这样的错误...? 请帮帮我。

from brisa.core.reactors.qtreactor import QtReactor
reactor = QtReactor()
from brisa.core import config
from brisa.upnp.device import Device
from brisa.upnp.device.service import Service, StateVariable
class QtDevice(QtGui.QWidget):
     def __init__(self):

         QtGui.QWidget.__init__(self)
         self.verticalLayout = QtGui.QVBoxLayout(self)
         self.title = QtGui.QLabel("Qt Simple Device")
         font = QtGui.QFont()
         font.setPointSize(15)
         self.title.setFont(font)
         self.title.setAlignment(QtCore.Qt.AlignCenter)
         self.verticalLayout.addWidget(self.title)
         self.lineEdit = QtGui.QLineEdit(self)
         self.verticalLayout.addWidget(self.lineEdit)
         self.search_btn = QtGui.QPushButton("Start Device", self)
         self.verticalLayout.addWidget(self.search_btn)
         QtCore.QObject.connect(self.search_btn, QtCore.SIGNAL("clicked()"), self.start)
         self.stop_btn = QtGui.QPushButton("Stop Device", self)
         self.verticalLayout.addWidget(self.stop_btn)
         QtCore.QObject.connect(self.stop_btn, QtCore.SIGNAL("clicked()"), self.stop)
         self.lineEdit.setText(’My Generic Device Name’)
         self.root_device = None
         self.upnp_urn = ’urn:schemas-upnp-org:device:MyDevice:1def _add_root_device(self):
         project_page = ’http://brisa.garage.maemo.org’
         serial_no = config.manager.brisa_version.replace(’.’, ’’).rjust(7, ’0’)
         self.root_device = Device(self.upnp_urn,str(self.lineEdit.text()),
                                    manufacturer=’’,
                                    manufacturer_url=,
                                    model_description=’ ’

                                    model_name=’’,
                                    model_number=,
                                    model_url=,
                                    serial_number=)  


     def _add_services(self):
         service_name = ’MyService’
         service_type = ’urn:schemas-upnp-org:service:MyService:1’
         myservice = Service(service_name, service_type, ’’)
         var = StateVariable(self, "A_ARG_TYPE_Variable",True, False, "string")
         myservice.add_state_variable(var)
         self.root_device.add_service(myservice)

    def _load(self):
         self._add_root_device()
         self._add_services()
         def start(self):
         self.stop()
         self._load()
         self.root_device.start()
         reactor.add_after_stop_func(self.root_device.stop)  

     def stop(self):
         if self.root_device:
             self.root_device.stop()
             self.root_device = None

def main():
     qt_dev = QtDevice()
     qt_dev.show()
     reactor.main()
if __name__ == ’__main__’:
     main()          

1
告诉我们第18行是什么,并展示错误信息,怎么样? - Falmarri
1
这些奇怪的单引号是从哪里来的?如果它们在源代码中,它们会触发无数的语法错误。此外,在 _add_root_device 中的长 Device(...) 存在几个错误:缺少逗号和大多数关键字参数缺少值。 - user395760
@user597293:调试的第一步是假设警告是准确的。在缩进错误的情况下,只需删除所有前导缩进并重新插入您认为正确的缩进即可轻松解决问题。 - Bryan Oakley
1
“'”字符不是撇号。它在Python中是非法的。为什么代码中会出现它? - S.Lott
4个回答

13
在这种情况下,通常最好使用-t标志来运行Python:

-t :对不一致的制表符使用发出警告(-tt:发出错误)

这将有助于查找由意外使用制表符引起的缩进问题。

2
这是一个不太为人所知的技巧(除了在docs.python.org上第一次看到它之外),但非常有用。 - user395760

7
行(The row)
     self.verticalLayout.addWidget(self.lineEdit)

应该与其他行处于同一级别。

可能是因为您的编辑器混合使用了制表符和空格,导致您错过了这个问题。

如果您在自己的问题中点击“编辑”,您会发现这一行没有正确缩进。


1
如果您在自己的问题中点击“编辑”,您会发现这一行没有正确缩进。 - Andrea Spadaccini
1
你为什么把重要信息作为评论发布到你的答案中?你知道你可以编辑你的答案,对吧? - SilentGhost
我认为答案中的信息已经足够了。不过,我会将其添加到答案中,谢谢。 :) - Andrea Spadaccini
是的。你说得对,但我该如何让它正确呢?我在我的Notepad++中按顺序看到了它。 - user597293
我不知道你的编辑器如何工作,但我找到了这个问题,应该可以帮助你:https://dev59.com/InRB5IYBdhLWcg3w9L3n - Andrea Spadaccini
1
@user59793:下定决心,使用制表符或空格(空格在广泛遵循的编码风格[PEP 8]中更常见和推荐,因此您应该坚持使用它)。 - user395760

5

这是你在线上的示例引用:

self.lineEdit.setText(’My Generic Device Name’)

试一下这个:

from brisa.core.reactors.qtreactor import QtReactor
reactor = QtReactor()
from brisa.core import config
from brisa.upnp.device import Device
from brisa.upnp.device.service import Service, StateVariable
class QtDevice(QtGui.QWidget):
     def __init__(self):

         QtGui.QWidget.__init__(self)
         self.verticalLayout = QtGui.QVBoxLayout(self)
         self.title = QtGui.QLabel("Qt Simple Device")
         font = QtGui.QFont()
         font.setPointSize(15)
         self.title.setFont(font)
         self.title.setAlignment(QtCore.Qt.AlignCenter)
         self.verticalLayout.addWidget(self.title)
         self.lineEdit = QtGui.QLineEdit(self)
         self.verticalLayout.addWidget(self.lineEdit)
         self.search_btn = QtGui.QPushButton("Start Device", self)
         self.verticalLayout.addWidget(self.search_btn)
         QtCore.QObject.connect(self.search_btn, QtCore.SIGNAL("clicked()"), self.start)
         self.stop_btn = QtGui.QPushButton("Stop Device", self)
         self.verticalLayout.addWidget(self.stop_btn)
         QtCore.QObject.connect(self.stop_btn, QtCore.SIGNAL("clicked()"), self.stop)
         self.lineEdit.setText('My Generic Device Name')
         self.root_device = None
         self.upnp_urn = 'urn:schemas-upnp-org:device:MyDevice:1'


     def _add_root_device(self):
         project_page = 'http://brisa.garage.maemo.org'
         serial_no = config.manager.brisa_version.replace('.', '').rjust(7, '0')
         self.root_device = Device(self.upnp_urn,str(self.lineEdit.text()),
                                    manufacturer='',
                                    manufacturer_url=,
                                    model_description=' '

                                    model_name='',
                                    model_number=,
                                    model_url=,
                                    serial_number=)  


     def _add_services(self):
         service_name = 'MyService'
         service_type = 'urn:schemas-upnp-org:service:MyService:1'
         myservice = Service(service_name, service_type, '')
         var = StateVariable(self, "A_ARG_TYPE_Variable",True, False, "string")
         myservice.add_state_variable(var)
         self.root_device.add_service(myservice)

    def _load(self):
         self._add_root_device()
         self._add_services()
         def start(self):
         self.stop()
         self._load()
         self.root_device.start()
         reactor.add_after_stop_func(self.root_device.stop)  

     def stop(self):
         if self.root_device:
             self.root_device.stop()
             self.root_device = None

def main():
     qt_dev = QtDevice()
     qt_dev.show()
     reactor.main()
if __name__ == '__main__':
     main()   

它可以消除缩进错误。谢谢。现在它显示制造商网址的错误...但我还没有提供。所以也许我会修复它。 - user597293
1
@User59:为什么这个答案被接受了?它与所提出的问题关系不大。 - SilentGhost

3

看起来你使用了错误的单引号标记。你需要使用',而不是

不确定这是否是你的问题。


1
+1 给反对者计数 - 解释一下你们的想法。这不是 OP 所询问的确切错误,但如果它在实际源代码中也存在,那么很可能是另一个错误的来源。 - user395760
我进行了反对投票,因为这个答案并没有解决实际提问的问题。此外,回答者承认自己不知道这是否是真正的问题,这也是我投反对票的原因之一。我从未来有人遇到相同错误并来到这里寻求解决方案的角度考虑这个问题。这个答案能够解决缩进错误吗?很明显不能。 - Bryan Oakley
这个问题是关于语法错误的,而且就是一个语法错误。 - Falmarri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接