1 / 9

TKINTER

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

yosefu
Download Presentation

TKINTER

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TKINTER Lesson 3 - The Entry Box.

  2. 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

  3. 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

  4. 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()

  5. 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

  6. 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

  7. 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

  8. 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()

  9. Next week we will create a canvas and place an image in our window.

More Related