我无法引用框架内创建的Toplevel。
我还是比较新的python所以任何帮助都会很棒。
当searchList(Entry)尝试将数据导入contactMenu(Toplevel)实例时,会出现问题。
from Tkinter import *
emailList = ['one.two@gmail.com','two.three@gmail.com','five.six@gmail.com','seven.eight@gmail.com']
class searchList(Entry):
def set_completion_list(self, completion_list):
self._completion_list = completion_list
self._hits = []
self._hit_index = 0
self.position = 0
self.bind('<KeyRelease>', self.handle_keyrelease)
def autocomplete(self, delta=0):
"""autocomplete the Entry, delta may be 0/1/-1 to cycle through possible hits"""
if delta: # need to delete selection otherwise we would fix the current position
self.delete(self.position, END)
else: # set position to end so selection starts where textentry ended
self.position = len(self.get())
# collect hits
_hits = []
_hits.sort(key=str.lower)
for element in self._completion_list:
if element.startswith(self.get().lower()):
_hits.append(element)
# if we have a new hit list, keep this in mind
if _hits != self._hits:
self._hit_index = 0
self._hits=_hits
# only allow cycling if we are in a known hit list
if _hits == self._hits and self._hits:
self._hit_index = (self._hit_index + delta) % len(self._hits)
# now finally perform the auto completion
if self._hits:
contactsMenu.displayField.delete(0, END)
#contacts.displayField.insert(0,self._hits)
self.select_range(self.position,END)
for items in range(0, len(self._hits)):
contactsMenu.displayField.insert(0,self._hits[items])
def handle_keyrelease(self, event):
"""event handler for the keyrelease event on this widget"""
if event.keysym == "BackSpace":
contactsMenu.displayField.delete(self.index(INSERT), END)
self.position = self.index(END)
self.autocomplete()
if event.keysym == "Left":
if self.position < self.index(END): # delete the selection
contactsMenu.displayField.delete(self.position, END)
self.autocomplete()
else:
self.position = self.position-1 # delete one character
contactsMenu.displayField.delete(self.position, END)
self.autocomplete()
if event.keysym == "Right":
self.position = self.index(END) # go to end (no selection)
if event.keysym == "Down":
self.autocomplete(1) # cycle to next hit
if event.keysym == "Up":
self.autocomplete(-1) # cycle to previous hit
# perform normal autocomplete if event is a single key or an umlaut
if len(event.keysym) == 1:
self.autocomplete()
class mainApp(object):
def __init__(self, master, content):
self.master = master
master.title("EMailer")
bgColour = "white"
#Setting up Frames
#Main Frame
self.mainFrame = Frame(self.master, padx = 10, pady = 10, bg = bgColour)
self.mainFrame.pack(fill = BOTH, expand = True)
self.toButton = Button(self.mainFrame, command=self.startContacts, borderwidth = 1, padx = 8, pady = 5, text = "To..", bg = bgColour)
self.toButton.pack(padx = 5, pady = 2)
def startContacts(self):
contactsMenu = Contacts(Toplevel(self.master), emailList)
class Contacts:
def __init__(self, master, content):
self.master = master
master.title('Contacts')
master.geometry('600x600+600+200')
self.bgColour = "white"
self.mainFrame = Frame(self.master, padx=10, pady=10, bg=self.bgColour)
self.mainFrame.pack(fill=BOTH, expand=True)
self._content = content
# Top and Bottom Frames
self.topFrame = Frame(self.mainFrame, bg='blue')
self.topFrame.pack(side=TOP, fill=X, expand=False, anchor=NE)
self.midFrame = Frame(self.mainFrame, bg='red')
self.midFrame.pack(fill=BOTH)
self.botFrame = Frame(self.mainFrame, bg='green')
self.botFrame.pack(side=BOTTOM, pady=10, fill=X, expand=False, anchor=SW)
self.topLeftFrame = Frame(self.topFrame, bg='pink', width=440, height=35)
self.topLeftFrame.propagate(0)
self.topLeftFrame.pack(side=LEFT)
self.topRightFrame = Frame(self.topFrame, bg='orange')
self.topRightFrame.pack(side=RIGHT)
self.botLeftFrame = Frame(self.botFrame, bg='pink')
self.botLeftFrame.pack(side=LEFT)
self.botRightFrame = Frame(self.botFrame, bg='orange')
self.botRightFrame.pack(side=LEFT)
self.searchBar = searchList(self.topLeftFrame)
self.searchBar.set_completion_list(emailList)
self.searchBar.pack(anchor=SE, padx = 10, pady = 8)
self.searchButton = Button(self.topRightFrame, text="Search", padx = 30, pady = 4)
self.searchButton.pack(padx = 5, pady = 5)
# Insert text field of some sort
self.scrollBar = Scrollbar(self.midFrame)
self.scrollBar.pack(side=RIGHT, fill=Y)
self.displayField = Listbox(self.midFrame, selectmode=MULTIPLE, bg="purple", yscrollcommand=self.scrollBar.set,font='Calibri', height=23, width=92)
for items in self._content:
self.displayField.insert(END, str(items))
self.displayField.pack(side=LEFT, padx=5, pady=8)
self.scrollBar.config(command=self.displayField.yview)
self.selectButton = Button(self.botLeftFrame, command=self.callBack, text="Select", padx = 30, pady = 4)
self.selectButton.pack(padx = 5, pady = 5)
self.cancelButton = Button(self.botRightFrame, command=self.closeWindow, text="Cancel", padx = 30, pady = 4)
self.cancelButton.pack(padx = 5, pady = 5)
self.searchBar.focus_set()
def closeWindow(self):
self.master.destroy()
def callBack(self):
pass
if __name__ =='__main__':
root = Tk()
app = mainApp(root, emailList)
root.mainloop()