mytklib.py 10.0 KB

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