Created
September 7, 2011 23:51
-
-
Save basicxman/1202204 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from Tkinter import * | |
| from PIL import Image, ImageTk | |
| class Banana(object): | |
| def __init__(self, root): | |
| self.img = self.dimg = Image.open("im_a_banana.jpg") | |
| self.dimensions = self.dd = self.img.size | |
| self.root = root | |
| self.l = Label(self.root) | |
| self.redraw_image() | |
| def redraw_image(self): | |
| self.the_proper_banana = ImageTk.PhotoImage(self.img) | |
| self.l.configure(image=self.the_proper_banana) | |
| def resize(self, width, height): | |
| self.img = self.dimg.resize((width, height), Image.ANTIALIAS) | |
| self.redraw_image() | |
| def resize_with_dimensions(self): | |
| self.resize(self.dimensions[0], self.dimensions[1]) | |
| def zoom_in(self): | |
| self.dimensions = map(lambda x: int(x * 1.1), self.dimensions) | |
| self.resize_with_dimensions() | |
| def zoom_out(self): | |
| self.dimensions = map(lambda x: int(x * 0.9), self.dimensions) | |
| self.resize_with_dimensions() | |
| def regular_zoom(self): | |
| self.dimensions = self.dd | |
| self.img = self.dimg | |
| self.resize_with_dimensions() | |
| root = Tk() | |
| root.title("I'm a banana!") | |
| root.minsize(400, 300) | |
| root.aspect(4, 3, 4, 3) | |
| f = Frame(root, padx=50, pady=50, width=400, height=300) | |
| f.pack() | |
| banana = Banana(f) | |
| in_b = Button(f, text="Zoom In", command=banana.zoom_in) | |
| out_b = Button(f, text="Zoom Out", command=banana.zoom_out) | |
| reg_b = Button(f, text="Regular Zoom", command=banana.regular_zoom) | |
| banana.l.grid(row=0, columnspan=3, column=1) | |
| in_b.grid(row=1, column=1) | |
| reg_b.grid(row=1, column=2) | |
| out_b.grid(row=1, column=3) | |
| root.bind("=", lambda e: banana.zoom_in()) | |
| root.bind("-", lambda e: banana.zoom_out()) | |
| root.bind("<space>", lambda e: banana.regular_zoom()) | |
| root.bind("q", lambda e: e.widget.quit()) | |
| root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment