Star Rider
Star Rider

Reputation: 107

Giving input through a button to the textbox created with tkinter

I have created a calculator's body with Tkinter.

But the buttons like 1,2,3 e.t.c are not yet displaying the corresponding text on the button.

The objective is that ,When I press button 1 or 2 or 3 e.t.c it should display

that corresponding number.

I just need the basic syntax...

thanks in advance for the help

Refer the following to understand my program...

from Tkinter import *

root=Tk()

root.geometry('350x400')

text=Text(root,width=400,height=2,bd=10)
text.pack()

b1=Button(root,text='1')
b1.pack()
b1.place(x=0,y=60)
b1.config(width=10,height=5)

b2=Button(root,text='2')
b2.pack()
b2.place(x=85,y=60)
b2.config(width=10,height=5)

b3=Button(root,text='3')
b3.pack()
b3.place(x=170,y=60)
b3.config(width=10,height=5)

bc=Button(root,text='C')
bc.pack()
bc.place(x=255,y=60)
bc.config(width=10,height=5)


b4=Button(root,text='4')
b4.pack()
b4.place(x=0,y=160)
b4.config(width=10,height=5)

b5=Button(root,text='5')
b5.pack()
b5.place(x=85,y=160)
b5.config(width=10,height=5)

b6=Button(root,text='6')
b6.pack()
b6.place(x=170,y=160)
b6.config(width=10,height=5)

b1=Button(root,text='+')
b1.pack()
b1.place(x=255,y=160)
b1.config(width=3,height=2)

b1=Button(root,text='-')
b1.pack()
b1.place(x=305,y=160)
b1.config(width=3,height=2)


b1=Button(root,text='x')
b1.pack()
b1.place(x=255,y=205)
b1.config(width=3,height=2)

b1=Button(root,text='/')
b1.pack()
b1.place(x=305,y=205)
b1.config(width=3,height=2)




b7=Button(root,text='7')
b7.pack()
b7.place(x=0,y=260)
b7.config(width=10,height=5)

b8=Button(root,text='8')
b8.pack()
b8.place(x=85,y=260)
b8.config(width=10,height=5)

b9=Button(root,text='9')
b9.pack()
b9.place(x=170,y=260)
b9.config(width=10,height=5)

b1=Button(root,text='Exit')
b1.pack()
b1.place(x=255,y=260)
b1.config(width=10,height=5)



root.mainloop()

Upvotes: 0

Views: 847

Answers (1)

deborah-digges
deborah-digges

Reputation: 1173

For doing something in response to a button click, you must use the command keyword parameter as follows:

b1=Button(root,text='1', command=lambda: display_number(text, 1))

command can be used to specify the function you want to execute in response to a button click. Of course, this function must be defined. A possible implementation:

def display_number(text, number):
    text.insert(END, number)

Upvotes: 1

Related Questions