我的python路径有问题

ddhy6vgd  于 2023-05-02  发布在  Python
关注(0)|答案(1)|浏览(73)

我使用MacOS莫哈韦,我刚开始在Python 3中学习tkinter。7.7,当我试图运行它(通过崇高的文本)它显示这个错误:

Traceback (most recent call last):
  File "/Users/julian/Documents/Ficheros_Python/tkinter.py", line 1, in <module>
    import tkinter as Tk
  File "/Users/julian/Documents/Ficheros_Python/tkinter.py", line 3, in <module>
    root = Tk()
TypeError: 'module' object is not callable
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/julian/Documents/Ficheros_Python/tkinter.py"]
[dir: /Users/julian/Documents/Ficheros_Python]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Users/julian/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin]

我曾经有anaconda,但我卸载了一段时间前,我有LaTeX安装,但我不知道为什么它在那里或为什么有xamarin,我需要一些帮助,请。
这也是我正在处理的代码(我也尝试过from tkinter import *,但错误是一样的):

import tkinter as Tk

root = Tk()

myLabel = Label(root, text="Hello world")
myLabel2 = Label(root, text="Hello world2")

myLabel.grid(row=0, column=0)
myLabel2.grid(row=1, column=5)

root.mainloop()
jq6vz3qz

jq6vz3qz1#

在语句import tkinter as Tk中,将 moduletkinter重命名为Tk。要启动tkinter,你需要 classTk,如上面的代码所示:

# here is your typo
from tkinter import Tk, Label

root = Tk()

myLabel = Label(root, text="Hello world")
myLabel2 = Label(root, text="Hello world2")

myLabel.grid(row=0, column=0)
myLabel2.grid(row=1, column=5)

root.mainloop()

您还需要将文件从tkinter.py重命名为与模块自导入不冲突的任何名称。

相关问题