如何在Python Tkinter中将变量值或Listbox值传递给另一个类的函数

时间:2017-03-28 12:22:55

标签: python class tkinter

我想将AutocompleteEntry中选择的变量传递给另一个形式或类中的另一个函数(即func2)。我尝试了如下

import Tkinter, tkFileDialog, Tkconstants , os
from Tkinter import *
import tkMessageBox as messagebox, Tkinter
import sys
import ttk
import tkFont
list1233 = ['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']

# My frame for form
class simpleform_ap(Tk):
    def __init__(self,parent):
        Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        self.grid()        
    def initialize(self):
        # Dropdown Menu
        self.dropMenu2 = AutocompleteEntry(list1233)
        self.dropMenu2.grid(row=6, column=3)
    def func2(self,value2):
        print value2
        messagebox.showinfo("FileSave Status ", valu2)
class AutocompleteEntry(Entry):
    def __init__(self, list123, *args, **kwargs):        
        Entry.__init__(self, *args, **kwargs)
        self.list123 = list123     
        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()
            if words:            
                if not self.lb_up:
                    self.lb = Listbox()
                    self.lb.bind("<Double-Button-1>", self.selection)
                    print "satyaaa"
                    self.lb.bind("<Right>", self.selection)
               #     self.lb.bind('<<ListboxSelect>>', self.functt)
                    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 functt(self,val):
        widget = val.widget
        selection=widget.curselection()
        value = widget.get(selection[0])
        print "selection:", selection, ": '%s'" % value
        if self.lb_up:
            print "I",self.lb.get(ACTIVE)

    def selection(self, event):        
        if self.lb_up:            
            self.var.set(self.lb.get(ACTIVE))
            print "she", self.lb.get(ACTIVE)
            enterednumber=self.lb.get(ACTIVE)            
            self.lb.destroy()
            self.lb_up = False
            self.icursor(END)
            self.app = simpleform_ap(self.selection, enterednumber)   # Send it to Demo3 as an argument

    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)
                #print 

    def comparison(self):
        pattern = re.compile('.*' + self.var.get() + '.*')
        return [w for w in self.list123 if re.match(pattern, w)]            


def create_form(argv):
    form = simpleform_ap(None)
    w, h = form.winfo_screenwidth(), form.winfo_screenheight()
    form.geometry("1000x650")
    form.resizable(width=False, height=False)
    form.title('Meta Data Exporter')
    form.mainloop()
if __name__ == "__main__":
     global label123
     create_form(sys.argv)

它给出了错误TypeError: init ()正好接受2个参数(给定3个)。请帮帮我

1 个答案:

答案 0 :(得分:1)

您可以向 init 函数添加另一个变量,并在没有传递值时将其设置为None

# My frame for form
class simpleform_ap(Tk):
    def __init__(self,parent, entered_value=None):
        Tk.__init__(self,parent)
        ..
        ..

您是否也希望将方法或类作为行

中的父项传递
self.app = simpleform_ap(self.selection, enterednumber)   # Send it to Demo3 as an argument

你可能只想传递self而不是self.selection