Home > Net >  Tkinter: Calling function when button is pressed but i am getting attribute error 'Application&
Tkinter: Calling function when button is pressed but i am getting attribute error 'Application&

Time:12-05

from tkinter import *
import sqlite3
conn = sqlite3.connect('database.db')
c = conn.cursor()


class Application:
    def __init__(self, master):
        self.master = master

        self.left = Frame(master, width=800, height=720, bg='lightgreen')
        self.left.pack(side=LEFT)

        self.right = Frame(master, width=400, height=720, bg='lightblue')
        self.right.pack(side=RIGHT)
        self.heading=Label(self.left,text="HM hospital Appointments",font=('arial 40 bold'),fg='white',bg='lightgreen')
        self.heading.place(x=0,y=0)

        self.name=Label(self.left,text="Patient Name",font=('arial 20 italic'),fg='black',bg='lightgreen')
        self.name.place(x=0,y=100)
        self.age=Label(self.left,text="Age",font=('arial 20 italic'),fg='black',bg='lightgreen')
        self.age.place(x=0,y=150)
        self.gender=Label(self.left,text="Gender",font=('arial 20 italic'),fg='black',bg='lightgreen')
        self.gender.place(x=0,y=200)
        self.location=Label(self.left,text="Location",font=('arial 20 italic'),fg='black',bg='lightgreen')
        self.location.place(x=0,y=250)
        self.time=Label(self.left,text="Appointment Time",font=('arial 20 italic'),fg='black',bg='lightgreen')
        self.time.place(x=0,y=300)
        self.name_ent=Entry(self.left,width=30)
        self.name_ent.place(x=250,y=110)
        self.age_ent=Entry(self.left,width=30)
        self.age_ent.place(x=250,y=160)
        self.gender_ent=Entry(self.left,width=30)
        self.gender_ent.place(x=250,y=210)
        self.location_ent=Entry(self.left,width=30)
        self.location_ent.place(x=250,y=260)
        self.time_ent=Entry(self.left,width=30)
        self.time_ent.place(x=250,y=310)
        self.submit=Button(self.left,text="Add appointment",width=20,height=2,bg='steelblue',command=self.hi)
        self.submit.place(x=300,y=350)
                
        def hi(self):
            self.val1=self.name_ent.get()
            

root = Tk()
b = Application(root)

root.geometry("1200x720 0 0")

root.resizable(False, False)

root.mainloop()

I looked many videos to solve it but can't find the right one so please help me.

CodePudding user response:

To fix the error you are seeing, you need to move the definition of the hi function inside the Application class and not inside __init__, like this:

class Application:
    def __init__(self, master):
        # code for the rest of the __init__ method

        self.submit=Button(self.left,text="Add appointment",width=20,height=2,bg='steelblue',command=self.hi)

    ...
    ...
    def hi(self):
        self.val1=self.name_ent.get()
  • Related