mytklib.py 10 KB

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