我有一个小 GUI,它有一个这样的自动完成设置:
completion_liststore = create_completion_model()
completion = Gtk.EntryCompletion()
completion.set_model(completion_liststore)
completion.set_text_column(0)
completion.set_match_func(match_anywhere, None)
builder.get_object('student_change').set_completion(completion)
completion.connect('match-selected', self.populate_main)
在我的处理程序类中,完成模型和匹配如下所示:
def create_completion_model():
db = sqlite3.connect('gradebook.db')
cursor = db.cursor()
cursor.execute('SELECT * from students')
students = cursor.fetchall()
names = Gtk.ListStore(str)
for student in students:
names.append([student[5]])
cursor.close()
return names
def match_anywhere(completion, entrystr, iter, data):
modelstr = completion.get_model()[iter][0]
return entrystr in modelstr.lower()
非常直截了当,效果很好。我想做的只是直接调用我的 populate_main 函数,但它似乎只能通过我的自动完成来工作。我的 populate_main 函数如下所示:
def populate_main(self, completion, treemodel, treeiter):
name = treemodel[treeiter][completion.get_text_column()]
db = sqlite3.connect('gradebook.db')
db.row_factory = dict_factory
cursor = db.cursor()
t = (name,)
cursor.execute('SELECT * from students WHERE name=?', t)
.... <-- and so on, filling in my form
有没有办法重用这个函数,而不仅仅是复制/粘贴到一个新函数中,只是将名称发送给函数