我得到了下面的代碼有問題的警告之外定義:的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 –