2011-02-02 106 views
3

我試過記事本++和eclipse,但即使如此,它顯示了第18行的縮進錯誤。我不知道,爲什麼它會拋出像這樣的錯誤...?請幫幫我。Python縮進錯誤:

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

如何告訴我們第18行是什麼,並告訴我們錯誤消息 – Falmarri 2011-02-02 18:35:54

+1

這些時髦的單引號來自哪裏?如果他們在源代碼中,他們會觸發語法錯誤。另外,`_add_root_device`中長的`Device(...)`有幾個錯誤:缺少逗號和大多數關鍵字參數缺少值。 – delnan 2011-02-02 18:38:04

+0

@ user597293:調試的第一步是假定警告是準確的。在縮進錯誤的情況下,簡單地刪除所有前導縮進並重新插入你認爲合適的縮進是非常簡單的。 – 2011-02-02 18:41:18

回答

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

 self.verticalLayout.addWidget(self.lineEdit) 

應該在其他行的同一水平。

您可能會錯過它,因爲您的編輯器混合了製表符和空格。

如果您在自己的問題中點擊「編輯」,您會看到該行沒有正確縮進。

+1

如果您在自己的問題中單擊「編輯」,您會看到該行沒有正確縮進。 – 2011-02-02 18:36:47

3

看起來您正在使用錯誤的單引號標記。您需要使用',而不是

不知道這是不是你的問題。

13

在這種情況下,通常是運行與-t標誌蟒蛇是一個好主意:

-t : issue warnings about inconsistent tab usage (-tt: issue errors)

這將有助於查找意外使用的選項卡導致的縮進問題。