本文整理汇总了Python中tkinter.Listbox.activate方法的典型用法代码示例。如果您正苦于以下问题:Python Listbox.activate方法的具体用法?Python Listbox.activate怎么用?Python Listbox.activate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.Listbox
的用法示例。
在下文中一共展示了Listbox.activate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ListboxVidget
# 需要导入模块: from tkinter import Listbox [as 别名]
# 或者: from tkinter.Listbox import activate [as 别名]
#.........这里部分代码省略.........
# Set changing flag on
self._is_changing = True
# Get old active index
old_indexcur = self._indexcur
# Set resetting flag on if new and old indexes are equal
self._is_resetting = (index == old_indexcur)
# If notify events
if notify:
# Notify pre-change event
self.handler_notify(self.ITEMCUR_CHANGE_SOON, notify_arg)
# If old active index is valid
if self.index_is_valid(old_indexcur):
# Set old active item's background color to normal color
self._listbox.itemconfig(old_indexcur, background=self._normal_bg)
# Set old active item's foreground color to normal color
self._listbox.itemconfig(old_indexcur, foreground=self._normal_fg)
# Cache new active index
self._indexcur = index
# Clear listbox widget's selection
self._listbox.selection_clear(0, END)
# Set listbox widget's selection
self._listbox.selection_set(index)
# Set listbox widget's activated index
self._listbox.activate(index)
# If new active index is valid
if index != -1:
# Set new active item's background color to active color
self._listbox.itemconfig(index, background=self._active_bg)
# Set new active item's foreground color to active color
self._listbox.itemconfig(index, foreground=self._active_fg)
# If set focus
if focus:
# Set focus on the listbox widget
self._listbox.focus_set()
# If new active index is valid
if index != -1:
# Make the active item visible
self._listbox.see(index)
# If notify events
if notify:
# Notify post-change event
self.handler_notify(self.ITEMCUR_CHANGE_DONE, notify_arg)
# Set resetting flag off
self._is_resetting = False
# Set changing flag off
self._is_changing = False
def indexcur_set_by_event(
self,
示例2: AutocompleteEntry
# 需要导入模块: from tkinter import Listbox [as 别名]
# 或者: from tkinter.Listbox import activate [as 别名]
class AutocompleteEntry(Entry):
def __init__(self, *args, **kwargs):
Entry.__init__(self, width=100, *args, **kwargs)
self.focus_set()
self.pack()
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("<Right>", self.selection)
self.bind("<Up>", self.up)
self.bind("<Down>", self.down)
self.bind("<Return>", self.enter)
self.lb_up = False
self.lb = None
def enter(self, event):
print(event)
def changed(self, name, index, mode):
if self.var.get() == '':
if self.lb:
self.lb.destroy()
self.lb_up = False
else:
words = self.comparison()
if words:
if not self.lb_up:
self.lb = Listbox(master=root, width=100)
self.lb.bind("<Double-Button-1>", self.selection)
self.lb.bind("<Right>", self.selection)
self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())
self.lb_up = True
self.lb.delete(0, END)
for w in words:
self.lb.insert(END,w)
else:
if self.lb_up:
self.lb.destroy()
self.lb_up = False
def selection(self, _):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.lb.destroy()
self.lb_up = False
self.icursor(END)
def up(self, _):
if self.lb_up:
if self.lb.curselection() == ():
index = '0'
else:
index = self.lb.curselection()[0]
if index != '0':
self.lb.selection_clear(first=index)
index = str(int(index)-1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def down(self, _):
if self.lb_up:
if self.lb.curselection() == ():
index = '0'
else:
index = self.lb.curselection()[0]
if index != END:
self.lb.selection_clear(first=index)
index = str(int(index)+1)
self.lb.selection_set(first=index)
self.lb.activate(index)
def comparison(self):
q = self.var.get()
q = str(q.decode('utf8'))
for hit in searcher.search(qp.parse(q), limit=50):
if hit['author']:
yield '%s. "%s"' % (hit['author'], hit['title'])
else:
yield hit['title']