类型错误:对象'TK'没有 len() 不断发生



使用tkinter时,我一直得到这个错误返回。它只出现在这个示例代码中,并且似乎起源于.pack()行。

Callback :
Traceback (most recent call last): 
File "c:UsersceliaDesktopZackAizackaitkinter.py", line 16, in <module> 
myPad.pack(root) 
File "C:UsersceliaAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 2398, in pack_configure 
+ self._options(cnf, kw)) 
File "C:UsersceliaAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1473, in _options 
cnf = _cnfmerge(cnf) 
File "C:UsersceliaAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 109, in _cnfmerge 
for c in _flatten(cnfs): 
TypeError: object of type 'Tk' has no len()

代码:

import random 

from tkinter import * 


while True : 
root = Tk()  

Greeting ="Dydh da !" 
choicelist = "language practise(A), positive message (B) or a idea for thought (C)"  
thing = ["You are doing well", "You look cute", "Keep smiling"] 

myPad= Label(root, text = Greeting) 
myPad2 = Label(root, text ="please type Yes to start") 

myPad.pack(root) 
myPad2.pack(root) 

e = Entry(root, borderwidth= 70) 
e.pack() 

varrr= e.get() 

if varrr == "Yes" or "yes": 
labelthree = Label(root, "choose a task") 
labelfour = Label(root, "language, happy message and ideas for thoughts") 
labelthree.pack(root) 
labelfour.pack(root) 

e2 = Entry(root,borderwidth =50) 
e.pack() 
else: 
labelstop =Label(root, text= "No problem") 

root.mainloop()

当使用pack()时,您不需要给出父元素。pack()自动将小部件放置在父元素中,父元素是您在初始化时分配给它的:

label = Label(parent, option...)

Label Docs
pack() Docs

还有,为什么要在while True:循环中包围整个应用程序?这通常是不受欢迎的,如果你能描述你想要做什么,我可以推荐一种不同的方法来实现你想要做的事情。

相关内容

  • 没有找到相关文章