Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- from Tkinter import *
- except:
- from tkinter import *
- import tkinter.ttk as ttk
- import PIL.Image, PIL.ImageTk, PIL.ImageDraw, PIL.ImageFont
- from itertools import count
- import threading, os, sys, re
- from datetime import datetime, date, time, timedelta
- class ValidatingEntry(Entry): ##used for MaxLengthEntry
- def __init__(self, master, value="", **kw):
- #apply(Entry.__init__, (self, master), kw)
- Entry.__init__(*(self, master), **kw)
- self.__value = value
- self.__variable = StringVar()
- self.__variable.set(value)
- self.__variable.trace("w", self.__callback)
- self.config(textvariable=self.__variable)
- def __callback(self, *dummy):
- value = self.__variable.get()
- newvalue = self.validate(value)
- if newvalue is None:
- self.__variable.set(self.__value)
- elif newvalue != value:
- self.__value = newvalue
- self.__variable.set(newvalue)
- else:
- self.__value = value
- def validate(self, value):
- # override: return value, new value, or None if invalid
- return value
- class MaxLengthEntry(ValidatingEntry): ##entry with a max length flag
- def __init__(self, master, value="", maxlength=None, valtype=None, **kw):
- self.maxlength = maxlength
- self.valtype = valtype
- #apply(ValidatingEntry.__init__, (self, master), kw)
- ValidatingEntry.__init__(*(self, master), **kw)
- def validate(self, value):
- if self.maxlength is None or len(value) <= self.maxlength:
- if self.valtype != None:
- if self.valtype == "int":
- try:
- x=int(value)
- return value
- except:
- return ""
- elif self.valtype == "int/hour":
- try:
- x=int(value)
- if x < 24:
- return value
- else:
- return ""
- except:
- return ""
- elif self.valtype == "int/minute":
- try:
- x=int(value)
- if x < 60:
- return value
- else:
- return ""
- except:
- return ""
- elif self.valtype == "int/currency":
- try:
- #x = float(value)
- if len(value.split(".")) == 2:
- if len(value.split(".")[1]) > 2:
- return value[:-1]
- else:
- return value
- elif len(value.split(".")) > 2:
- return ""
- else:
- return value
- except:
- return ""
- elif self.valtype == "str":
- try:
- x=int(value)
- return value
- except:
- return ""
- else:
- return ""
- else:
- return None
- return None # new value too long
- class IconBuilder:
- def __init__(self, exe_path):
- self.exe_path = exe_path
- self.icons = {}
- self.gen_icons()
- def create_icon(self, url, w=32, h=32):
- im = PIL.Image.open(self.exe_path+url)
- im = im.resize((w, h), PIL.Image.ANTIALIAS)
- icon = PIL.ImageTk.PhotoImage(im)
- return icon
- def gen_icons(self):
- icons = {"Logo": self.create_icon("\\assets\\Logo.gif"),
- "profile": self.create_icon("\\assets\\profile.gif", w=128, h=128),
- "gtick": self.create_icon("\\assets\\gtick.gif", w=16, h=16),
- "rcross": self.create_icon("\\assets\\rcross.gif", w=16, h=16),
- "+": self.create_icon("\\assets\\add.gif", w=24, h=24),
- "+x16": self.create_icon("\\assets\\add.gif", w=24, h=24),
- "edit": self.create_icon("\\assets\\edit.gif", w=24, h=24),
- "print": self.create_icon("\\assets\\print.gif", w=24, h=24),
- "save": self.create_icon("\\assets\\save.gif", w=24, h=24),
- "cancel": self.create_icon("\\assets\\cancel.gif", w=24, h=24),
- "search": self.create_icon("\\assets\\search.gif", w=32, h=24),
- "back": self.create_icon("\\assets\\back.gif", w=32, h=24),
- "settings": self.create_icon("\\assets\\settings.gif", w=32, h=24),
- "refresh": self.create_icon("\\assets\\refresh.gif", w=32, h=24),
- "arrow_left": self.create_icon("\\assets\\arrow_left.gif", w=32, h=32),
- "arrow_right": self.create_icon("\\assets\\arrow_right.gif", w=16, h=16),
- "delete": self.create_icon("\\assets\\delete.gif", w=16, h=16),
- "casenote": self.create_icon("\\assets\\casenote.gif", w=16, h=16),
- "filter": self.create_icon("\\assets\\filter.gif", w=16, h=16),
- "LED_RED": self.create_icon("\\assets\\LED_RED.gif", w=16, h=16),
- "LED_AMBER": self.create_icon("\\assets\\LED_AMBER.gif", w=16, h=16),
- "LED_GREEN": self.create_icon("\\assets\\LED_GREEN.gif", w=16, h=16)
- }
- self.icons = icons
- def get_icons(self):
- return self.icons
- pass
- class ImageLabel(Label): #for animated gifs
- def load(self, im):
- if isinstance(im, str):
- im = PIL.Image.open(im)
- self.loc = 0
- self.frames = []
- try:
- for i in count(1):
- self.frames.append(PIL.ImageTk.PhotoImage(im.copy()))
- im.seek(i)
- except EOFError:
- pass
- try:
- self.delay = im.info['duration']
- except:
- self.delay = 100
- if len(self.frames) == 1:
- self.config(image=self.frames[0])
- else:
- self.next_frame()
- def unload(self):
- self.config(image=None)
- self.frames = None
- def next_frame(self):
- if self.frames:
- self.loc += 1
- self.loc %= len(self.frames)
- self.config(image=self.frames[self.loc])
- self.after(self.delay, self.next_frame)
- class Tooltip:
- def __init__(self, widget,*,bg='#FFFFEA',pad=(5, 3, 5, 3),text='widget info',waittime=400,wraplength=250):
- self.waittime = waittime
- self.wraplength = wraplength
- self.widget = widget
- self.text = text
- self.widget.bind("<Enter>", self.onEnter)
- self.widget.bind("<Leave>", self.onLeave)
- self.widget.bind("<ButtonPress>", self.onLeave)
- self.bg = bg
- self.pad = pad
- self.id = None
- self.tw = None
- def onEnter(self, event=None):
- self.schedule()
- pass
- def onLeave(self, event=None):
- self.unschedule()
- self.hide()
- def schedule(self):
- self.unschedule()
- self.id = self.widget.after(self.waittime, self.show)
- def unschedule(self):
- id_ = self.id
- self.id = None
- if id_:
- self.widget.after_cancel(id_)
- def show(self):
- def tip_pos_calculator(widget, label,*,tip_delta=(10, 5), pad=(5, 3, 5, 3)):
- w = widget
- s_width, s_height = w.winfo_screenwidth(), w.winfo_screenheight()
- width, height = (pad[0] + label.winfo_reqwidth() + pad[2],pad[1] + label.winfo_reqheight() + pad[3])
- mouse_x, mouse_y = w.winfo_pointerxy()
- x1, y1 = mouse_x + tip_delta[0], mouse_y + tip_delta[1]
- x2, y2 = x1 + width, y1 + height
- x_delta = x2 - s_width
- if x_delta < 0:
- x_delta = 0
- y_delta = y2 - s_height
- if y_delta < 0:
- y_delta = 0
- offscreen = (x_delta, y_delta) != (0, 0)
- if offscreen:
- if x_delta:
- x1 = mouse_x - tip_delta[0] - width
- if y_delta:
- y1 = mouse_y - tip_delta[1] - height
- offscreen_again = y1 < 0
- if offscreen_again:
- y1 = 0
- return x1, y1
- bg = self.bg
- pad = self.pad
- widget = self.widget
- self.tw = Toplevel(widget)
- self.tw.wm_overrideredirect(True)
- win = Frame(self.tw,background=bg,borderwidth=0)
- label = ttk.Label(win,text=self.text,justify=LEFT,background=bg,relief=SOLID,borderwidth=0,wraplength=self.wraplength)
- label.grid(padx=(pad[0], pad[2]),pady=(pad[1], pad[3]),sticky=NSEW)
- win.grid()
- x, y = tip_pos_calculator(widget, label)
- self.tw.wm_geometry("+%d+%d" % (x, y))
- def hide(self):
- tw = self.tw
- if tw:
- tw.destroy()
- self.tw = None
- class BaseThread(threading.Thread):
- def __init__(self, callback=None, callback_args=None, *args, **kwargs):
- target = kwargs.pop('target')
- super(BaseThread, self).__init__(target=self.target_with_callback, *args, **kwargs)
- self.callback = callback
- self.method = target
- self.callback_args = callback_args
- def target_with_callback(self):
- self.method()
- if self.callback is not None:
- self.callback(*self.callback_args)
- class SafeVars:
- def __init__(self):
- self.strtype = type("test")
- self.inttype = type(0)
- self.datetype = type(datetime.now())
- def safe_var(self, userinput):
- if type(userinput) == self.strtype:
- output = self.safe_string(userinput)
- elif type(userinput) == self.inttype:
- output = self.safe_int(userinput)
- elif type(userinput) == self.datetype:
- output = self.safe_date(userinput)
- else:
- output = userinput
- return output
- def decode_vars(self, userinput):
- if type(userinput) == self.strtype:
- userinput = userinput.encode().decode('unicode-escape')
- #userinput = userinput.encode().decode('unicode-escape')
- output = userinput.encode("latin1").decode("UTF-8")
- elif type(userinput) == self.inttype:
- output = self.safe_int(userinput)
- elif type(userinput) == self.datetype:
- output = self.safe_date(userinput)
- else:
- output = userinput
- return output
- def safe_string(self, userinput):
- output = str(userinput)
- output = output.encode('UTF-8')
- output = str(output).replace("\\\\", "")[2:-1].replace("\\x", "\\\\x")
- output = output.replace("\\'", "'")
- output = output.replace('"', '\\\\\\"')
- output = output.replace("'", "\\\\\\'")
- return output
- def safe_int(self, userinput):
- output = userinput
- return output
- def safe_date(self, userinput):
- return userinput
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement