我得到了下面的代码有问题的警告之外定义:的Python - 实例属性__init __()
from tkinter import *
from tkinter import ttk
class Autocomplete(Frame, object):
def __init__(self, *args, **kwargs):
super(Autocomplete, self).__init__(*args, **kwargs)
self.list = []
def build(self, width, height, entries):
# I get the warning for the following 8 lines:
self._entries = entries
self.listbox_height = height
self.entry_width = width
self.text = StringVar()
self.entry = ttk.Entry(self, textvariable=self.text, width=self.entry_width)
self.frame = Frame(self)
self.listbox = Listbox(self.frame, height=self.listbox_height, width=self.entry_width)
self.dropdown = Listbox(self.frame, height=self.listbox_height, width=self.entry_width, background="#cfeff9",
takefocus=0)
self.entry.pack()
self.frame.pack()
self.listbox.grid(column=0, row=0, sticky=N)
self.dropdown.grid(column=0, row=0, sticky=N)
self.dropdown.grid_forget()
return self
root = Frame(Tk())
autocomplete = Autocomplete(root).build(74, 10, entries)
root.pack()
autocomplete.pack()
mainloop()
我应该如何解决这个问题?我尝试将所有内容都移动到init,但是在创建Autocompelete对象的行中传递参数时出现了一些错误。所以请给我提供我必须做的所有改变。不只是像你必须移动它们一样。 我可以通过添加8条将所有变量赋值为None的定义行来修复警告,但我认为这是一个很愚蠢的解决方案。那么,正确的做法是什么?
*“我可以修复警告通过添加8个定义行没有分配到所有的变量” * ...这是警告告诉你要做什么。通常情况下,在'__init__'方法中未定义的对象上突然出现属性会使调试更加困难。 – CoryKramer
也是,这非常依赖你使用的棉绒。 – turbulencetoo
@turbulencetoo我用pycharm –