Question: Try to run the following programfrom tkinter import *parent = Tk()C1 = Canvas(parent, bg=blue, height=250, width=300)coord = 10, 40, 210, 210C1.create_arc(coord, start=0, extent=120, fill=red)C1.pack()C2 =

Try to run the following programfrom tkinter import *parent = Tk()C1 = Canvas(parent, bg="blue", height=250, width=300)coord = 10, 40, 210, 210C1.create_arc(coord, start=0, extent=120, fill="red")C1.pack()C2 = Canvas(parent, bg="gray", height=300, width=200)C2.create_oval(20,20,100, 100, outline="yellow", fill="blue")C2.pack()parent.mainloop()

The first line from tkinter import * means we like to import tkinter module and want to use everything (the *)defined inside it. The line parent = Tk() means we want to create a top level window. Most people use root insteadof parent. Note it is just a name and we just choose an identifier we like. When calling Tk() without giving anyparameter means to create a top level window for your application.

The line C1 = Canvas(parent, bg="blue", height=250, width=300)Means we want to create a Canvas object and place it onto the top level window. Note bg is the background color,here we set it to color blue, The width and height are the dimensions of the canvas C1.The line coord = 10, 40, 210, 210Which allows us to set arc bounding box. We start an acr from degree 0 and extent 120 degrees as inC1.create_arc(coord, start=0, extent=120, fill="red") which will draw an arc on the C1 canvasC1.pack() will call pack method to place C1 on its parent window. Note that you can specify to place C1 in differentplace in its parent widget. For example C1.pack(side=bottom) will place it in the bottom of parent widget.We can create another canvas if we want. TheC2 = Canvas(parent, bg="gray", height=300, width=200) create another canvasWe then draw an oval. Because the bounding box has same dimensions so we have a circle.oval_1 = C2.create_oval(20,20,100, 100, outline="yellow", fill="blue")C2.pack() will allow us to place C2 on its parent widget.The last line, parent.mainloop(), shall be called once and only once. This start and allow your application to interactwith events such as mouse clicking, key press and so on.

Questions: 1. Can you place three canvas on the top level window? If yes. Create such an application.

The following is another program which will allow the system to interact with users mouse click event. When userclick the mouse, it will then draw a circle around the place where user clicked.from tkinter import *root = Tk()def clicked(event): C1.delete(ALL) C1.create_oval(event.x, event.y, event.x+20, event.y+20, outline="yellow", fill="blue")C1 = Canvas(root, height=250, width=300)C1.bind("", clicked)# bind mouse event to this canvas C1C1.pack()root.mainloop()# mainloop shall be called once and only once# it allows the application to handle events

Questions: 4. Button-1 is left button, how about Button-2 and Button-3? Modify the above program so that yourprogram can handle both Button-1 and Button-3 as well. When Button-3 clicked, it will draw a filled red circle. Note Button-1 still draw a filled blue circle.

Exam the following program and run it from tkinter import *root = Tk()def key(event): print ("pressed", event.char)def callback(event): frame.focus_set() print ("clicked at", event.x, event.y)frame = Frame(root, width=100, height=100)frame.bind("", key)frame.bind("", callback)frame.pack()root.mainloop()

Frame is a window which allows components like GUI components to be place on it. For example, you can place abutton on to a frame. You can use focus_set method to move focus to a widget such as the frame above

.Question

6. Modify the program above and save as another python file by binding . Dobule-1 meanto double click on Button-1. I.e. double click on left mouse button.Create a function and name this function stop. The stop function would be similar to functioncallback but this time, it will quit the program by calling sys.exit(). In order to use this function, youhave to import sys .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!