80 likes | 331 Views
Tkinter Basics. Initial Stuff. To get the package: from Tkinter import * To make a window, give it a title and operate it: windowVar = Tk () windowVar.title (“your window title”) … Code to setup your window contents … windowVar.mainloop () # runs the window,
E N D
Initial Stuff • To get the package: from Tkinter import * • To make a window, give it a title and operate it: windowVar = Tk() windowVar.title(“your window title”) … Code to setup your window contents … windowVar.mainloop() # runs the window, # issuing events and # callbacks to your code
Window Operation • Make a frame in the window: frameVar = Frame(windowVar) frameVar.pack() • Destroy/Shutdown the window: windowVar.destroy() # exits mainloop() • Schedule a call to a procedure: windowVar.after(mSec,procedureName) # schedules mainloop to call # procedureName() after a # delay of mSec milliseconds • Redrawing/updating the window windowVar.update()
Widgets • Button buttonVar = Button(frameVar,text=”Quit Game", \ fg="green”,command=self.newGame) # Makes a Button widget in frameVar, displaying # assigned text and using foreground color indicated # when Button is clicked, command is called • Scale scaleVar = Scale(frameVar, \ orient=HORIZONTAL, \ showvalue=0,to=7,length=200, \ label="Difficulty -->”, \ command=callbackProcedure) # Makes a “slider-type” bar inside # frameVar, horizontal, numbered from 0 – 7, # gives it a text label and calls back # callBackProcedure(num) giving new value in num
Canvas Widget • To make one: canvasVar = Canvas(windowVar, width=w, height=h,bg="yellow",borderwidth=0, \ highlightbackground="black", \ highlightthickness=2) # Makes a canvas in windowVar with size # w x h pixels, sets highlight and border # characteristics canvasVar.bind("<Button-1>”,mouseProc) # arrange to call mouseProc() when user # clicks left mouse button inside the canvas canvasVar.pack()
Drawing Objects in the Canvas • Draw a circle: circleVar = canvasVar.create_oval(llx,lly,urx,ury, \ fill="white") # draws circle/ellipse in box (llx,lly), # (urs,ury) with fill color “fill” • Draw a line: lineVar = canvasVar.create_line(x1,y1,x2,y2, \ width=15,fill="blue") # Line from (x1,y1) to (x2,y2) with # thickness of “width” and fill color “fill” • Write text: textVar = canvasVar.create_text(x,y,\ text=string,anchor="w”,font="Courier 24") # Place text into canvasVar at location(x,y) # “string” is the text, set the font and # (in this example) anchor text to the “w”est
Net Resources for Tkinter • Try this as a complete reference: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html • Here is a decent tutorial: http://zetcode.com/gui/tkinter/introduction/