mumu
mumu

Reputation: 11

tkinter Button callback function not being called

I want to make a simple household account program using tkinter module in python. I want to bind okclick function to button1. I think I coded appropriate referencing other codes but when I execute this code there happens a message that the function okclick is not defined.

Does anyone know what is wrong with it?

from tkinter import *
from tkinter import ttk
import colors as c
from tkcalendar import DateEntry

b=Tk()

class memo(Frame):

    def __init__(self,master):
        Frame.__init__(self,master)
        self.master=master
        self.master.title('Memo')
        self.pack(fill=BOTH, expand=True)

        frame1=Frame(self,width=500,height=50)
        frame1.pack(expand=False)
        label1=Label(frame1,text='Amount',width=10)
        label1.pack(side=LEFT, padx=10,pady=10)
        entry1=Entry(frame1,width=20)
        entry1.pack(padx=10,fill=X,expand=True)

        frame2=Frame(self,width=500,height=50)
        frame2.pack(expand=False)
        label2=Label(frame2,text='Cartegory',width=10)
        label2.pack(side=LEFT, padx=10,pady=10)
        listbox1=Listbox(frame2,width=20)
        listbox1.insert(END,"식료품비","잡화비","건강관리비","외식비")
        listbox1.pack(side=LEFT, padx=10,pady=10)

        frame3=Frame(self,width=500,height=50)
        frame3.pack(expand=False)
        label3=Label(frame3,text='Date',width=10)
        label3.pack(side=LEFT, padx=10,pady=10)
        dateentry = DateEntry(frame3)
        dateentry.pack(padx=10,pady=10)

        frame4=Frame(self,width=500,height=500)
        frame4.pack(expand=False)
        button1=Button(frame4,text='csv Export',command=self.okClick)
        button1.pack(side=LEFT,padx=10,pady=10)

    def okClick(self):
        name = self.entry1.get()
        print(name)

a=memo(b)
a.mainloop()

Upvotes: 1

Views: 1053

Answers (1)

3dnerd
3dnerd

Reputation: 66

You have a few things wrong with your code:

  1. onClick needs the "self" param passed to it and you need to update "command=onClick" to "command=self.onClick"

  2. Your entry1 variable should be set to an instance variable by prepending it with "self" so that it is accessible from the onClick method. Variable scope is a little different in python OOP.

from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from tkcalendar import DateEntry

class Memo(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.master=master
        self.master.title('Memo')
        self.pack(fill=BOTH, expand=True)

        frame1=Frame(self,width=500,height=50)
        frame1.pack(expand=False)
        label1=Label(frame1,text='Amount',width=10)
        label1.pack(side=LEFT, padx=10,pady=10)
        self.entry1=Entry(frame1,width=20)
        self.entry1.pack(padx=10,fill=X,expand=True)

        frame2=Frame(self,width=500,height=50)
        frame2.pack(expand=False)
        label2=Label(frame2,text='Cartegory',width=10)
        label2.pack(side=LEFT, padx=10,pady=10)
        listbox1=Listbox(frame2,width=20)
        listbox1.insert(END,"식료품비","잡화비","건강관리비","외식비")
        listbox1.pack(side=LEFT, padx=10,pady=10)

        frame3=Frame(self,width=500,height=50)
        frame3.pack(expand=False)
        label3=Label(frame3,text='Date',width=10)
        label3.pack(side=LEFT, padx=10,pady=10)
        dateentry = DateEntry(frame3)
        dateentry.pack(padx=10,pady=10)

        frame4=Frame(self,width=500,height=500)
        frame4.pack(expand=False)
        button1=Button(frame4,text='csv Export',command=self.okClick)
        button1.pack(side=LEFT,padx=10,pady=10)

    def okClick(self):
        name = self.entry1.get()
        messagebox.showinfo("이름", name)

if __name__ == "__main__":
    a=Memo(Tk())
    a.mainloop()

Upvotes: 3

Related Questions