Advertisement
here2share

# tk_s_curve.py

Mar 14th, 2024 (edited)
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. # tk_s_curve.py
  2.  
  3. import tkinter as tk
  4. import random
  5. import math
  6.  
  7. steps_value = 500
  8. start_y = 200
  9. end_y = 50
  10.  
  11. def draw_s_curve():
  12.     canvas.delete("all")
  13.     canvas_width = canvas.winfo_width()
  14.     start_x = (canvas_width - steps_value) // 2
  15.     end_x = start_x + steps_value
  16.     for i in range(start_x, end_x + 1):
  17.         x = i - start_x
  18.         y = start_y + (end_y - start_y) * (math.sin((x / steps_value) * math.pi - (math.pi / 2)) + 1) / 2 + 30
  19.         canvas.create_oval(i, y, i, y, fill='black')
  20.  
  21. def get_steps_value(event):
  22.     global steps_value
  23.     steps_value = int(steps_scale.get())
  24.     draw_s_curve()
  25.  
  26. def get_start_y(event):
  27.     global start_y
  28.     start_y = 255 - int(start_scale.get())
  29.     draw_s_curve()
  30.  
  31. def get_end_y(event):
  32.     global end_y
  33.     end_y = 255 - int(end_scale.get())
  34.     draw_s_curve()
  35.  
  36. root = tk.Tk()
  37. root.title("S-Curve Generator")
  38.  
  39. canvas = tk.Canvas(root, width=600, height=300, bg="white")
  40. canvas.pack()
  41.  
  42. steps_scale = tk.Scale(root, from_=8, to=600, orient=tk.HORIZONTAL, label="Steps", command=get_steps_value)
  43. steps_scale.set(steps_value)  # Set default length to 500
  44. steps_scale.pack(fill='x')
  45.  
  46. start_scale = tk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="Start Point", command=get_start_y)
  47. start_scale.set(start_y)  # Set default start to 200
  48. start_scale.pack(fill='x')
  49.  
  50. end_scale = tk.Scale(root, from_=0, to=255, orient=tk.HORIZONTAL, label="End Point", command=get_end_y)
  51. end_scale.set(end_y)  # Set default end to 50
  52. end_scale.pack(fill='x')
  53.  
  54. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement