简体   繁体   English

PyCharm中的tkinter“未定义tkinter”

[英]tkinter "tkinter is not defined" in PyCharm

So im trying to use Tkinter, and it only works in the interpreter thing.所以我尝试使用 Tkinter,它只适用于解释器。 (image1) enter image description here (image1)在此输入图片说明

When i run the exact same lines in PyCharm i get this error当我在 PyCharm 中运行完全相同的行时,我收到此错误
x = tkinter.Tk() NameError: name 'tkinter' is not defined

Using python3.8.(i think)使用 python3.8。(我认为)

import tkinter as tk
gui = Tk()

#may this help #这可能有帮助

Try doing from tkinter import tk .尝试执行from tkinter import tk This is the middle ground between having to type out tkinter.Tk() or doing Tk() , which isn't really recommended.这是必须输入tkinter.Tk()或执行Tk()之间的中间地带,这并不是真正推荐的。 If you do from tkinter import tk , you will only have to do x=tk.Tk()如果您执行from tkinter import tk ,则只需执行x=tk.Tk()

This is very simple to resolve in PyCharm.这在 PyCharm 中很容易解决。 Do this:做这个:

import tkinter as tk

x = tk.Tk()

However, I wouldn't use x, but something like "root," "win," or "main."但是,我不会使用 x,而是使用“root”、“win”或“main”之类的东西。 The reason being that x could be confused with a similarly worded variable you have in your module.原因是 x 可能会与模块中的类似措辞变量混淆。

Pycharm and the python interpreter aren't same, the pycharm works in another python interpreter. Pycharm 和 python 解释器不一样,pycharm 在另一个 python 解释器中工作。 It is on...它开着...

C:\Users\{username}\PycharmProjects\{project name}\venv\Scripts\python.exe

If it does work on that interpreter I don't know what.如果它确实适用于那个口译员,我不知道是什么。 If it does not consider upgrade the python interpreter (Find how in google)如果不考虑升级python解释器(在google中查找方法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM