1 / 7

Tkinter Basics

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,

wesley
Download Presentation

Tkinter Basics

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 Basics

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

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

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

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

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

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

More Related