即使我已经从tkinter导入*,为什么我仍会收到此错误?

时间:2020-06-17 13:22:58

标签: python tkinter

from tkinter import *
from tkinter import StringVar
root = Tk()

root.geometry('500x400')

l2 = Label(root, text = 'USERNAME')
l3 = Label (root, text = 'PASSWORD')

l2.grid()
l3.grid(row=1)

user = Tk.SringVar()
password = Tk.StringVar()

e1 = Entry (root, textvariable=user)
e2 = Entry (root, textvariable=password)

e1.grid()
e2.grid()

root.mainloop()

回溯(最近通话最近):
用户= Tk.SringVar()
AttributeError:类型对象“ Tk”没有属性“ SringVar”

解决此问题可获得:

user = Tk.StringVar()
AttributeError:类型对象“ Tk”没有属性“ StringVar”

1 个答案:

答案 0 :(得分:0)

问题是您正在导入*,但仍在调用Tk。

from tkinter import *
#from tkinter import StringVar
root = Tk()

root.geometry('500x400')

l2 = Label(root, text = 'USERNAME')
l3 = Label (root, text = 'PASSWORD')

l2.grid()
l3.grid(row=1)

user = StringVar() #Typo here #Not
password = StringVar()

e1 = Entry (root, textvariable=user)
e2 = Entry (root, textvariable=password)

e1.grid()
e2.grid()

root.mainloop()

我测试了此代码,现在应该可以正常运行了。导入*时,它会导入所有函数和对象,因此没有理由调用Tk.StringVar()