Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def no():
- pass
- class TkUtils:
- def __init__(self, log, root):
- import Tkinter
- import threading
- import time
- self.time = time
- self.threading = threading
- self.TkUtil = Tkinter
- self.log = log
- self.root = root
- self.active_window = None
- self.active_side = None
- self.active_scroll = None
- self.run = False
- 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
- try:
- b = self.TkUtil.Button(element, borderwidth=borderwidth, fg=fg, bg=bg, text=text, padx=padx, pady=pady, relief=relief, command=command)
- b.pack(side=side, expand=expand, fill=fill)
- return b
- except Exception as err:
- self.log.error("cFrame failed\n%s" %err)
- 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
- try:
- f = self.TkUtil.Frame(element, bg=bg, borderwidth=borderwidth, relief=relief, height=height, width=width, image=image)
- f.pack(side=side, padx=padx, pady=pady, expand=expand, fill=fill)
- return f
- except Exception as err:
- self.log.error("cFrame failed\n%s" %err)
- def create_overlay(self, over, event, title, height=400, width=400):
- if self.active_window != None:
- self.clear_overlay(self.TkUtil.Event())
- self.active_window = None
- self.root.update_idletasks()
- self.root.update()
- window = self.TkUtil.Canvas(over, width=width, height=height, bg="white", relief="groove", highlightbackground="black", highlightcolor="black", highlightthickness=1, borderwidth=4)
- window.pack_propagate(False)
- window.place(x=self.root.winfo_width()/2-(width/2), y=(self.root.winfo_height()/2)-(height/4))
- self.root.update_idletasks()
- self.root.update()
- spacer = self.cFrame(window, padx=7, pady=7, fill=self.TkUtil.BOTH)
- Title = self.TkUtil.Label(spacer, text=title, justify=self.TkUtil.RIGHT, font=("Helvetica", 10), bg="#EEEEEE", padx=10, pady=10, cursor="fleur")
- Title.pack(side=self.TkUtil.LEFT, expand=1, fill=self.TkUtil.X)
- Title.bind("<ButtonPress-1>", lambda e=self.TkUtil.Event(), w=window, run=True: self.move_window(e, w, run))
- Title.bind("<ButtonRelease-1>", lambda e=self.TkUtil.Event(), w=window, run=False: self.move_window(e, w, run))
- 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))
- spacer = self.cFrame(window, side=self.TkUtil.BOTTOM, pady=3)
- #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))
- self.set_pos(window)
- self.active_window = window
- return "break"
- def clear_overlay(self, event):
- if self.active_window != None:
- self.active_window.destroy()
- self.time.sleep(0.1)
- return "break"
- def overlay(self, over, event, title, height=400, width=400):
- if self.active_window == None:
- self.create_overlay(over, event, title, width=width, height=height)
- return self.active_window
- else:
- self.clear_overlay(self.TkUtil.Event())
- self.create_overlay(over, event, title, width=width, height=height)
- return self.active_window
- def move_window(self, event, window, run):
- self.run = run
- if self.run:
- t= self.threading.Thread(target=self.move_window_thread,args=(window, event))
- t.start()
- def move_window_thread(self, window, event):
- while self.run:
- self.root.update_idletasks()
- self.root.update()
- try:
- if(self.root.winfo_pointerx() != None and self.root.winfo_pointery() != None):
- 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)
- except Exception as err:
- print(err)
- pass
- self.time.sleep(0.025)
- def set_pos(self, window):
- window.place(x=(self.root.winfo_width()/2)-window.winfo_width()/2, y=self.root.winfo_height()/2-window.winfo_height()/2)
- def scrollable_area2(self, holder):
- base_frame = self.cFrame(holder, fill=self.TkUtil.BOTH, expand=1)
- base_frame.rowconfigure(0, weight=0)
- base_frame.columnconfigure(0, weight=1)
- can = self.TkUtil.Canvas(base_frame, bg="white")
- can.pack(side=self.TkUtil.LEFT, expand=1, fill=self.TkUtil.BOTH)
- scrollArea = self.cFrame(can, bg="white", side=self.TkUtil.LEFT, expand=1, fill=self.TkUtil.BOTH)
- can.create_window(0, 0, window=scrollArea, anchor='nw')
- Scroll = self.TkUtil.Scrollbar(base_frame, orient=self.TkUtil.VERTICAL)
- Scroll.config(command=can.yview)
- Scroll.pack(side=self.TkUtil.RIGHT, fill=self.TkUtil.Y)
- can.config(yscrollcommand=Scroll.set)
- scrollArea.bind("<Configure>", lambda e=self.TkUtil.Event(), c=can: self.update_scrollregion(e, c))
- base_frame.bind("<Enter>", lambda e=self.TkUtil.Event():self.set_active(e, can))
- base_frame.bind("<Leave>", lambda e=self.TkUtil.Event():self.unset_active(e))
- return scrollArea, can
- def set_active(self, event, canvas):
- self.active_scroll = canvas
- def unset_active(self, event):
- self.active_scroll = None
- def reset_scroll(self, element):
- self.root.nametowidget(element.winfo_parent()).yview_moveto(0)
- self.root.nametowidget(element.winfo_parent()).yview_scroll(0, "units")
- def _on_mousescroll(self, event):
- if self.active_scroll != None:
- self.active_scroll.yview_scroll(-1*(event.delta/120), "units")
- def update_scrollregion(self, event, can):
- can.configure(scrollregion=can.bbox("all"))
- def OnCanvasConfigure(self, event, can, scroll):
- canvas_width = event.width
- can.itemconfig(scroll, width = canvas_width)
- def test_buttons(self, main, defaultIcon):
- self.root.update_idletasks()
- self.root.update()
- for i in range(20):
- Packer = self.cFrame(main, bg="white", borderwidth=2, width=400, relief=self.TkUtil.RAISED, fill=self.TkUtil.X, expand=1)
- L= self.TkUtil.Label(Packer, anchor=self.TkUtil.W, justify=self.TkUtil.LEFT, text="Test %s"%i, bg="red", borderwidth=0)
- L.pack(side=self.TkUtil.LEFT, fill=self.TkUtil.X)
- L2= self.TkUtil.Label(Packer, anchor=self.TkUtil.W, image=defaultIcon)
- L2.pack(side=self.TkUtil.RIGHT)
- class Log: #class to write to log file with time stamps
- def __init__(self):
- import os, traceback, time, sys
- self.traceback = traceback
- self.sys = sys
- self.os = os
- self.time = time
- if getattr(self.sys, 'frozen', False): #windows path fix
- self.exe = self.os.path.dirname(self.sys.executable)
- elif __file__:
- self.exe = self.os.path.dirname(__file__)
- self.fname = self.os.path.join(self.exe, "debug.log")
- self.logfile = None
- def error(self, error):
- exc_type, exc_obj, exc_tb = self.sys.exc_info()
- trace_stack = self.traceback.extract_tb(exc_tb)[-1]
- 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])
- self.logfile = open(self.fname, "a+")
- strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
- self.logfile.write("error: %s, %s, %s\r" %(strtime, error, trace_format))
- self.logfile.close()
- self.logfile = None
- def log(self, log):
- self.logfile = open(self.fname, "a+")
- strtime = str(self.time.strftime("%d-%m-%Y,(%z),%H:%M:%S"))
- self.logfile.write("log: %s, %s\r" %(strtime, log))
- self.logfile.close()
- self.logfile = None
- class Error(Exception):
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement