mytklib.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/python3
  2. import tkinter as tk
  3. from tkinter import font
  4. import time
  5. #import _thread as thread
  6. import os
  7. from pathlib import Path
  8. home_dir = str(Path.home())
  9. def tk_btn_bg_loop(btn,c1="#00ffdd",c2="#00ffff",stime=time.time()):
  10. while time.time() < stime+10:
  11. time.sleep(0.1)
  12. #time.sleep(10)
  13. c1b = c1
  14. c2b = c2
  15. print("tk_btn_loop",btn,c1,c2,"sleep 20")
  16. flip = 0
  17. change = 0
  18. t_last = time.time()
  19. try:
  20. while 1:
  21. #t = int(time.time()*1000)
  22. #if t % 500 == 0:
  23. # change = 1
  24. # time.sleep(.001)
  25. #else:
  26. # time.sleep(.001)
  27. # continue
  28. change = 1
  29. time.sleep(0.5)
  30. if change:
  31. if os.path.isfile(home_dir+"/LibreLight/blink"):
  32. c1 = "#0f0"
  33. c2 = "#00f"
  34. else:
  35. c1 = c1b #"#0f0"
  36. c2 = c2b #"#00f"
  37. #print(btn,"change",str(t)[:-3],btn["text"])
  38. if flip:flip = 0
  39. else:flip = 1
  40. if flip:c = c1
  41. else:c = c2
  42. btn["bg"] = c
  43. except Exception as e:
  44. print(__file__,"loop() exception")
  45. print(e)
  46. time.sleep(3)
  47. #usage
  48. #thread.start_new_thread(tk_btn_bg_loop,(b,))
  49. class MiniButton:
  50. def __init__(self,root,width=72,height=38,text="button"):
  51. self.text=text
  52. self.rb = tk.Frame(root, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
  53. self.bb = tk.Canvas(self.rb, highlightbackground = "black", highlightthickness = 1, bd=1,relief=tk.RAISED)
  54. self.bb.configure(width=width, height=height)
  55. self.fg = "#002"
  56. self.fx = 0
  57. self.label = []
  58. self.bind("<Enter>", self.on_enter)
  59. self.bind("<Leave>", self.on_leave)
  60. # !! BLOCK's other bindings like GO
  61. #self.bind("<Button-1>", self.on_b1)
  62. #self.bind("<ButtonPress>", self.on_press)
  63. #self.bind("<ButtonRelease>", self.on_release)
  64. #self.bind("<ButtonRelease-1>", self.on_release)
  65. self._last_label_id = 0
  66. self._label_ring = ["labelA","labelB"]
  67. self.activebackground="lightgrey"
  68. self.defaultBackground="grey"
  69. def on_b1(self, e):
  70. print("on_b1",e)
  71. #self.bb.config(background=self.activebackground)
  72. self.bb.config(relief=tk.SUNKEN)#abackground=self.activebackground)
  73. return 1
  74. def on_press(self, e):
  75. print("on_press",e)
  76. #self.bb.config(background=self.activebackground)
  77. self.bb.config(relief=tk.SUNKEN)#abackground=self.activebackground)
  78. return 1
  79. def on_release(self, e):
  80. print("on_release",e)
  81. #self.bb.config(background=self.activebackground)
  82. self.bb.config(relief=tk.RAISED)#abackground=self.activebackground)
  83. return 1
  84. def on_enter(self, e):
  85. #print("on_enter",e)
  86. #self.bb.config(background=self.activebackground)
  87. self.bb.config(relief=tk.FLAT)#abackground=self.activebackground)
  88. return 1
  89. def on_leave(self, e):
  90. #print("on_leave",e)
  91. self.bb.config(background=self.defaultBackground)
  92. self.bb.config(relief=tk.RAISED)#abackground=self.activebackground)
  93. return 1
  94. def _label(self,text="1\n2\n3\n"):
  95. z = 0
  96. tag = self._label_ring[self._last_label_id]
  97. #self.bb.delete("label")
  98. self.label = []
  99. for t in text.split("\n"):
  100. self.l = self.bb.create_text(37,z*10+9,text=t,anchor="c",tag=tag)
  101. #self.l["color"] = self.fg
  102. self.label.append(self.l)
  103. z+=1
  104. self.delete_tag()
  105. def delete_tag(self):
  106. self._last_label_id += 1
  107. if self._last_label_id >=len(self._label_ring ):
  108. self._last_label_id = 0
  109. tag = self._label_ring[self._last_label_id]
  110. self.bb.delete(tag)
  111. def _configure(self,**args):
  112. if "text" in args:
  113. if self.text != args["text"]:
  114. self.text = args["text"]
  115. self._label(self.text)
  116. if "bg" in args:
  117. #print(dir(self.bb))
  118. self.bb.configure(bg=args["bg"])
  119. self.defaultBackground=args["bg"]
  120. if "fg" in args:
  121. #print(dir(self.bb))
  122. self.fg=args["fg"]
  123. #if len(self.label):
  124. # self.label[0].configure(color="red") #args["fg"])
  125. #self.defaultBackground=args["fg"]
  126. if "fx" in args:
  127. #print(dir(self.bb))
  128. self.fx=args["fx"]
  129. def configure(self,**args):
  130. self._configure(**args)
  131. def config(self,**args):
  132. self._configure(**args)
  133. def bind(self,etype="<Button>",cb=None):
  134. #bb.bind("<ButtonRelease>",MAIN.tk_event(fix=0,elem=b,attr=k,data=self,mode="EXEC").cb)
  135. if cb:
  136. self.bb.bind(etype,cb)
  137. def grid(self,row=0, column=0, sticky=""):
  138. self.bb.pack() #(row=row, column=column, sticky=sticky)
  139. self.rb.grid(row=row, column=column, sticky=sticky)
  140. class ExecButton(MiniButton):
  141. def __init__(self,root,width=72,height=38,text="button"):
  142. #self.text = "1\n2\n3\n"
  143. super().__init__(root,width,height,text)
  144. self.x9font = tk.font.Font(family="FreeSans", size=9, weight="bold")
  145. self.x8font = tk.font.Font(family="FreeSans", size=8, weight="bold")
  146. self.x7font = tk.font.Font(family="FreeSans", size=7, weight="bold")
  147. self.x6font = tk.font.Font(family="FreeSans", size=6, weight="bold")
  148. self.x5font = tk.font.Font(family="FreeSans", size=5, weight="bold")
  149. #print(self,"init()",[self.text])
  150. def config(self,**args):
  151. self._configure(**args)
  152. self._label()
  153. def configure(self,**args):
  154. self._configure(**args)
  155. self._label()
  156. def dbg_info(self):
  157. print(self,"_label()",[self.text])
  158. for i in dir(self.bb):
  159. print("-",i)
  160. def _label(self,text=None):
  161. if type(text) is str:
  162. if self.text == text:
  163. return
  164. self.text = text
  165. else:
  166. text = self.text[:]
  167. tag = self._label_ring[self._last_label_id]
  168. #self.bb.delete("labelB")
  169. #self.bb.delete("labelA")
  170. txt2 = text
  171. try:
  172. text = text.split("\n")[1]
  173. except:pass
  174. if "grün" in text.lower() or "green" in text.lower():
  175. self.l = self.bb.create_rectangle(10,29,20,39,fill="green",tag=tag)
  176. elif "purple" in text.lower() or "purple" in text.lower():
  177. self.l = self.bb.create_rectangle(10,29,20,39,fill="#800080",tag=tag)
  178. elif "lime" in text.lower() or "lime" in text.lower():
  179. self.l = self.bb.create_rectangle(10,29,20,39,fill="#00ff00",tag=tag)
  180. elif "blau" in text.lower() or "blue" in text.lower():
  181. self.l = self.bb.create_rectangle(10,29,20,39,fill="blue",tag=tag)
  182. elif "rot" in text.lower() or "red" in text.lower():
  183. self.l = self.bb.create_rectangle(10,29,20,39,fill="red",tag=tag)
  184. elif "orange" in text.lower():# or "yellow" in text.lower():
  185. self.l = self.bb.create_rectangle(10,29,20,39,fill="orange",tag=tag)
  186. elif "weiß" in text.lower() or "white" in text.lower():
  187. self.l = self.bb.create_rectangle(10,29,20,39,fill="white",tag=tag)
  188. elif "cyan" in text.lower():# or "yellow" in text.lower():
  189. self.l = self.bb.create_rectangle(10,29,20,39,fill="cyan",tag=tag)
  190. elif "gelb" in text.lower() or "yellow" in text.lower():
  191. self.l = self.bb.create_rectangle(10,29,20,39,fill="yellow",tag=tag)
  192. elif "pink" in text.lower() or "pink" in text.lower():
  193. self.l = self.bb.create_rectangle(10,29,20,39,fill="#ff69b4",tag=tag)
  194. elif "mage" in text.lower() or "mage" in text.lower():
  195. self.l = self.bb.create_rectangle(10,29,20,39,fill="magenta",tag=tag)
  196. if "nebel" in text.lower() or "smoke" in text.lower() or "haze" in text.lower():
  197. self.l = self.bb.create_rectangle(10,29,60,39,fill="white",tag=tag)
  198. if "mh " in text.lower() or " mh" in text.lower() :
  199. self.l = self.bb.create_rectangle(30,29,35,32,fill="black",tag=tag)
  200. self.l = self.bb.create_rectangle(28,34,37,39,fill="black",tag=tag)
  201. if "off" in text.lower():
  202. self.l = self.bb.create_rectangle(50,30,55,35,fill="black",tag=tag)
  203. if "dim" in text.lower() or "front" in text.lower() or "on" in text.lower():
  204. #self.l = self.bb.create_line(56,30,60,28,fill="black",tag=tag)
  205. self.l = self.bb.create_rectangle(50,30,55,35,fill="white",tag=tag)
  206. #self.l = self.bb.create_line(56,36,58,36,fill="black",tag=tag)
  207. if "circle" in text.lower():
  208. self.l = self.bb.create_oval(30,29,40,39,fill="",tag=tag)
  209. if "pan" in text.lower():
  210. self.l = self.bb.create_line(20,34 ,45,34,fill="black",arrow=tk.BOTH,tag=tag)
  211. if "tilt" in text.lower():
  212. self.l = self.bb.create_line(30,25 ,30,43,fill="black",arrow=tk.BOTH,tag=tag)
  213. if self.fx:
  214. #self.l = self.bb.create_rectangle(1,34,12,48,fill="cyan",outline="",tag=tag) # bad contrast !
  215. self.l = self.bb.create_text(2,33,text="FX"+str(self.fx),anchor="nw",tag=tag,fill="black",font=self.x5font)
  216. text = txt2
  217. z = 0
  218. for t in text.split("\n"):
  219. ts = 10
  220. _max = 7
  221. if z==1 and len(t) >= _max:
  222. ts = int(10 - (len(t)-_max)/1.5)
  223. if ts < 5:
  224. ts = 5
  225. xfont = self.x9font
  226. if 1:
  227. if ts == 9:
  228. xfont = self.x9font
  229. elif ts == 8:
  230. xfont = self.x8font
  231. elif ts == 7:
  232. xfont = self.x7font
  233. elif ts == 6:
  234. xfont = self.x7font
  235. elif ts == 5:
  236. xfont = self.x7font
  237. if len(t) > 14:
  238. t2 = t[:14]
  239. t3 = t[14:]
  240. self.l = self.bb.create_text(37,z*10+9-2,text=t2,anchor="c",tag=tag,fill=self.fg,font=xfont)
  241. self.l = self.bb.create_text(37,z*10+9+6,text=t3,anchor="c",tag=tag,fill=self.fg,font=xfont)
  242. else:
  243. self.l = self.bb.create_text(37,z*10+9,text=t,anchor="c",tag=tag,fill=self.fg,font=xfont)
  244. #self.l = self.bb.create_text(37,z*10+9,text=t,anchor="c",tag=tag,fill=self.fg)
  245. else:
  246. self.l = self.bb.create_text(37,z*10+9,text=t,anchor="c",tag=tag,fill=self.fg)
  247. z+=1
  248. #self.bb.update(0)
  249. #self.bb.after(0)
  250. self.delete_tag()