Advertisement
here2share

# Tk_simple_button_animation.py

Dec 29th, 2021
2,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # Tk_simple_button_animation.py
  2.  
  3. from tkinter import *
  4.  
  5. root = Tk()
  6. root.title('Simple Button Animation')
  7. root.geometry("400x300")
  8.  
  9. #define some variables
  10. count = 0
  11. size = 26
  12. pos = 100
  13.  
  14. # Contract the button
  15. def contract():
  16.     global count, size, pos
  17.     if count:
  18.         size -= 2
  19.         # Configure button font size
  20.         my_button.config(font=("Helvetica", size))
  21.         # Change button position
  22.         my_button.pack_configure(pady=pos)
  23.         # decrease the count by 1
  24.         count -= 1
  25.         pos += 2
  26.         # Set a timer
  27.         root.after(4, contract)
  28.  
  29. # Expand the button
  30. def expand():
  31.     global count, size, pos
  32.     if count < 15:
  33.         size += 2
  34.         # Configure button font size
  35.         my_button.config(font=("Helvetica", size))
  36.         # Change button position
  37.         my_button.pack_configure(pady=pos)
  38.         # Increase the count by 1
  39.         count += 1
  40.         pos -= 2
  41.         # Set the timer
  42.         root.after(2, expand)
  43.  
  44.     else:
  45.         contract()
  46.  
  47.  
  48. # Create a button
  49. my_button = Button(root,
  50.     text="Click Me!",
  51.     command=expand,
  52.     font=("Helvetica", 24),
  53.     fg="red")
  54. my_button.pack(pady=100)
  55.  
  56.  
  57. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement