Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write python programs: one that displays the mouse position and when the mouse is clicked, and the other displays the mouse position when the mouse
Write python programs: one that displays the mouse position and when the mouse is clicked, and the other displays the mouse position when the mouse button is pressed and ceases to display it when the mouse button is released.
The following code is the solution.
from tkinter import * # Import tkinter width = 220 height = 100 class MainGUI: def __init__(self): window = Tk() # Create a window window.title("Mouse Position") # Set a title self.canvas = Canvas(window, bg = "white", width = width, height = height) self.canvas.pack() # Bind canvas with mouse events self.canvas.bind("", self.displayPosition) window.mainloop() # Create an event loop def displayPosition(self, event): self.canvas.delete("text") self.canvas.create_text(event.x, event.y - 4, text = "(" + str(event.x) + ", " + str(event.y) + ")", tags = "text") MainGUI()
Now also consider the following Exercise:
And the following code as its solution.
from tkinter import * # Import tkinter width = 240 height = 120 class MainGUI: def __init__(self): window = Tk() # Create a window window.title("Inside the circle?") # Set a title self.canvas = Canvas(window, bg = "white", width = width, height = height) self.canvas.pack() self.canvas.create_oval(100 - 50, 60 - 50, 100 + 50, 60 + 50, tags = "circle") self.canvas.bind("", self.isInside) window.mainloop() # Create an event loop def isInside(self, event): self.canvas.delete("text") if isInsideCircle(100, 60, 50, event.x, event.y): self.canvas.create_text(event.x, event.y - 5, text = "Mouse pointer is in the circle", tags = "text") else: self.canvas.create_text(event.x, event.y - 5, text = "Mouse pointer is not in the circle", tags = "text") def isInsideCircle(xCenter, yCenter, radius, x, y): return distance(xCenter, yCenter, x, y)Modify the code so it looks and behaves as follows:
O Mouse Position Mouse Position (128, 49) (79, 61) **11.10 (Geometry: inside a circle?) Write a program that draws a fixed circle centered at ( 100, 60 ) with radius 50. Whenever the mouse is moved while the left button is pressed, display the message indicating whether the mouse pointer is inside the circle, as shown in Figure 11.190. Figure 11.19 0 Inside the circle? OO Inside the circle? Mouse pointer is in the circle Mouse pointer is not in the circle (b) Detect whether the mouse pointer is inside a circle. C:\WINDOWS\py.exe Inside the circle or rectangle? - O X Outside circle Inside r angle (178, 39)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started