IDLE 2.7.11/12 “NameError: global name 'Toplev

2019-03-04 10:58发布

I ran APT-GET update on a Debian based OS on Oracle VB. While it was running I messed around with some python code in IDLE 2.7.12 (I opened it with a terminal). After it finished updating, I tried saving my code. In the terminal that I opened IDLE with I got an error. It says this:

root@kali:~# idle

Idle opens, I load my code, I edit my code, then I click [FILE] [SAVE]

This happens

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1545, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/idlelib/ScriptBinding.py", line 140, in run_module_event
    filename = self.getfilename()
  File "/usr/lib/python2.7/idlelib/ScriptBinding.py", line 205, in getfilename
    self.editwin.io.save(None)
  File "/usr/lib/python2.7/idlelib/IOBinding.py", line 345, in save
    if self.writefile(self.filename):
  File "/usr/lib/python2.7/idlelib/IOBinding.py", line 378, in writefile
    chars = self.encode(self.text.get("1.0", "end-1c"))
  File "/usr/lib/python2.7/idlelib/IOBinding.py", line 450, in encode
    dialog = EncodingMessage(self.editwin.top, enc)
  File "/usr/lib/python2.7/idlelib/IOBinding.py", line 76, in __init__
    self.root = top = Toplevel(master)
NameError: global name 'Toplevel' is not defined

Picture of my terminal with the error

I have already tried restarting the VB, and I've uninstalled, autocleaned, then re-installed IDLE with no luck.

2条回答
虎瘦雄心在
2楼-- · 2019-03-04 11:37

open IDLE.
Options
Configure IDLE
General
set 'Default Source Encoding' : UTF-8

I'm running Python 2.7.12 on Windows 10 and also Ubuntu 16.04 this solved my save/crash IDLE issue on both OS's .

Why do I get the feeling they are trying to throw us off Python 2 to 3 ?! jk.

查看更多
老娘就宠你
3楼-- · 2019-03-04 11:46

The problem is a bug in the updated version of IDLE. Add the following near the top of .../Lib/idlelib/IOBinding.py, for instance before import tkFileDialog.

from Tkinter import *

The NameError occured in a function that displays a warning with the following message:

Non-ASCII found, yet no encoding declared. Add a line like
-*- coding: <enc> -*-
Choose OK to save this file as <enc>
Edit your general options to silence this warning

where <enc> will be an encoding such as utf-8.

It occurs to me that the message might be more helpful for beginners if it pointed to the Python Language Reference, Section 2.1.4., Encoding declarations

This bug is 2.x specific because in Python3, utf-8 is the default used in the absence of an encoding declaration.

查看更多
登录 后发表回答