ImportError: cannot import name 'Tk'
ImportError: cannot import name 'Tk'
我在 python ubuntu 上写了一个程序,但是这几天我遇到了这个非常恼人的问题,
Traceback (most recent call last):
File "/home/tansen/Documents/python2/button25_01JanV1.py", line 1, in <module>
import Tkinter,ttk
File "/home/tansen/Documents/python2/Tkinter.py", line 1, in <module>
from Tkinter import Tk, Label, Button
ImportError: cannot import name 'Tk'
之前这个程序运行没问题,我运行好几次程序都成功了
import tkinter.ttk
from tkinter import *
def viewFile():
pass
if __name__ == '__main__':
root = Tk()
step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold italic")
step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)
Button(step, text="OpenFile", font = "Arial 8 bold italic", activebackground="turquoise", width=30, height=5, command=viewFile).grid (row= 6, column =3)
Button(step, text="Exit", font = "Arial 8 bold italic", activebackground="turquoise", width=20, height=5, command=root.quit).grid (row= 6, column =5)
tex = Text(master=root) # TextBox For Displaying File Information
scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
scr.grid(row=8, column=2, rowspan=15, columnspan=1, sticky=NS)
tex.grid(row=8, column=1, sticky=E)
tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))
root.mainloop()
根据专家的建议,我将文件重命名为 Tkinter.py,并将大写字母从 'T' 更改为 't'。之后我的程序 运行ning 成功,但又产生了另一个问题。按下退出按钮无效。
你能帮忙成功解决这个问题吗?
您在同一目录中调用了一个文件 Tkinter.py
,重命名它并删除 .pyc
/home/tansen/Documents/python2/Tkinter.py <- importing from this not the module
您还在使用 python 2 的 Tkinter 的导入语法,请使用:
from tkinter import ttk, Text, Button, LabelFrame, VERTICAL, E, NS, Scrollbar, Tk
确保您要导入的文件与您的程序位于同一文件夹中。然后,删除 pycache 文件夹(或在 Ubuntu 上调用的任何内容,我正在使用 Windows。它将是 "Compiled Python File")。这可能会损坏并阻止您的程序正常工作。如果这不起作用,那么恐怕我没有主意了。
我在 python ubuntu 上写了一个程序,但是这几天我遇到了这个非常恼人的问题,
Traceback (most recent call last):
File "/home/tansen/Documents/python2/button25_01JanV1.py", line 1, in <module>
import Tkinter,ttk
File "/home/tansen/Documents/python2/Tkinter.py", line 1, in <module>
from Tkinter import Tk, Label, Button
ImportError: cannot import name 'Tk'
之前这个程序运行没问题,我运行好几次程序都成功了
import tkinter.ttk
from tkinter import *
def viewFile():
pass
if __name__ == '__main__':
root = Tk()
step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold italic")
step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)
Button(step, text="OpenFile", font = "Arial 8 bold italic", activebackground="turquoise", width=30, height=5, command=viewFile).grid (row= 6, column =3)
Button(step, text="Exit", font = "Arial 8 bold italic", activebackground="turquoise", width=20, height=5, command=root.quit).grid (row= 6, column =5)
tex = Text(master=root) # TextBox For Displaying File Information
scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
scr.grid(row=8, column=2, rowspan=15, columnspan=1, sticky=NS)
tex.grid(row=8, column=1, sticky=E)
tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))
root.mainloop()
根据专家的建议,我将文件重命名为 Tkinter.py,并将大写字母从 'T' 更改为 't'。之后我的程序 运行ning 成功,但又产生了另一个问题。按下退出按钮无效。
你能帮忙成功解决这个问题吗?
您在同一目录中调用了一个文件 Tkinter.py
,重命名它并删除 .pyc
/home/tansen/Documents/python2/Tkinter.py <- importing from this not the module
您还在使用 python 2 的 Tkinter 的导入语法,请使用:
from tkinter import ttk, Text, Button, LabelFrame, VERTICAL, E, NS, Scrollbar, Tk
确保您要导入的文件与您的程序位于同一文件夹中。然后,删除 pycache 文件夹(或在 Ubuntu 上调用的任何内容,我正在使用 Windows。它将是 "Compiled Python File")。这可能会损坏并阻止您的程序正常工作。如果这不起作用,那么恐怕我没有主意了。