Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 windowVar.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(" ”,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 Changing a Canvas item To modify an existing item in the canvas: draw.itemconfig(item,fill=newColor) Where: draw is the Canvas object (returned by the Canvas constructor) And item is a reference to something created in the Canvas by a draw.create_???? method Any named attribute (such as “fill”) can be modified with the Canvas itemconfig method.

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

9 GUI Startup Code I would expect your startup code to look something like this: root = Tk() root.title("Connect4”) bd = Board(7, 6, root) aiPlayer = Player('o', 'Random', 5) bd.playGUI(aiPlayer) root.mainloop()


Download ppt "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."

Similar presentations


Ads by Google