90 likes | 396 Views
TKINTER. Lesson 3 - The Entry Box. Last week. We produced a Tkinter window with a label and a button Now we are going to create a new program that creates a Button and an Entry Box. The user writes in the box. When the button is pressed that input is made into a new label
E N D
TKINTER Lesson 3 - The Entry Box.
Last week • We produced a Tkinter window with a label and a button • Now we are going to create a new program that creates a Button and an Entry Box. • The user writes in the box. When the button is pressed that input is made into a new label • To do this we must write or define a function • And then call (execute) that function when the button is pressed
The New Program • Open Python • Then go File > New window • In the new untitled window • Go File > Save As • Save the file tkentry.py in your area
import sys from tkinter import * mGui = Tk() mGui.geometry (‘400 x400’) mGui.title(‘My Window’) mlabel=Label(text=‘Testwood School’).pack() mbutton=Button(mGui,text=“OK”).pack() mGui.mainloop()
First lets create the Entry Box – underneath the label Testwood School import sys from tkinter import* mGui= Tk() ment = Stringvar() mGui.geometry (‘400 x400’) mGui.title(‘My Window’) mlabel=Label(text=‘Testwood School’).pack() mEntry = Entry(mGui,textvariable = ment).pack() mbutton=Button(mGui,text=“OK”).pack() mGui.mainloop() # ment is a variable which stores what is typed in the entry box
Now lets write our function. import sys from tkinter import* defmyinput(): mtext = ment.get() mlabel2= Label(mGui,text=mtext).pack() mGui= Tk() mGui.geometry (‘400 x400’) mGui.title(‘My Window’) mlabel=Label(text=‘Testwood School’).pack() mEntry = Entry(mGui,textvariable = ment).pack() mbutton=Button(mGui,text=“OK”).pack() mGui.mainloop() # ment is a variable which stores what is typed in the entry box
And finally lets get the button to call (execute) the function. import sys from tkinter import* defmyinput(): mtext = ment.get() mlabel2= Label(mGui,text=mtext).pack() mGui= Tk() mGui.geometry (‘400 x400’) mGui.title(‘My Window’) mlabel=Label(text=‘Testwood School’).pack() mEntry = Entry(mGui,textvariable = ment).pack() mbutton=Button(mGui,text=“OK”,command=myinput).pack() mGui.mainloop() # ment is a variable which stores what is typed in the entry box
Extension Task – Try this code what’s changed and why? import sys from tkinter import* defmentry(): mtext=ment.get() ytext=yent.get() mlabel2=Label(mGui,text=mtext+ytext).pack() mGui =Tk() ment=IntVar() yent=IntVar() mGui.geometry("450x450+200+200") mGui.title ("Testwood Calculator") mlabel=Label(mGui,text="Testwood Calculator").pack() mEntry=Entry(mGui,textvariable=ment,width=5).pack() yEntry=Entry(mGui,textvariable=yent).pack() mbutton=Button(mGui,text="OK",command=mentry).pack() mGui.mainloop()
Next week we will create a canvas and place an image in our window.