mytklib.py 11 KB

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