#!/usr/bin/python3 import tkinter as tk from tkinter import font import time #import _thread as thread import os def tk_btn_bg_loop(btn,c1="#00ffdd",c2="#00ffff",stime=time.time()): while time.time() < stime+10: time.sleep(0.1) #time.sleep(10) c1b = c1 c2b = c2 print("tk_btn_loop",btn,c1,c2,"sleep 20") flip = 0 change = 0 t_last = time.time() try: while 1: #t = int(time.time()*1000) #if t % 500 == 0: # change = 1 # time.sleep(.001) #else: # time.sleep(.001) # continue change = 1 time.sleep(0.5) if change: if os.path.isfile("/home/user/LibreLight/blink"): c1 = "#0f0" c2 = "#00f" else: c1 = c1b #"#0f0" c2 = c2b #"#00f" #print(btn,"change",str(t)[:-3],btn["text"]) if flip:flip = 0 else:flip = 1 if flip:c = c1 else:c = c2 btn["bg"] = c except Exception as e: print(__file__,"loop() exception") print(e) time.sleep(3) #usage #thread.start_new_thread(tk_btn_bg_loop,(b,)) class MiniButton: def __init__(self,root,width=72,height=38,text="button"): self.text=text self.rb = tk.Frame(root, highlightbackground = "lightgrey", highlightthickness = 1, bd=0) self.bb = tk.Canvas(self.rb, highlightbackground = "black", highlightthickness = 1, bd=1,relief=tk.RAISED) self.bb.configure(width=width, height=height) self.fg = "#002" self.fx = 0 self.label = [] self.bind("", self.on_enter) self.bind("", self.on_leave) # !! BLOCK's other bindings like GO #self.bind("", self.on_b1) #self.bind("", self.on_press) #self.bind("", self.on_release) #self.bind("", self.on_release) self._last_label_id = 0 self._label_ring = ["labelA","labelB"] self.activebackground="lightgrey" self.defaultBackground="grey" def on_b1(self, e): print("on_b1",e) #self.bb.config(background=self.activebackground) self.bb.config(relief=tk.SUNKEN)#abackground=self.activebackground) return 1 def on_press(self, e): print("on_press",e) #self.bb.config(background=self.activebackground) self.bb.config(relief=tk.SUNKEN)#abackground=self.activebackground) return 1 def on_release(self, e): print("on_release",e) #self.bb.config(background=self.activebackground) self.bb.config(relief=tk.RAISED)#abackground=self.activebackground) return 1 def on_enter(self, e): #print("on_enter",e) #self.bb.config(background=self.activebackground) self.bb.config(relief=tk.FLAT)#abackground=self.activebackground) return 1 def on_leave(self, e): #print("on_leave",e) self.bb.config(background=self.defaultBackground) self.bb.config(relief=tk.RAISED)#abackground=self.activebackground) return 1 def _label(self,text="1\n2\n3\n"): z = 0 tag = self._label_ring[self._last_label_id] #self.bb.delete("label") self.label = [] for t in text.split("\n"): self.l = self.bb.create_text(37,z*10+9,text=t,anchor="c",tag=tag) #self.l["color"] = self.fg self.label.append(self.l) z+=1 self.delete_tag() def delete_tag(self): self._last_label_id += 1 if self._last_label_id >=len(self._label_ring ): self._last_label_id = 0 tag = self._label_ring[self._last_label_id] self.bb.delete(tag) def _configure(self,**args): if "text" in args: if self.text != args["text"]: self.text = args["text"] self._label(self.text) if "bg" in args: #print(dir(self.bb)) self.bb.configure(bg=args["bg"]) self.defaultBackground=args["bg"] if "fg" in args: #print(dir(self.bb)) self.fg=args["fg"] #if len(self.label): # self.label[0].configure(color="red") #args["fg"]) #self.defaultBackground=args["fg"] if "fx" in args: #print(dir(self.bb)) self.fx=args["fx"] def configure(self,**args): self._configure(**args) def config(self,**args): self._configure(**args) def bind(self,etype="