当运行直接与自动完成条目(与“直接”命令行参数)的窗口,进入正常工作和打字强调当上述项目建议的4硬编码字符串。
当从另一个窗口(使用“间接”命令行参数)双击事件后运行此窗口时,自动完成条目不起作用。 更新:更确切地说,自动完成在第一个窗口(而不是自动完成条目的窗口)上显示选项。
是什么导致了这种不一致?我怎样才能使它在两种情况下工作?
见MWE附:
from Tkinter import *
from ttk import Frame, Label, Style
class AutocompleteEntry(Entry):
def __init__(self, contacts, mainComposeMailWindow, *args, **kwargs):
Entry.__init__(self, *args, **kwargs)
self.contacts = contacts
self.mainComposeMailWindow = mainComposeMailWindow
self.var = self["textvariable"]
if self.var == '':
self.var = self["textvariable"] = StringVar()
self.var.trace('w', self.changed)
self.bind("", self.selection)
self.bind("", self.up)
self.bind("", self.down)
self.lb_up = False
def changed(self, name, index, mode):
words = self.comparison()
if words:
if not self.lb_up:
self.lb = Listbox()
self.lb.bind("", self.selection)
self.lb.bind("", 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, event):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.lb.destroy()
self.lb_up = False
self.icursor(END)
def up(self, event):
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, event):
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):
return [w for w in self.contacts if w.lower().startswith(self.var.get().lower())]
def get_content(self):
return self.var.get()
class AutoCompleteWindow:
def __init__(self):
autocomplete_choices = ['_This', '_order', '_is', '_important']
self.root = Tk()
self.root.minsize(300,300)
Label(self.root, text="Enter text:").grid(row=0)
self.autocomplete_entry = AutocompleteEntry(autocomplete_choices, self, self.root, bd = 2, width=50)
self.autocomplete_entry.grid(row=0, column=1)
self.root.mainloop()
def on_open_window(event):
AutoCompleteWindow()
def makeWindow():
global select
win = Tk()
frame3 = Frame(win)
frame3.pack()
scroll = Scrollbar(frame3, orient=VERTICAL)
select = Listbox(frame3, yscrollcommand=scroll.set, height=17, width=100)
select.bind("" , on_open_window)
scroll.config (command=select.yview)
scroll.pack(side=RIGHT, fill=Y)
select.pack(side=LEFT, fill=BOTH, expand=1)
return win
def setSelect() :
scrollbar_choices = ["first", "second", "third"]
select.delete(0,END)
for a_choice in scrollbar_choices:
select.insert(END, a_choice)
def intro_window():
win = makeWindow()
setSelect()
win.mainloop()
if __name__ == "__main__":
if sys.argv[1] == "indirect":
intro_window()
elif sys.argv[1] == "direct":
AutoCompleteWindow()
2015-07-11
zvisofer