Skip to content

Instantly share code, notes, and snippets.

@gph03n1x
Last active December 21, 2015 10:49
Show Gist options
  • Save gph03n1x/6295009 to your computer and use it in GitHub Desktop.
Save gph03n1x/6295009 to your computer and use it in GitHub Desktop.
Minimalistic Library for tkinter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Tkinter import *
from Queue import Queue
import threading
class Tk_thread_safe(object):
"""Makes Sure that your app isn't going to crash due to tclstack errors"""
def __init__(self, tk_root):
self.tk_root = tk_root
self.queue = Queue()
self.tk_loop()
self.tk_root.mainloop()
def tk_loop(self):
try:
while True:
f, a, k = self.queue.get_nowait()
f(*a, **k)
except Exception:
root.after(500, self.tk_loop)
def new_thread(self, process):
self.process = process
threading.Thread(self.queue.put(self.process)).start()
def tk_destruct(_root_):
"""Removes all current widgets on the gui """
try:
for child in _root_.winfo_children():
child.destroy()
except:
pass
class Image_loader(object):
"""Loads images on widgets and caches them """
def __init__(self):
self._image_ = ""
self.images_list = {}
def load_one(self, _image_):
self._image_ = _image_
self.images_list[self._image_] = PhotoImage(file=self._image_)
def set_image(self, _widget_, _image_):
self._widget_ = _widget_
self._image_ = _image_
if not(self._image_ in self.images_list):
if self.load_one(self._image_) is False:
raise(IOError)
try:
self._widget_.image = self.images_list[self._image_]
self._widget_.config(image=self.images_list[self._image_])
except:
return False
else:
return True
class Hover_Effect(object):
"""Creates a hover effect (used on Buttons and labels with images)"""
def tk_config_bind(self, _widget_, bind_target, _command_):
self.command = _command_
self.__widget__ = _widget_
self.bind_targer = bind_target
try:
self.__widget__.bind(self.bind_targer, self.command)
except:
return False
def set_hover(self, _widget_, _enter_, _leave_):
self._widget_ = _widget_
self._enter_ = _enter_
self._leave_ = _leave_
self.tk_config_bind(self._widget_, "<Enter>",
lambda event: event.widget.config(background=self._enter_))
self.tk_config_bind(self._widget_, "<Leave>",
lambda event: event.widget.config(background=self._leave_))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment