我有一个带有组合框和列表框的简单GUI,它依赖于组合框。列表框具有自动完成功能。现在我想将组合框的选择以及列表框的选择传递给函数。这是我到目前为止的代码:
from Tkinter import *
import ttk, os, re
root = Tk()
root.minsize(500,300)
root.maxsize(550,310)
def go(Arg1, Arg2):
print "works" + Arg1 + Arg2
class AutocompleteEntry(Entry):
def __init__(self, lista, *args, **kwargs):
Entry.__init__(self, *args, **kwargs)
self.lista = lista
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.lb_up = False
def changed(self, name, index, mode):
if self.var.get() == '':
self.lb.destroy()
self.lb_up = False
else:
words = self.comparison()
print words
if words:
if not self.lb_up:
self.lb = Listbox()
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, event):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.list_out = self.lb.get(ACTIVE)
self.lb.destroy()
self.lb_up = False
self.icursor(END)
return self.list_out
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):
pattern = re.compile(self.var.get() + '.*')
return [w for w in self.lista if re.match(pattern, w)]
class MyListbox:
lista = ['a', 'actions', 'additional', 'also', 'an', 'and', 'angle', 'are', 'as', 'be', 'bind', 'bracket', 'brackets', 'button', 'can', 'cases', 'configure', 'course', 'detail', 'enter', 'event', 'events', 'example', 'field', 'fields', 'for', 'give', 'important', 'in', 'information', 'is', 'it', 'just', 'key', 'keyboard', 'kind', 'leave', 'left', 'like', 'manager', 'many', 'match', 'modifier', 'most', 'of', 'or', 'others', 'out', 'part', 'simplify', 'space', 'specifier', 'specifies', 'string;', 'that', 'the', 'there', 'to', 'type', 'unless', 'use', 'used', 'user', 'various', 'ways', 'we', 'window', 'wish', 'you']
def __init__(self, parent, title):
self.parent = parent
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW", self.closes)
self.cities = ['New York', 'Vienna', 'Miami', 'Oslo']
self.establishment()
def combobox_handler(self, event):
current = self.combobox.current()
print self.combobox.get()
self.combo_out = self.combobox.get()
self.entNumber.delete(0, END)
print self.lista
self.entNumber.delete(0, END)
self.entNumber = AutocompleteEntry(self.lista, self.parent)
self.entNumber.place(x=50, y=100)
return self.combo_out
def establishment(self):
mainFrame = Frame(self.parent)
mainFrame.pack(fill=BOTH, expand=YES)
fr_left = Frame(mainFrame, bd=10)
fr_left.pack(fill=BOTH, expand=YES, side=LEFT)
self.combobox = ttk.Combobox(fr_left, values=self.cities)
self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)
self.combobox.pack()
fr_right = Frame(mainFrame, bd=10)
fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)
fr_up = Frame(fr_right)
fr_up.pack(side=TOP, expand=YES)
self.entNumber = Entry()
self.entNumber.place(x=50, y=100)
labb=Button(fr_left,text="OK",command= lambda: go(self.combo_out, AutocompleteEntry.list_out), bg="green")
labb.pack(side=BOTTOM, fill=X, pady=5)
def closes(self, event=None):
self.parent.destroy()
if __name__ == '__main__':
app = MyListbox(root, "Main Window")
root.mainloop()
传递组合框的变量不是问题,但传递列表框的变量对我来说很棘手,因为它来自不同的类。以下是选择列表框的部分,我想传递list_out
:
def selection(self, event):
if self.lb_up:
self.var.set(self.lb.get(ACTIVE))
self.list_out = self.lb.get(ACTIVE)
self.lb.destroy()
self.lb_up = False
self.icursor(END)
return self.list_out
复制到您的系统时,代码可以正常工作。
答案 0 :(得分:0)
首先,请勿使用lambda
。 lambda
在真正需要时非常有用,但在这种情况下并非如此。它所做的就是增加代码的复杂性。一个好的经验法则是永远不要使用lambda,直到没有其他选择。
其次,让按钮调用属于该类的一部分的函数。您需要的数据已经可访问,因此根本无需传递它。
类MyListbox: ... def go(self): print“works”+ self.combo_out,self.entNumber.list_out ... def建立(自我): ... labb = Button(fr_left,text =“OK”,command = self.go)