python tkinter自错误
这可能是一件非常琐碎的事情 我得到以下错误python tkinter自错误,python,object,tkinter,self,Python,Object,Tkinter,Self,这可能是一件非常琐碎的事情 我得到以下错误 "self.d = {'Roosevelt Bridge': {"Monday": final()}} TypeError: final() missing 1 required positional argument: 'self'" 如果我不查字典,其他一切都会正常工作,不会出错。我不知道如何将dictionary类型的对象添加到tkinter中 谢谢你的澄清 import tkinter as tk def main(): roo
"self.d = {'Roosevelt Bridge': {"Monday": final()}}
TypeError: final() missing 1 required positional argument: 'self'"
如果我不查字典,其他一切都会正常工作,不会出错。我不知道如何将dictionary类型的对象添加到tkinter中
谢谢你的澄清
import tkinter as tk
def main():
root = tk.Tk()
root.title("class basic window")
root.geometry("250x350")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="ivory2", bd=2,
relief=tk.RAISED)
self.parent = parent
self.pack(fill=tk.BOTH, expand=1)
self.initUI()
def initUI(self):
self.grid()
self.optionList=["", "repair", "boat crossing", "mechinical
issue", "accident"]
self.dropVar=tk.StringVar()
self.dropVar.set(self.optionList[0])
self.dropMenu=tk.OptionMenu(self, self.dropVar,
*self.optionList)
self.dropMenu.config(width=10)
self.dropMenu.pack()
self.get=tk.Button(self, text="update", command=self.final)
self.get.pack()
#self.pack(fill=t.BOTH, expand=1)
def final(self):
if self.dropVar.get()==self.optionList[0]:
return ("1")
else:
return ("0")
self.d = {'Roosevelt Bridge': {"Monday": final()}}
print (self.d)
if __name__ == '__main__':
main()
这有一个非常简单的答案,你所需要做的就是将带有错误的行更改为
self.d={'Roosevelt Bridge':{“星期一”:最终(self)}
这完全是一样的,除了它说的是final()
我把它改成了final(self)
然后它工作得很好
希望这有帮助 我明白了。结果我被变量d的范围和函数final搞糊涂了。下面是我想要的工作方式的代码。谢谢你的提问和对我学习的贡献
import tkinter as tk
D={'Roosevelt Bridge':{“星期一”:1}
def main():
类应用程序(tk.Frame):
如果name='main':
main()请修复缩进。现在很难看到哪个代码在哪个范围/方法中。你真正想要实现什么。什么是
self.d={'Roosevelt Bridge':{“星期一”:final()}
应该再次调用final()
?如果final
不改变任何可能使递归停止的值,那么这怎么可能不会导致无限递归?{“星期一”:self.final}???@Lafexlos。对于初学者来说,我想我现在已经修复了缩进。除非我的缩进也错了。hi@Guydangerous99我试过你的代码,但它仍然给我一个错误“self.d={'Roosevelt Bridge':{“Monday”:final(self)}}NameError:name'self'没有定义“然而正如Lafexios所说,它可能与缩进有关。我移动了自我。。。。由于与if-else语句的对齐方式相同,并且没有给我任何错误,唯一的问题是我的print语句不工作,并且表示相同的“self-is-not-defined”。这给了我一个结论,我可以把这个“self.d”嵌套dict放在程序中的什么地方?再次感谢。
root = tk.Tk()
root.title("class basic window")
root.geometry("250x350")
root.config(background="LightBlue4")
app = Application(root)
root.mainloop()
def __init__(self, parent):
tk.Frame.__init__(self, parent, bg="ivory2", bd=2, relief=tk.RAISED)
self.parent = parent
self.pack(fill=tk.BOTH, expand=1)
self.initUI()
def initUI(self):
self.grid()
self.optionList=["", "repair", "boat crossing", "mechinical issue", "accident"]
self.dropVar=tk.StringVar()
self.dropVar.set(self.optionList[0])
self.dropMenu=tk.OptionMenu(self, self.dropVar, *self.optionList)
self.dropMenu.config(width=10)
self.dropMenu.pack()
self.get=tk.Button(self, text="update", command=self.update)
self.get.pack()
#self.pack(fill=t.BOTH, expand=1)
def final(self):
if self.dropVar.get()==self.optionList[0]:
return ("1")
else:
return ("0")
def update(self):
global D
D = {'Roosevelt Bridge': {"Monday": self.final()}}
print (D)