John R
John R

Reputation: 129

How to invoke a method from combo box event

I am trying to invoke a method from combo box selected change event with lambda expression but I am stuck with following error

TypeError: () takes no arguments (1 given)

I think I have passed 1 argument as per the method definition, could somebody please help me where I am wrong

or any other combobox selected change event code will be great help!

please note my code

self.boxWidget[boxName].bind("<<ComboboxSelected>>", lambda:invoke_Setting_Group(self))

def invoke_My_method1(self):    
    print "expand another window"

I am trying to pass the first class object to the second python script file for variable value assigning easeness.I tried to use this combox event change code without lambda then I noticed that this method is getting called automatically so I used lambda to prevent this automatic method calling

Sorry I am not having the knowledge on lambda expression usage; here I used only to prevent the automatic method execution. Without lambda expression I noticed my combo box function starts automatically, I did not understand why it happens so?

I am using TKinter python 2.6

More Detailed Code of above:

#Main_GUI_Class.py
##----------------------
import sys

class App():  

    def __init__ (self,master,geometry=None,root=None):    
        try:
           self.master=master                  
           if not root:
              self.root=Tkinter.Toplevel(master) 

    def initUI(self):
        try:                      
            self.master.title("GUI")        
            menubar = Menu(self.master)
            self.root.config(menu=menubar)    
            fileMenu.add_command(label='Open')                     
            submenu_ncss.add_command(label='Model Setting',command=lambda:Combo_Expand_Script.Call_Model_Setting(self))   

##----------------------

def main():
    r = Tkinter.Tk()    
    r.withdraw()
    r.title("GUI Sample")
    r.wm_iconbitmap(Pic1)
    v = App(r)
    r.mainloop()    

if __name__ == "__main__":
    main()

##Combo_Expand_Script.py
##-----------------------

import sys
import Tkinter

import Main_GUI_Class

def Call_Model_Setting(self):
    try:    
        self.PopUpWin = Toplevel(bg='#54596d',height=500, width=365)
        self.PopUpWin.title("POP UP SETTING")

        #Combo Boxs in Pop Up                      
        boxNameGroup="boxSetting"                  
        boxPlaceY=0    
        for Y in range(4):                      
          boxName=boxNameGroup+str(Y)
          if Y == 0:
              boxPlaceY=50
          else:
              boxPlaceY=boxPlaceY+40
          self.box_value = StringVar()

          self.boxWidget[boxName] = ttk.Combobox(self.PopUpWin, height=1, width=20)             

          if Y== 0:
               self.boxWidget[boxName]['values'] = ('A', 'B')
               self.boxWidget[boxName].current(1)
          if Y== 1:
               self.boxWidget[boxName]['values'] = ('X', 'Y')               
               self.boxWidget[boxName].bind("<<ComboboxSelected>>",lambda:invoke_Setting_Group(self))
          self.boxWidget[boxName].place(x=180, y = boxPlaceY)

       #Buttons in Pop Up
        self.btnApply = tk.Button(self.PopUpWin,width=10, height=1,text="Apply",relief=FLAT,bg=btn_Bg_Color,command=lambda: treeDataTransfer(self,0))
        self.btnApply.pack()          
        self.btnApply.place(x=75, y = 460)                  
        self.btnCancel = tk.Button(self.PopUpWin,width=10, height=1,text="Cancel",relief=FLAT,command=lambda: deleteTreeNodes(self))
        self.btnCancel.pack()          
        self.btnCancel.place(x=170, y = 460)
    except IOError:
       print "Error: data error"

def invoke_Setting_Group(self):#, event=None
    try:    
        #self.boxName.current(0)
        self.boxWidget["boxSetting3"].current(0)
        self.PopUpWin['width']=1050
        self.PopUpWin['height']=700
        self.btnApply.place(x=500, y = 550)
        self.btnCancel.place(x=600, y = 550)        

        self.txtWidget={}  

        lsttxtSetting = ['1', '2','3 ','4','5 ','6','7','8','9','10']                         
        for t in range(10):                  
              txtName=txtNameGroupTS+str(t)
              if t == 0:
                  txtPlaceY=120
              else:
                  txtPlaceY=txtPlaceY+30

              self.txtWidget[txtName] = Text(self.groupSettingFrame,height=1, width=10,borderwidth = 2)
              self.txtWidget[txtName].insert(INSERT, lsttxtSetting[t])
              self.txtWidget[txtName].pack()                      
              self.txtWidget[txtName].place(x=200, y = txtPlaceY)

    except IOError:
       print "Error: Group Settings Popup error"      

def turbDataTransferBind(self):

    for P in range(0,3):
        boxName="boxSetting"+str(X)
        dataSettingbox=self.lstTurb[X]+" "+self.boxWidget[boxName].get()
        self.root_node_Setting = self.tree.insert( self.root_node_ChildSetting["ChildSettingNode"], 'end', text=dataSettingbox, open=True) 

def treeDataTransfer(self,dlgTurbFlag):    
    self.treeDataTransferBind()
    print "data tranfer sucess"
def deleteTreeNodes(self):
    print "delete nodes"

Upvotes: 1

Views: 4011

Answers (1)

furas
furas

Reputation: 142641

command= and bind expect function name - without () and arguments - so in place of

If you use

.bind("<<ComboboxSelected>>", invoke_Setting_Group(self) )

then you use result from invoke_Setting_Group(self) as second argument in .bind(). This way you could dynamicly generate function used as argument in bind


TypeError: () takes no arguments (1 given)

This means you have function function() but python run it as function(arg1)

You run lambda:invoke_Setting_Group(self) but python expects lambda arg1:self.invoke_Setting_Group(self)

You could create function with extra argument

def invoke_My_method1(self, event):    
    print "expand another window"
    print "event:", event, event.widget, event.x, event.y

And then you could use it

.bind("<<ComboboxSelected>>", lambda event:invoke_Setting_Group(self, event))

BTW: it looks strange - you have class App() but in second file you use only functions instead of some class too.

Upvotes: 2

Related Questions