Python 使用 PyCharm 导入 Tkinter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39195711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:59:00  来源:igfitidea点击:

Tkinter import with PyCharm

pythontkinterpycharm

提问by PyDer

I want to create a tkinter window using pycharm:

我想使用 pycharm 创建一个 tkinter 窗口:

from tkinter import *

root = Tk()

root.mainloop()

Apparently PyCharm tells me that from tkinter import *is an unused import statement, and root = Tk()is an unresolved reference. What's confusing me is that the code works completely fine, a tkinter window shows up, no errors.

显然 PyCharm 告诉我这from tkinter import *是一个未使用的导入语句,并且root = Tk()是一个未解析的引用。让我感到困惑的是代码完全正常,显示了一个 tkinter 窗口,没有错误。

How do I fix this?

我该如何解决?

Edit:PyCharm shows these error whenever I import any other library I have.

编辑:每当我导入我拥有的任何其他库时,PyCharm 都会显示这些错误。

回答by Dennis Simiyu

from Tkinter import * 

root = Tk()

thislabel = Label(root, text = "This is an string.")

thislabel.pack()

root.mainloop()

Use Tkinternot tkinter

Tkinter不使用tkinter

回答by RIPPLR

from tkinter import*

从 tkinter 进口*

works just fine. You just have to go to the next line and type something along the lines of

工作得很好。你只需要转到下一行并沿着

tk = Tk()

tk = tk()

or any tkinter code and it will recognize it and work just fine.

或任何 tkinter 代码,它会识别它并且工作得很好。

from tkinter import*
tk = Tk()
btn = Button(tk, text="Click Me")
btn.pack()
tk.mainloop()

Does that code above work?

上面的代码有效吗?

Hope this helps

希望这可以帮助

回答by PyDer

In the end I managed to fix this problem myself, here's what I did:

最后我自己设法解决了这个问题,这就是我所做的:

  • Deleted the ".idea" file associated with the project.
  • In PyCharm: File >> Open >> "path to project" >> Ok(reopen project)
  • 删除了与项目关联的“.idea”文件。
  • 在 PyCharm 中:文件>>打开>>“项目路径”>>确定(重新打开项目)

Now it it looks as normal as it was before.

现在它看起来和以前一样正常。

回答by A.Eckert

I could solve it by doing the following

我可以通过执行以下操作来解决它

  • Delete the .idea file.
  • Delete the __py_cache__file.
  • 删除 .idea 文件。
  • 删除__py_cache__文件。