Home > Software design >  Function calling is not working in optionmenu widget in python
Function calling is not working in optionmenu widget in python

Time:01-03

# Import module
from tkinter import *
import csv
import pandas as pd

# Create object
root = Tk()
def find_account_no():
    with open('PartyNames.csv', newline ='') as f:
        reader=csv.reader(f)
        parties = []
        parties.extend(reader)
        global account_number_var
    for i in parties:
        if(i[0] == party_name_var):
            account_number_var = i[1]
            print(account_number_var)
        else:
            continue
        return(account_number_var)
# Adjust size
root.geometry( "1361x768" )
root.state('zoomed')
with open('PartyNames.csv', newline ='') as f:
    reader=csv.reader(f)
    party_names = []
    party_names.extend(reader)
global party_name_var
party_name_var = StringVar()
party_name_var.set("Select Party Name")
Label(root, text='Party Name').grid(row=0,column=0)
party_name_list = [" ".join(row) for row in party_names]
party_name = OptionMenu(root, party_name_var,*party_name_list,command=find_account_no)
party_name.grid(row=0,column=1)
Label(root, text='Account Number').grid(row=1,column=0)
global account_number
account_number = Entry(root)
account_number.grid(row=1, column=1)
# Execute tkinter
root.mainloop()

This is the code.On line 33 I am calling the function find_account_nobut it is throwing this error.

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\DELL\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 3943, in __call__
    self.__callback(self.__value, *args)
TypeError: find_account_no() takes 0 positional arguments but 1 was given

Why?

CodePudding user response:

I think, its because when a function is binded to something in tkinter, the function is always called with a argument being passed to it...

One way to eradicate is to define the function with a dummy argument like this...

# Import module
from tkinter import *
import csv
import pandas as pd

# Create object
root = Tk()
def find_account_no(event): # <-- 'event' is a dummy argument which plays no role in the function
    with open('PartyNames.csv', newline ='') as f:
        reader=csv.reader(f)
        parties = []
        parties.extend(reader)
        global account_number_var
    for i in parties:
        if(i[0] == party_name_var):
            account_number_var = i[1]
            print(account_number_var)
        else:
            continue
        return(account_number_var)
# Adjust size
root.geometry( "1361x768" )
root.state('zoomed')
with open('PartyNames.csv', newline ='') as f:
    reader=csv.reader(f)
    party_names = []
    party_names.extend(reader)
global party_name_var
party_name_var = StringVar()
party_name_var.set("Select Party Name")
Label(root, text='Party Name').grid(row=0,column=0)
party_name_list = [" ".join(row) for row in party_names]
party_name = OptionMenu(root, party_name_var,*party_name_list,command=find_account_no)
party_name.grid(row=0,column=1)
Label(root, text='Account Number').grid(row=1,column=0)
global account_number
account_number = Entry(root)
account_number.grid(row=1, column=1)
# Execute tkinter
root.mainloop()
  • Related