Visual Studio - NameError:未定义名称“Tk" [英] Visual Studio - NameError: name 'Tk' is not defined

查看:68
本文介绍了Visual Studio - NameError:未定义名称“Tk"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Visual Studio 社区 2015 中的 python Tkinter 模块显示一个简单的窗口.每当我尝试时,我都会收到一条错误消息代码如下:

I'm trying to display a simple window using the python Tkinter module in visual studio community 2015. Whenever I tried I get an error message Here is the code:

  from tkinter import *

  root = Tk()
  theLabel = Labe1(root, text="This is too easy")
  theLabel1.pack()
  root.mainloop()

错误信息如下:

NameError: name 'Tk' is not defined

我该如何解决这个问题?

How do I solve this problem?

推荐答案

如果你的 pythonpath 中有一些名为tkinter"的其他模块,你会得到这个结果.例如,如果您将程序命名为tkinter.py",或者名为tkinter.py"的文件位于您的路径中.

You will get this result if you have some other module in your pythonpath named "tkinter". For example, if you name your program "tkinter.py", or if a file named "tkinter.py" is somewhere in your path.

解决方法是简单地重命名您的文件.当您执行导入 tkinter"时,它会导入您的文件而不是 tkinter 模块.

The fix is to simply rename your file. When you do "import tkinter", it's importing your file rather than the tkinter module.

检查实际导入内容的一种简单方法是执行以下操作:

An easy way to check what was actually imported is to do this:

import tkinter
print("the imported file is", tkinter.__file__)

这篇关于Visual Studio - NameError:未定义名称“Tk"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆