It is a simple Python program to add two integers and display their sum. This program is only for learning Python Tkinter.
Here two text boxes will accept two numbers from user and display their sum in another text box.
Interface:
Code Behind Interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from tkinter import * def addNumbers(): res=int(e1.get())+int(e2.get()) myText.set(res) master = Tk() myText=StringVar(); Label(master, text="First").grid(row=0, sticky=W) Label(master, text="Second").grid(row=1, sticky=W) Label(master, text="Result:").grid(row=3, sticky=W) result=Label(master, text="", textvariable=myText).grid(row=3,column=1, sticky=W) e1 = Entry(master) e2 = Entry(master) e1.grid(row=0, column=1) e2.grid(row=1, column=1) b = Button(master, text="Calculate", command=addNumbers) b.grid(row=0, column=2,columnspan=2, rowspan=2,sticky=W+E+N+S, padx=5, pady=5) mainloop() |
I get following error when I click Button for calculate
AttributeError: ‘NoneType’ object has no attribute ‘get’
@anky Can you share more details on error, also snip of code you ran?
Worked really flawless, super example to begin GUI learning with Python Tk.
One thing to note, should compile with python 3.* versions as 2.* versions have Tkinter with cap “T” and 3.* versions have tkinter with small “t”.
Keep posting more stuff, on complicated GUI’s with Tk.
Thanks,