mytklib.py 9.5 KB

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