Advertisement
AceScottie

aceutils.py

Nov 15th, 2018
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.39 KB | None | 0 0
  1. def no():
  2.     pass
  3. class TkUtils:
  4.     def __init__(self, log, root):
  5.         import Tkinter
  6.         import threading
  7.         import time
  8.         self.time = time
  9.         self.threading = threading
  10.         self.TkUtil = Tkinter
  11.         self.log = log
  12.         self.root = root
  13.         self.active_window = None
  14.         self.active_side = None
  15.         self.active_scroll = None
  16.         self.run = False
  17.  
  18.  
  19.     def cButton(self, element, borderwidth=0, fg="#07a501", bg="white", text="", relief="groove", padx=0, pady=0, command=no(), side=None, expand=0, fill=None): #basic button
  20.         try:
  21.             b = self.TkUtil.Button(element, borderwidth=borderwidth, fg=fg, bg=bg, text=text, padx=padx, pady=pady, relief=relief, command=command)
  22.             b.pack(side=side, expand=expand, fill=fill)
  23.             return b
  24.         except Exception as err:
  25.             self.log.error("cFrame failed\n%s" %err)       
  26.     def cFrame(self, element, bg="white", borderwidth=0, relief="groove", side=None, padx=0, pady=0, height=0, width=0, expand=0, fill=None, image=None):#basic frame
  27.         try:
  28.             f = self.TkUtil.Frame(element, bg=bg, borderwidth=borderwidth, relief=relief, height=height, width=width, image=image)
  29.             f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
  30.             return f
  31.         except Exception as err:
  32.             self.log.error("cFrame failed\n%s" %err)
  33.    
  34.     def create_overlay(self, over, event, title, height=400, width=400):
  35.         if self.active_window != None:
  36.             self.clear_overlay(self.TkUtil.Event())
  37.             self.active_window = None
  38.         self.root.update_idletasks()
  39.         self.root.update()
  40.         window = self.TkUtil.Canvas(over, width=width, height=height, bg="white", relief="groove", highlightbackground="black", highlightcolor="black", highlightthickness=1, borderwidth=4)
  41.         window.pack_propagate(False)
  42.         window.place(x=self.root.winfo_width()/2-(width/2), y=(self.root.winfo_height()/2)-(height/4))
  43.         self.root.update_idletasks()
  44.         self.root.update()
  45.         spacer = self.cFrame(window, padx=7, pady=7, fill=self.TkUtil.BOTH)
  46.         Title = self.TkUtil.Label(spacer, text=title, justify=self.TkUtil.RIGHT, font=("Helvetica", 10), bg="#EEEEEE", padx=10, pady=10, cursor="fleur")
  47.         Title.pack(side=self.TkUtil.LEFT,  expand=1, fill=self.TkUtil.X)
  48.         Title.bind("<ButtonPress-1>", lambda e=self.TkUtil.Event(), w=window, run=True: self.move_window(e, w, run))
  49.         Title.bind("<ButtonRelease-1>", lambda e=self.TkUtil.Event(), w=window, run=False: self.move_window(e, w, run))
  50.         close = self.cButton(spacer, text="X", relief="raised", borderwidth=1, fg="black", side=self.TkUtil.RIGHT, command=lambda e=self.TkUtil.Event(): self.clear_overlay(e))
  51.         spacer = self.cFrame(window, side=self.TkUtil.BOTTOM, pady=3)
  52.         #close = self.cButton(window, text="close", relief="raised", borderwidth=1, fg="red", side=self.TkUtil.BOTTOM, command=lambda e=self.TkUtil.Event(): self.clear_overlay(e))
  53.         self.set_pos(window)
  54.         self.active_window = window
  55.         return "break"
  56.     def clear_overlay(self, event):
  57.         if self.active_window != None:
  58.             self.active_window.destroy()
  59.             self.time.sleep(0.1)
  60.         return "break"
  61.     def overlay(self, over, event, title, height=400, width=400):
  62.         if self.active_window == None:
  63.             self.create_overlay(over, event, title, width=width, height=height)
  64.             return self.active_window
  65.         else:
  66.             self.clear_overlay(self.TkUtil.Event())
  67.             self.create_overlay(over, event, title, width=width, height=height)
  68.             return self.active_window
  69.     def move_window(self, event, window, run):
  70.         self.run = run
  71.         if self.run:
  72.             t= self.threading.Thread(target=self.move_window_thread,args=(window, event))
  73.             t.start()
  74.     def move_window_thread(self, window, event):
  75.         while self.run:
  76.             self.root.update_idletasks()
  77.             self.root.update()
  78.             try:
  79.                 if(self.root.winfo_pointerx() != None and self.root.winfo_pointery() != None):
  80.                     window.place(x=(self.root.winfo_pointerx() - self.root.winfo_rootx())-window.winfo_width()/2, y=self.root.winfo_pointery() - self.root.winfo_rooty()-10)
  81.             except Exception as err:
  82.                 print(err)
  83.                 pass
  84.             self.time.sleep(0.025)     
  85.     def set_pos(self, window):
  86.         window.place(x=(self.root.winfo_width()/2)-window.winfo_width()/2, y=self.root.winfo_height()/2-window.winfo_height()/2)
  87.  
  88.     def scrollable_area2(self, holder):
  89.         base_frame = self.cFrame(holder, fill=self.TkUtil.BOTH, expand=1)
  90.         base_frame.rowconfigure(0, weight=0)
  91.         base_frame.columnconfigure(0, weight=1)
  92.         can = self.TkUtil.Canvas(base_frame, bg="white")
  93.         can.pack(side=self.TkUtil.LEFT, expand=1, fill=self.TkUtil.BOTH)
  94.         scrollArea = self.cFrame(can, bg="white", side=self.TkUtil.LEFT, expand=1, fill=self.TkUtil.BOTH)
  95.         can.create_window(0, 0, window=scrollArea, anchor='nw')
  96.         Scroll = self.TkUtil.Scrollbar(base_frame, orient=self.TkUtil.VERTICAL)
  97.         Scroll.config(command=can.yview)
  98.         Scroll.pack(side=self.TkUtil.RIGHT, fill=self.TkUtil.Y)
  99.         can.config(yscrollcommand=Scroll.set)
  100.         scrollArea.bind("<Configure>",  lambda e=self.TkUtil.Event(), c=can: self.update_scrollregion(e, c))
  101.         base_frame.bind("<Enter>", lambda e=self.TkUtil.Event():self.set_active(e, can))
  102.         base_frame.bind("<Leave>", lambda e=self.TkUtil.Event():self.unset_active(e))
  103.         return scrollArea, can
  104.        
  105.     def set_active(self, event, canvas):
  106.         self.active_scroll = canvas
  107.     def unset_active(self, event):
  108.         self.active_scroll = None
  109.     def reset_scroll(self, element):
  110.         self.root.nametowidget(element.winfo_parent()).yview_moveto(0)
  111.         self.root.nametowidget(element.winfo_parent()).yview_scroll(0, "units")
  112.     def _on_mousescroll(self, event):
  113.         if self.active_scroll != None:
  114.             self.active_scroll.yview_scroll(-1*(event.delta/120), "units")
  115.     def update_scrollregion(self, event, can):
  116.         can.configure(scrollregion=can.bbox("all"))
  117.     def OnCanvasConfigure(self, event, can, scroll):
  118.         canvas_width = event.width
  119.         can.itemconfig(scroll, width = canvas_width)
  120.     def test_buttons(self, main, defaultIcon):
  121.         self.root.update_idletasks()
  122.         self.root.update()
  123.         for i in range(20):
  124.             Packer = self.cFrame(main, bg="white", borderwidth=2, width=400, relief=self.TkUtil.RAISED, fill=self.TkUtil.X, expand=1)
  125.             L= self.TkUtil.Label(Packer, anchor=self.TkUtil.W, justify=self.TkUtil.LEFT, text="Test %s"%i, bg="red", borderwidth=0)
  126.             L.pack(side=self.TkUtil.LEFT, fill=self.TkUtil.X)
  127.             L2= self.TkUtil.Label(Packer, anchor=self.TkUtil.W, image=defaultIcon)
  128.             L2.pack(side=self.TkUtil.RIGHT)
  129.  
  130. class Log: #class to write to log file with time stamps
  131.     def __init__(self):
  132.         import os, traceback, time, sys
  133.         self.traceback = traceback
  134.         self.sys = sys
  135.         self.os = os
  136.         self.time = time
  137.         if getattr(self.sys, 'frozen', False): #windows path fix
  138.             self.exe = self.os.path.dirname(self.sys.executable)
  139.         elif __file__:
  140.             self.exe = self.os.path.dirname(__file__)
  141.         self.fname = self.os.path.join(self.exe, "debug.log")  
  142.         self.logfile = None
  143.     def error(self, error):
  144.         exc_type, exc_obj, exc_tb = self.sys.exc_info()
  145.         trace_stack = self.traceback.extract_tb(exc_tb)[-1]
  146.         trace_format = "Error in file "+str(trace_stack[0])+"\r     on line "+str(trace_stack[1])+", from module '"+str(trace_stack[2])+"'\r        "+str(trace_stack[3])
  147.         self.logfile = open(self.fname, "a+")
  148.         strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
  149.         self.logfile.write("error: %s, %s, %s\r" %(strtime, error, trace_format))
  150.         self.logfile.close()
  151.         self.logfile = None
  152.     def log(self, log):
  153.         self.logfile = open(self.fname, "a+")
  154.         strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
  155.         self.logfile.write("log: %s, %s\r" %(strtime, log))
  156.         self.logfile.close()
  157.         self.logfile = None
  158. class Error(Exception):
  159.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement