0% found this document useful (0 votes)
4 views7 pages

Mini Project - 23BAI10294

The document describes a Python project for creating a GUI fidget spinner using the Tkinter library. It outlines the components of the program, including class definitions, methods for initializing the spinner, and event handling for user interaction. The fidget spinner simulates spinning on screen, providing a digital alternative to the physical toy.

Uploaded by

tanmaybotttt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Mini Project - 23BAI10294

The document describes a Python project for creating a GUI fidget spinner using the Tkinter library. It outlines the components of the program, including class definitions, methods for initializing the spinner, and event handling for user interaction. The fidget spinner simulates spinning on screen, providing a digital alternative to the physical toy.

Uploaded by

tanmaybotttt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Aman

Reg. No.: Mi Goswami


23BAI10294
Subject: Python Programming

ni
Slot: B21 + E21 + B22 +E22
Submitted To: Prof. Jayanthi J
Date of Submission: 5/8/24

Pro
jec
GUI Fidget
t Spinner
OUTPUT :
A GUI (Graphical User Interface) fidget spinner is a digital version of a physical
fidget spinner that you can interact with on a screen. It's designed to provide a
similar tactile experience as spinning a real fidget spinner, offering a way to
reduce stress or pass the time.

Explanation :

 import tkinter as tk: This imports the Tkinter library and gives it the alias
tk for easier use.
 import math: This imports the math library, which provides mathematical
functions such as cos and sin.

 class FidgetSpinner:: This defines a new class called FidgetSpinner.


 def __init__(self, root):: This is the initializer method for the class. It sets
up the initial state of the FidgetSpinner object.
 self.root = root: This assigns the root parameter to an instance variable
self.root.
 self.root.title("GUI Fidget Spinner"): This sets the title of the window to
"GUI Fidget Spinner".
 self.canvas = tk.Canvas(root, width=400, height=400, bg="white"): This
creates a Tkinter Canvas widget with a width and height of 400 pixels and
a white background.
 self.canvas.pack(): This adds the canvas to the window and makes it
visible.

 self.spinner = self.canvas.create_oval(250, 250, 250, 250, fill="purple"):


This creates an oval on the canvas with the top-left corner at (250, 250)
and the bottom-right corner at (250, 250). The oval is filled with blue
color. This oval represents the fidget spinner.
 self.speed = 0: This initializes the speed of the spinner to 0.
 self.angle = 0: This initializes the angle of the spinner to 0.

 self.root.bind("<Button-1>", self.start_spinning): This binds the left mouse


button click event to the self.start_spinning method. When the left mouse
button is clicked, the self.start_spinning method will be called.
 self.update_spinner(): This calls the self.update_spinner method to start
the spinner update loop.
 def start_spinning(self, event):: This defines the start_spinning method,
which takes an event parameter (the mouse click event).
 self.speed = 10: This sets the speed of the spinner to 10 when the left
mouse button is clicked.

 def update_spinner(self):: This defines the update_spinner method.


 self.angle += self.speed: This increases the angle of the spinner based on
the current speed.
 self.speed *= 0.95: This decreases the speed by multiplying it by 0.95 to
simulate friction.

 if self.speed < 0.1:: This checks if the speed is less than 0.1.
 self.speed = 0: If the speed is less than 0.1, it sets the speed to 0.

 x_center = 200: This sets the x-coordinate of the center of the spinner to
200.
 y_center = 200: This sets the y-coordinate of the center of the spinner to
200.
 radius = 50: This sets the radius of the spinner to 50.
 These lines calculate the coordinates of three points on the spinner, which
are 120 degrees apart. The math.radians function converts the angle from
degrees to radians, and math.cos and math.sin calculate the x and y
offsets based on the angle.

 self.canvas.coords(self. Spinner, x1-25, y1-25, x3+25, y3+25): This updates


the coordinates of the spinner oval to the new positions calculated.
 self.root. After(20, self.update_spinner): This schedules the
self.update_spinner method to be called again after 20 milliseconds,
creating an animation loop.

 if __name__ == "__main__":: This checks if the script is being run directly


(not imported as a module).
 root = tk.Tk(): This creates the main Tkinter window.
 app = FidgetSpinner(root): This creates an instance of the FidgetSpinner
class.
 root.mainloop(): This starts the Tkinter main loop, which handles events
and updates the GUI.

You might also like