def new_figure_manager_given_figure(num, figure): """ Create a new figure manager instance for the given figure. """ _focus = windowing.FocusManager() window = Tk.Tk() window.withdraw() if Tk.TkVersion >= 8.5: # put a mpl icon on the window rather than the default tk icon. Tkinter # doesn't allow colour icons on linux systems, but tk >=8.5 has a iconphoto # command which we call directly. Source: # http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html icon_fname = os.path.join(rcParams['datapath'], 'images', 'matplotlib.ppm') icon_img = Tk.PhotoImage(file=icon_fname) try: window.tk.call('wm', 'iconphoto', window._w, icon_img) except (SystemExit, KeyboardInterrupt): # re-raise exit type Exceptions raise except: # log the failure, but carry on verbose.report('Could not load matplotlib icon: %s' % sys.exc_info()[1]) canvas = FigureCanvasTkAgg(figure, master=window) figManager = FigureManagerTkAgg(canvas, num, window) if matplotlib.is_interactive(): figManager.show() canvas.draw_idle() return figManager
def _Button(self, text, image_file, toggle, frame): if image_file is not None: im = Tk.PhotoImage(master=self, file=image_file) else: im = None if not toggle: b = Tk.Button(master=frame, text=text, padx=2, pady=2, image=im, command=lambda: self._button_click(text)) else: # There is a bug in tkinter included in some python 3.6 versions # that without this variable, produces a "visual" toggling of # other near checkbuttons # https://bugs.python.org/issue29402 # https://bugs.python.org/issue25684 var = Tk.IntVar() b = Tk.Checkbutton(master=frame, text=text, padx=2, pady=2, image=im, indicatoron=False, command=lambda: self._button_click(text), variable=var) b._ntimage = im b.pack(side=Tk.LEFT) return b
def resize(self, event): width, height = event.width, event.height if self._resize_callback is not None: self._resize_callback(event) # compute desired figure size in inches dpival = self.figure.dpi winch = width / dpival hinch = height / dpival self.figure.set_size_inches(winch, hinch, forward=False) self._tkcanvas.delete(self._tkphoto) self._tkphoto = Tk.PhotoImage(master=self._tkcanvas, width=int(width), height=int(height)) self._tkcanvas.create_image(int(width / 2), int(height / 2), image=self._tkphoto) self.resize_event() self.show() # a resizing will in general move the pointer position # relative to the canvas, so process it as a motion notify # event. An intended side effect of this call is to allow # window raises (which trigger a resize) to get the cursor # position to the mpl event framework so key presses which are # over the axes will work w/o clicks or explicit motion self._update_pointer_position(event)
def new_figure_manager_given_figure(num, figure): """ Create a new figure manager instance for the given figure. """ _focus = windowing.FocusManager() window = Tk.Tk(className="matplotlib") window.withdraw() # Put a mpl icon on the window rather than the default tk icon. # Tkinter doesn't allow colour icons on linux systems, but tk>=8.5 has # a iconphoto command which we call directly. Source: # http://mail.python.org/pipermail/tkinter-discuss/2006-November/000954.html icon_fname = os.path.join(rcParams['datapath'], 'images', 'matplotlib.ppm') icon_img = Tk.PhotoImage(file=icon_fname) try: window.tk.call('wm', 'foobar', window._w, icon_img) except Exception as exc: # log the failure (due e.g. to Tk version), but carry on verbose.report('Could not load matplotlib icon: %s' % exc) canvas = FigureCanvasTkAgg(figure, master=window) manager = FigureManagerTkAgg(canvas, num, window) if matplotlib.is_interactive(): manager.show() canvas.draw_idle() return manager
def createImage(width, height, fill, outline=None): image = tkinter.PhotoImage(width=width, height=height) assert image.width() == width assert image.height() == height image.blank() fillImage(image, fill, outline) return image
def _Button(self, text, file, command, extension='.gif'): img_file = os.path.join(rcParams['datapath'], 'images', file + extension) im = Tk.PhotoImage(master=self, file=img_file) b = Tk.Button( master=self, text=text, padx=2, pady=2, image=im, command=command) b._ntimage = im b.pack(side=Tk.LEFT) return b
def makeImage(file=None, data=None, dither=None, alpha=None): kw = {} if data is None: assert file is not None kw["file"] = file else: # assert data is not None kw["data"] = data if Image: # use PIL if file: im = PIL_Image(file) return im # fromstring(mode, size, data, decoder_name='raw', *args) else: return tkinter.PhotoImage(data=data) return tkinter.PhotoImage(**kw)
def getImage(self, fn): if fn in self.images: return self.images[fn] try: img = tkinter.PhotoImage(master=self.parent, file=fn) except Exception: img = None self.images[fn] = img return img
def test(aggimage): r = Tk.Tk() c = Tk.Canvas(r, width=aggimage.width, height=aggimage.height) c.pack() p = Tk.PhotoImage(width=aggimage.width, height=aggimage.height) blit(p, aggimage) c.create_image(aggimage.width,aggimage.height,image=p) blit(p, aggimage) while True: r.update_idletasks()
def _GuiLogo(self): """Creates the UI graphical logo.""" self.logo_frame = tk.Frame(self.root) self.logo_frame.columnconfigure(0, weight=1) r = resources.Resources() path = r.GetResourceFileName('logo.gif') self.logo_img = tk.PhotoImage(file=path) self.logo = tk.Label(self.logo_frame, image=self.logo_img, text='logo here') self.logo.grid(column=0, row=0, sticky='SE') self.logo_frame.grid(column=0, row=self.row, sticky='EW') self.row += 1
def _loadImage(self, name): file = os.path.join(self.dir, name) image = None for ext in IMAGE_EXTENSIONS: file = os.path.join(self.dir, name + ext) if os.path.isfile(file): if Image: image = ImageTk.PhotoImage(Image.open(file)) else: image = tkinter.PhotoImage(file=file) break return image
def copyImage(image, x, y, width, height): if Image: if isinstance(image, PIL_Image): return ImageTk.PhotoImage( image._pil_image.crop((x, y, x+width, y+height))) dest = tkinter.PhotoImage(width=width, height=height) assert dest.width() == width assert dest.height() == height dest.blank() image.tk.call(dest, "copy", image.name, "-from", x, y, x+width, y+height) assert dest.width() == width assert dest.height() == height return dest
def __init__(self, figure, master=None, resize_callback=None): super(FigureCanvasTk, self).__init__(figure) self._idle = True self._idle_callback = None t1, t2, w, h = self.figure.bbox.bounds w, h = int(w), int(h) self._tkcanvas = Tk.Canvas(master=master, background="white", width=w, height=h, borderwidth=0, highlightthickness=0) self._tkphoto = Tk.PhotoImage(master=self._tkcanvas, width=w, height=h) self._tkcanvas.create_image(w // 2, h // 2, image=self._tkphoto) self._resize_callback = resize_callback self._tkcanvas.bind("<Configure>", self.resize) self._tkcanvas.bind("<Key>", self.key_press) self._tkcanvas.bind("<Motion>", self.motion_notify_event) self._tkcanvas.bind("<Enter>", self.enter_notify_event) self._tkcanvas.bind("<Leave>", self.leave_notify_event) self._tkcanvas.bind("<KeyRelease>", self.key_release) for name in "<Button-1>", "<Button-2>", "<Button-3>": self._tkcanvas.bind(name, self.button_press_event) for name in "<Double-Button-1>", "<Double-Button-2>", "<Double-Button-3>": self._tkcanvas.bind(name, self.button_dblclick_event) for name in "<ButtonRelease-1>", "<ButtonRelease-2>", "<ButtonRelease-3>": self._tkcanvas.bind(name, self.button_release_event) # Mouse wheel on Linux generates button 4/5 events for name in "<Button-4>", "<Button-5>": self._tkcanvas.bind(name, self.scroll_event) # Mouse wheel for windows goes to the window with the focus. # Since the canvas won't usually have the focus, bind the # event to the window containing the canvas instead. # See http://wiki.tcl.tk/3893 (mousewheel) for details root = self._tkcanvas.winfo_toplevel() root.bind("<MouseWheel>", self.scroll_event_windows, "+") # Can't get destroy events by binding to _tkcanvas. Therefore, bind # to the window and filter. def filter_destroy(evt): if evt.widget is self._tkcanvas: self._master.update_idletasks() self.close_event() root.bind("<Destroy>", filter_destroy, "+") self._master = master self._tkcanvas.focus_set()
def _Button(self, text, image_file, toggle, frame): if image_file is not None: im = Tk.PhotoImage(master=self, file=image_file) else: im = None if not toggle: b = Tk.Button(master=frame, text=text, padx=2, pady=2, image=im, command=lambda: self._button_click(text)) else: b = Tk.Checkbutton(master=frame, text=text, padx=2, pady=2, image=im, indicatoron=False, command=lambda: self._button_click(text)) b._ntimage = im b.pack(side=Tk.LEFT) return b
def __init__(self, parent, image=None, **kwargs): tk.Frame.__init__(self, parent) self.parent = parent self.input_list = [] if not image: image = os.path.join('static', 'fileicon.png') self.photo = tk.PhotoImage(file=image) self.photo = self.photo.subsample(8) frame_ops = funcs.extract_args(kwargs, FRAME_KEYS, FRAME_KEY) frame = tk.Frame(self, frame_ops) frame.pack(fill='both', expand=True) label_ops = funcs.extract_args(kwargs, LABEL_KEYS, LABEL_KEY) label = tk.Label(frame, label_ops) tvar = tk.StringVar() entry_ops = funcs.extract_args(kwargs, ENTRY_KEYS, ENTRY_KEY) entry_ops['textvariable'] = tvar entry = tk.Entry(frame, entry_ops) self.input_list.append(entry) button_ops = funcs.extract_args(kwargs, BUTTON_KEYS, BUTTON_KEY) button_ops['command'] = self._open button_ops['image'] = self.photo file_button = tk.Button(frame, button_ops) self.input_list.append(file_button) label.pack(side='top', fill='x', expand=True) entry.pack(side='left', fill='x', expand=True) file_button.pack(side='right', expand=False) entry.focus_force() self.parent.columnconfigure(0, weight=1) self.insert = entry.insert self.delete = entry.delete self.get = entry.get self.set = tvar.set self.index = entry.index self.bind = entry.bind self.enable = self._enable self.normal = self._enable self.disable = self._disable self.clear = lambda: funcs.clear(entry)
def show_cardset(*args): global tk_images tk_images = [] if list_box.curselection(): cs_name = list_box.get(list_box.curselection()) cs = cardsets_dict[cs_name] ls = glob(os.path.join(cs.dir, '[0-9][0-9][a-z]' + cs.ext)) ls += glob(os.path.join(cs.dir, 'back*' + cs.ext)) if all_imgs: ls += glob(os.path.join(cs.dir, 'bottom*' + cs.ext)) ls += glob(os.path.join(cs.dir, 'l*' + cs.ext)) # ls = glob(os.path.join(cs.dir, '*.gif')) # if not ls: return ls.sort() pf = None x, y = 10, 10 width, height = 0, 0 canvas.delete('all') for f in ls: if Image: filter = { 'NEAREST': Image.NEAREST, 'BILINEAR': Image.BILINEAR, 'BICUBIC': Image.BICUBIC, 'ANTIALIAS': Image.ANTIALIAS, }[filter_var.get()] # filter = Image.BILINEAR # filter = Image.BICUBIC # filter = Image.ANTIALIAS # print f im = Image.open(f) if zoom != 0: w, h = im.size im = im.convert('RGBA') # for save transparency if rotate_var.get(): # rotate # if filter == Image.ANTIALIAS: # filter = Image.BICUBIC z = zoom * 5 a = abs(pi / 2 / 90 * z) neww = int(w * cos(a) + h * sin(a)) newh = int(h * cos(a) + w * sin(a)) # print w, h, neww, newh d = int(sqrt(w * w + h * h)) dx, dy = (d - w) / 2, (d - h) / 2 newim = Image.new('RGBA', (d, d)) newim.paste(im, (dx, dy)) im = newim im = im.rotate(z, resample=filter) x0, y0 = (d - neww) / 2, (d - newh) / 2 x1, y1 = d - x0, d - y0 im = im.crop((x0, y0, x1, y1)) t = str(z) else: # zoom z = 1.0 + zoom / 10.0 z = max(0.2, z) if 1: tmp = Image.new('RGBA', (w + 2, h + 2)) tmp.paste(im, (1, 1), im) im = tmp.resize((int(w * z), int(h * z)), resample=filter) else: im = im.resize((int(w * z), int(h * z)), resample=filter) t = '%d %%' % int(z * 100) zoom_label.config(text=t) else: zoom_label.config(text='') image = ImageTk.tkinter.PhotoImage(im) else: image = tkinter.PhotoImage(file=f) tk_images.append(image) ff = os.path.split(f)[1] if pf is None: pf = ff[:2] x, y = 10, 10 elif ff[:2] != pf: pf = ff[:2] x = 10 y += image.height() + 10 else: x += image.width() + 10 canvas.create_image(x, y, image=image, anchor=tkinter.NW) # canvas.create_rectangle(x, y, x+image.width(), y+image.height()) width = max(width, x) height = max(height, y) width, height = width + image.width() + 10, height + image.height( ) + 10 canvas.config(scrollregion=(0, 0, width, height)) # print image.width(), image.height() label.config(text='''\ Name: %s Type: %s Directory: %s''' % (cs.name, cs.type, cs.dir))