weixin_39605414 2020-11-20 23:28
浏览 0

Preventing Inconsistent use of tabs and spaces error

Original report by Taz (Bitbucket: tazr42 GitHub: tazr42 ).

"inconsistent use of tabs and spaces in indentation" is one of the most frustrating error messages for a class of students where you can't see what is happening and why.

Is it possible to do the following: * Filter clipboard input replacing tabs with spaces * Scan file on load replacing tabs with spaces

This would make it easier to see without too much configuration (given it's on 100s of computers) why the code is wrong.

To recreate, try the following (yes, it's weird, but it simulates the copy and paste that happens in classes). * Open Word * Type, in Word: if i % 2 == 0: print(i) * Open Thonny, * Type, in Thonny: for i in range(10): * Then copy and paste the code from Word into Thonny.

It will look valid, but complain about inconsistent tabs and spaces. If you manually copy it then there's no problem. For a class of 11 year olds trying to figure out what coding is (some of whom struggle to spell 'range'), this can be frustrating.

I've got what I'm looking for by adding a line to the CodeView class in function "_on_text_changed": self.text.replace("1.0", tk.END, self.text.get("1.0", tk.END).replace('\t', ' '))

I appreciate this isn't the best solution as it's doing a search and replace on the entire file, every keystroke, but it does make the above scenario clear on why it's not working.

Any other suggested workarounds welcome.

该提问来源于开源项目:thonny/thonny

  • 写回答

8条回答 默认 最新

  • weixin_39605414 2020-11-20 23:28
    关注

    Thanks for the suggestion!

    I think I'll modify thonny.txtextext.EnhancedText such that with suitable constructor argument it will perform the replacement in the low-level text insertion method.

    评论

报告相同问题?