So following on from the previous posts, I am how sharing a small application, that makes use of what we have been learning.
This presents 2 input boxes and an output box, any values entered in to the first two, the sum is placed in to the last box.
This is not perfect, but getting there slowly.
We need to:-
- Fix the name of the button from button1
- Detect if the user has entered numerical values
- Detect for empty boxes
- Make labels more useful
- Fix spelling in comments
- Fix clarity of comments
The code for the above is as follows.
#!/usr/bin/env python import Tkinter # note use of caps from Tkinter import * window = Tk() window.title('App 1') window.geometry("650x125") # w x h window.resizable(0,0) #define button actions def btn1(): #convert box text in to integers ent1 = int(entrytext.get()) ent2 = int(entrytext2.get()) #add the 2 integers and store in variable add add = (ent1 + ent2) print add #instert value add in to box outtext outtext1.insert(0,str(add)) # insert response btn_tog2 = Button( window, text ='button1', command=btn1) btn_exit = Button( window, text ='exit',command=exit) # define some labels box1 = Label(window, text="Entry 1: ") box2 = Label(window, text="Entry 2: ") box3 = Label(window, text="Ouput1: ") #define entry box entry1 = StringVar() # this is our entry box entry2 = StringVar() entrytext = Entry(window, textvariable=entry1) # this is our entry box entrytext2 = Entry(window, textvariable=entry2) # this is our second entry box #define out box entry2 = StringVar() # this is our output box outtext1 = Entry(window, textvariable=entry2) # this is our output box #display boxes entrytext.grid(row = 3, column = 2,) #display entry box entrytext2.grid(row = 3, column = 3,) #display entry box outtext1.grid(row = 3, column = 4,) #display output box #place labels box1.grid(row = 1 , column = 2, padx = 5, pady = 5) box2.grid(row = 1 , column = 3, padx = 5, pady = 5) box3.grid(row = 1 , column = 4, padx = 5, pady = 5) #buttons btn_tog2.grid(row = 3, column = 5, padx = 5, pady = 5) btn_exit.grid(row = 3, column = 6, padx = 5, pady = 5) window.mainloop()