Home > Software engineering >  NameError: name 'event' is not defined in Tkinter
NameError: name 'event' is not defined in Tkinter

Time:02-20

I'm writing a code in Python using tkinter to get a value from a combo box, after that I use this value in click event of a button. But when I run this code I got this error

"NameError: name 'event' is not defined"

My code is like this

from tkinter import *
from tkinter.ttk import *    
from matplotlib.font_manager import FontProperties

font = FontProperties()
font.set_family('serif')
font.set_name('Times New Roman')
font.set_style('normal')  

    
window = Tk()
 
window.title("CCE Tool")
 
tab_control = Notebook(window) 

tab1 = Frame(tab_control) 
tab2 = Frame(tab_control)
 
tab_control.add(tab1, text='Regional') 
tab_control.add(tab2, text='Plot')

Filee_Tab1=Entry(tab1 ,width=70 , text='Enter adress of database')
Filee_Tab1.grid(column=1, row=0) 
 
lbl1_Tab1 = Label(tab1, text= 'Input data address') 
lbl1_Tab1.grid(column=0, row=0)  



def combo3_Tab1_click(event):
    global X # Setting select_sheet to global, so it can be modified
    X = combo3_Tab1.get()
    return X


def clicked_btn2_Tab1():

  X = combo3_Tab1_click(event)     
  return 0

combo3_Tab1 = Combobox(tab1, state="readonly")  
# combo3_Tab1.current(1) 
combo3_Tab1['values']= ["1","2","3"]
combo3_Tab1.grid(column=1, row=4) 
combo3_Tab1.bind("<<ComboboxSelected>>", combo3_Tab1_click)

btn2_Tab1 = Button(tab1,text='Run CNN', command=clicked_btn2_Tab1)
btn2_Tab1.grid(column=0,row=7)

tab_control.pack(expand=1, fill='both')     

window.geometry('900x500')
 
window.mainloop()

How can I fixed this error Thank you

CodePudding user response:

Your btn2_Tab1 is defined with a command= parameter which correctly calls the intended function.

Why does this function then need to call another that is intended to be the target for a bind() (which supplies the correct event)?

Just copy and paste the function contents:

def clicked_btn2_Tab1():

  X = combo3_Tab1.get()     
  return 0

CodePudding user response:

I've fixed it with below command:

from tkinter import *
from tkinter.ttk import *    
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from scipy.stats.kde import gaussian_kde
from scipy.stats import norm



font = FontProperties()
font.set_family('serif')
font.set_name('Times New Roman')
font.set_style('normal')  

    
window = Tk()
 
window.title("CCE Tool")
 
tab_control = Notebook(window) 

tab1 = Frame(tab_control) 
tab2 = Frame(tab_control)
 
tab_control.add(tab1, text='Regional') 
tab_control.add(tab2, text='Plot')

Filee_Tab1=Entry(tab1 ,width=70 , text='Enter adress of database')
Filee_Tab1.grid(column=1, row=0) 
 
lbl1_Tab1 = Label(tab1, text= 'Input data address') 
lbl1_Tab1.grid(column=0, row=0)  



def combo3_Tab1_click(event):
    global X # Setting select_sheet to global, so it can be modified
    X = combo3_Tab1.get()
    return X


def clicked_btn2_Tab1(event):

  X = combo3_Tab1_click(event)   
  print("OK")  
  return 0

combo3_Tab1 = Combobox(tab1, state="readonly")  
# combo3_Tab1.current(1) 
combo3_Tab1['values']= ["1","2","3"]
combo3_Tab1.grid(column=1, row=4) 
combo3_Tab1.bind("<<ComboboxSelected>>", combo3_Tab1_click)

btn2_Tab1 = Button(tab1,text='Run CNN')
btn2_Tab1.grid(column=0,row=7)
btn2_Tab1.bind("<Button-1>", clicked_btn2_Tab1)

tab_control.pack(expand=1, fill='both')     

window.geometry('900x500')
 
window.mainloop()

CodePudding user response:

The problem is that you have created a function you want to be callable both from an event being handled, and from other places in the code. When you use bind, the function automatically gets a parameter we typically name event. However, when you call functions via a command attribute you don't get this parameter.

In your case, the simplest solution is to just pass None as the event parameter since the function doesn't use it

def clicked_btn2_Tab1():
  X = combo3_Tab1_click(event=None)     
  return 0

Another solution is to make the event parameter optional, so you can directly call it without the parameter:

def combo3_Tab1_click(event=None):
    ...
  • Related