-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpan_example2.py
More file actions
46 lines (35 loc) · 1.5 KB
/
Copy pathpan_example2.py
File metadata and controls
46 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import tkinter as tk
from PIL import Image, ImageTk
class ImagePanner(tk.Tk):
def __init__(self, image_path):
super().__init__()
self.title("Image Panner")
# Load the image
self.image = Image.open(image_path)
self.tk_image = ImageTk.PhotoImage(self.image)
# Create a canvas and add the image
self.canvas = tk.Canvas(self, width=800, height=600)
self.canvas.pack(fill=tk.BOTH, expand=True)
self.image_id = self.canvas.create_image(0, 0, anchor=tk.NW, image=self.tk_image)
# Bind mouse events for panning
self.canvas.bind("<ButtonPress-1>", self.start_pan)
self.canvas.bind("<B1-Motion>", self.do_pan)
# Add Home button
self.home_button = tk.Button(self, text="Home", command=self.reset_view)
self.home_button.pack()
self.last_x = 0
self.last_y = 0
def start_pan(self, event):
self.last_x = event.x
self.last_y = event.y
def do_pan(self, event):
dx = event.x - self.last_x
dy = event.y - self.last_y
self.canvas.move(tk.ALL, dx, dy)
self.last_x = event.x
self.last_y = event.y
def reset_view(self):
self.canvas.coords(self.image_id, 0, 0) # Reset image position to (0, 0)
if __name__ == "__main__":
app = ImagePanner(r".\data\51048_14_F2_RE_RS_51048_10_F2_RE_LS\51048_10_F2_RE_LS.jpg")
app.mainloop()