tkinter Button回调函数未调用

问题描述

我想使用python中的tkinter模块制作一个简单的家庭帐户程序。我想将okclick函数绑定到button1。我想我已经编码了适当的引用其他代码,但是当我执行此代码时,会出现一条消息,提示未定义函数okclick

有人知道这是怎么回事吗?

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,height=50)
        frame2.pack(expand=False)
        label2=Label(frame2,text='Cartegory',width=10)
        label2.pack(side=LEFT,pady=10)
        listBox1=ListBox(frame2,width=20)
        listBox1.insert(END,"식료품비","잡화비","건강관리비","외식비")
        listBox1.pack(side=LEFT,pady=10)

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

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

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

a=memo(b)
a.mainloop()

解决方法

您的代码有些错误:

  1. onClick需要传递给它的“自我”参数,您需要将“ command = onClick”更新为“ command = self.onClick”

  2. 您的entry1变量应设置为实例变量,方法是在其前面加上“ self”,以便可以从onClick方法访问它。变量范围在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,height=50)
        frame2.pack(expand=False)
        label2=Label(frame2,text='Cartegory',width=10)
        label2.pack(side=LEFT,pady=10)
        listbox1=Listbox(frame2,width=20)
        listbox1.insert(END,"식료품비","잡화비","건강관리비","외식비")
        listbox1.pack(side=LEFT,pady=10)

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

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

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

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