Created
March 11, 2024 14:50
-
-
Save APN-Pucky/687fe69b017ad8eb2b1eb313b43970fa to your computer and use it in GitHub Desktop.
display images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg | |
import tkinter as tk | |
# Sample data: dictionary mapping names to image paths | |
image_data = { | |
'Image 1': 'image1.jpg', | |
'Image 2': 'image2.jpg', | |
'Image 3': 'image3.jpg' | |
} | |
class ImageSelectorApp: | |
def __init__(self, master): | |
self.master = master | |
self.master.title('Image Selector') | |
self.fig, self.ax = plt.subplots() | |
self.canvas = FigureCanvasTkAgg(self.fig, master=self.master) | |
self.canvas.get_tk_widget().pack(side='right', fill='both', expand=True) | |
self.dropdown_var = tk.StringVar(master) | |
self.dropdown_var.set(list(image_data.keys())[0]) # Set default selection | |
self.dropdown_var.trace('w', self.update_image) | |
self.dropdown = tk.OptionMenu(self.master, self.dropdown_var, *image_data.keys()) | |
self.dropdown.pack(side='left', fill='x') | |
self.update_image() | |
def update_image(self, *args): | |
selected_image_name = self.dropdown_var.get() | |
image_path = image_data[selected_image_name] | |
# Clear previous image | |
self.ax.clear() | |
# Load and display the new image | |
img = plt.imread(image_path) | |
self.ax.imshow(img) | |
self.ax.axis('off') | |
# Refresh canvas | |
self.canvas.draw() | |
def main(): | |
root = tk.Tk() | |
app = ImageSelectorApp(root) | |
root.mainloop() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment