Skip to content

Instantly share code, notes, and snippets.

@trialan
Created October 2, 2024 15:19
Show Gist options
  • Save trialan/d7dad35f6c64aec39590db531168c67a to your computer and use it in GitHub Desktop.
Save trialan/d7dad35f6c64aec39590db531168c67a to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
# Sample data for X and Y
X = np.random.rand(10, 3)
Y = np.random.rand(10, 3)
# Create the main window
root = tk.Tk()
root.title("Move Y Points")
# Create a matplotlib figure and embed it in Tkinter
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D # Necessary for 3D plotting
fig = Figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# Function to plot X and Y points
def plot_points():
ax.clear()
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c='blue', label='X')
ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], c='red', label='Y')
ax.legend()
canvas.draw()
plot_points()
# Movement step size
step = 0.1
# Function to move Y points
def move_y(dx=0, dy=0, dz=0):
global Y
Y = Y + np.array([dx, dy, dz])
plot_points()
# Frame to hold the control buttons
button_frame = tk.Frame(root)
button_frame.pack(side=tk.BOTTOM)
# Control buttons to move Y points
button_up = tk.Button(button_frame, text='Up', command=lambda: move_y(dy=step))
button_down = tk.Button(button_frame, text='Down', command=lambda: move_y(dy=-step))
button_left = tk.Button(button_frame, text='Left', command=lambda: move_y(dx=-step))
button_right = tk.Button(button_frame, text='Right', command=lambda: move_y(dx=step))
button_zup = tk.Button(button_frame, text='Z Up', command=lambda: move_y(dz=step))
button_zdown = tk.Button(button_frame, text='Z Down', command=lambda: move_y(dz=-step))
button_exit = tk.Button(button_frame, text='Exit')
# Function to handle exit
def on_exit():
print("New coordinates of Y:")
print(Y)
root.destroy()
button_exit.config(command=on_exit)
# Arrange buttons in the frame
button_up.grid(row=0, column=1)
button_left.grid(row=1, column=0)
button_down.grid(row=1, column=1)
button_right.grid(row=1, column=2)
button_zup.grid(row=0, column=3)
button_zdown.grid(row=1, column=3)
button_exit.grid(row=2, column=1)
# Start the Tkinter main loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment