Advertisement
miklis

mscanvasing

Aug 24th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.92 KB | None | 0 0
  1. '''Once we've defined what MsCanvas is, we need to identify in mathematical way,
  2. where will startpoint and endpoint of segment between two mscanvases be located.
  3. Let's write a function named find_connect_bb to find these locations'''
  4. #hexcode:
  5. #print '#'+''.join([['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'][k] for k in [18/16, 18%16, 33/16, 33%16, 8/16, 8%16]])
  6. from Tkinter import*
  7. from subprocess import call
  8. from random import randint
  9. from PIL import Image, ImageTk, ImageColor
  10. from types import*
  11. from time import*
  12. from re import finditer
  13. root = Tk()
  14. jo=10
  15. array_of_positions=[(randint(0, 200-1), randint(0, 200-1)) for i in range(jo)]
  16. # this file demonstrates the movement of a single canvas item under mouse control
  17.  
  18. class SCanvas(Canvas, object):
  19. '''
  20. Implementation of de button equivalent via Canvas
  21. It can create text, image and rectangle and use options of any of them.
  22. Options: the same as create_text, create_rectangle except {fill} + {command(buggy) + bg, fg, image}
  23. Author: Night Simon
  24. Date: 07.16.2017
  25. '''
  26. numOfButtons = 0
  27. rectangle_fill, text_fill = '', 'black'
  28. image=''
  29.  
  30. def __init__(self, root, *args, **kwargs):
  31. Canvas.__init__(self, root, *args, **kwargs)
  32.  
  33. def click(self,event,Id,config):
  34. '''only ids but not tags!old version of itemconfig'''
  35. super(SCanvas,self).itemconfig(Id, config)
  36.  
  37. def find_withbuttontag(self,tag):
  38. #X in button family or X='buttonX' |-> 3 numbers that represents button family
  39. #print 'find_withbuttontag of tag =',tag,'which has',type(tag)
  40. if type(tag) is str and 'button' in tag:
  41. return self.find_withtag(tag) #case1: input is buttonX
  42. else:
  43. button_tags=self.gettags(tag) #case2: input is a number
  44. if 'button' in button_tags: return self.find_withtag(button_tags[0])#case2_1: input is a number of buttonFamily
  45. else: raise TclError('tag = '+str(tag)+' is not a button') #case2_2: input is not a number of buttonFamily
  46.  
  47. def type(self,tag):
  48. #if X is in buttonFamily or X=='buttonX', return 'button'
  49. #print 'type of tag =', tag, 'which has',type(tag)
  50. if type(tag) is str: #case1: input is buttonX or edgeX
  51. if 'button' in tag: return 'button'
  52. elif 'edge' in tag: return 'edge'
  53. else: #case2: input is a number
  54. tags=self.gettags(tag)
  55. if 'button' in tags: return 'button' #case2_1: input is a number of buttonFamily or edge
  56. elif 'edge' in tags: return 'edge'
  57. else: return super(SCanvas,self).type(tag)
  58. #raise TypeError('scanvas.type of tag =',tag,' is unidentified') #case2_2: input is not a number of buttonFamily
  59.  
  60. def mixcolor(self, string_obj):
  61. """Internal function that converts '.tex-color' to hex-color"""
  62. print 'args of self.mixcolor:', string_obj
  63. color_array=string_obj.split('!')
  64. if len(color_array) == 1:
  65. c=ImageColor.getrgb(color_array[0])
  66. print 'result of self.mix_color(',string_obj,'):', '#%02x%02x%02x' % tuple([int(c[i]) for i in [0,1,2]])
  67. return '#%02x%02x%02x' % tuple([int(c[i]) for i in [0,1,2]])
  68. elif len(color_array) == 2: color_array.append('white')
  69. c1=ImageColor.getrgb(color_array[0])
  70. c2=ImageColor.getrgb(color_array[2])
  71. print c1
  72. print c2
  73. m1=int(color_array[1])/100.
  74. m2=1-m1
  75. print tuple([int(m1*c1[i]+m2*c2[i]) for i in [0,1,2]])
  76. print 'result of self.mix_color(',string_obj,'):','#%02x%02x%02x' % tuple([int(m1*c1[i]+m2*c2[i]) for i in [0,1,2]])
  77. return '#%02x%02x%02x' % tuple([int(m1*c1[i]+m2*c2[i]) for i in [0,1,2]])
  78.  
  79. def replace_color(self, img, color):
  80. """Internal function that converts black&white PIL.Image to PIL.Image with white pixels replaced with given color;
  81. color type is stringType (.tex-color, hex-color or named-color)"""
  82. image=img.copy()
  83. pixels=image.load()
  84. #pixals=pixels[for x in range(img.size[0])
  85. c=self.getrgb(color)
  86. for x in range(image.size[0]):
  87. for y in range(image.size[1]):
  88. px = (pixels[x,y][0]+pixels[x,y][1]+pixels[x,y][2])/3
  89. pixels[x,y] = (int(px*c[0]/255.), int(px*c[1]/255.), int(px*c[2]/255.))
  90. img.save('watchout.png')
  91. return image
  92.  
  93. def getrgb(self, string_obj):
  94. """Internal function that converts hex-color to rgb-tuple"""
  95. print 'args of self.getrgb:', string_obj
  96. It=[['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'].index(n) for n in string_obj[1:]]
  97. print 'result of self.getrgb(',string_obj,'):',(It[0]*16+It[1], It[2]*16+It[3], It[4]*16+It[5])
  98. return (It[0]*16+It[1], It[2]*16+It[3], It[4]*16+It[5])
  99.  
  100. def itemconfig(self, tag, **kwargs):
  101. #overriding itemconfig to accept tags such as 'buttonX', X in button family or 'edgeX'
  102. #text ['justify', 'text', 'font', 'underline', 'anchor']
  103. #rectangle ['disabledoutlinestipple', 'dash', 'disabledwidth', 'activeoutlinestipple', 'dashoffset', 'activewidth', 'disabledoutline', 'disableddash', 'outlinestipple', 'activedash', 'activeoutline', 'outlineoffset', 'outline']
  104. #image ['image', 'disabledimage', 'anchor', 'activeimage']
  105. if self.type(tag)=='button':
  106. tags_of_button=self.find_withbuttontag(tag)
  107. if kwargs:
  108. for n in kwargs.keys():
  109. #----------------------------------------------------------------------------------------------------------------------
  110. if n=='fill': raise TclError('fill can be whether text or rectangle fill; use bg for background and fg for textcolor')
  111. elif n=='bg': #allowed in mscanvas class only; used in mscanvas.create_button only
  112. if super(SCanvas,self).type(tags_of_button[1])=='image':
  113. buttonX=self.gettags(tags_of_button[1])[0]
  114. super(SCanvas,self).itemconfig(tags_of_button[0], fill=self.register[buttonX].color)
  115. for it in tags_of_button:
  116. self.tag_bind(it,"<Enter>", lambda event, tag=tags_of_button[1], config={'image':self.register[buttonX].photo_active}: self.click(event,tag,config))
  117. self.tag_bind(it,"<Leave>", lambda event, tag=tags_of_button[1], config={'image':self.register[buttonX].photo}: self.click(event,tag,config))
  118. #super(SCanvas,self).itemconfig(tags_of_button[0], fill=self.mixcolor(kwargs['bg']+'!60'))
  119. #self.tag_bind(it,"<Enter>", lambda event, tag=tags_of_button[0], config={'fill':self.mixcolor(kwargs['bg'])}: self.click(event,tag,config))
  120. else:
  121. print 'other case'
  122. elif n=='fg':
  123. super(SCanvas,self).itemconfig(tags_of_button[1], fill=self.mixcolor(kwargs['fg']))
  124. #----------------------------------------------------------------------------------------------------------------------
  125. elif n=='width': raise TclError('width can be whether text or rectangle fill; use textwidth or borderwidth')
  126. elif n=='textwidth': super(SCanvas,self).itemconfig(tags_of_button[1], width=kwargs['textwidth'])
  127. elif n=='borderwidth': super(SCanvas,self).itemconfig(tags_of_button[0], width=kwargs['borderwidth'])
  128. #----------------------------------------------------------------------------------------------------------------------
  129. elif n=='image':
  130. print '------------------'
  131. for x in tags_of_button:
  132. print super(SCanvas,self).type(x)
  133. print '------------------'
  134. if super(SCanvas,self).type(tags_of_button[1]) <> 'image':
  135. print 'fixing the subtype:', super(SCanvas,self).type(tags_of_button[1])
  136. print 'getting coords:', self.coords(tags_of_button[1])
  137. coords_of_image=self.coords(tags_of_button[1])
  138. self.delete(tags_of_button[1])
  139. print 'deleting subtype'
  140. tags_of_button[0] = self.create_image(*coords_of_image, image=kwargs['image'])
  141. print 'placed a new image at', coords_of_image
  142. self.coords(tags_of_button[0], self.bbox(tags_of_button[0]))
  143. print 'creating a bounding box at', self.bbox(tags_of_button[0])
  144. else:
  145. super(SCanvas,self).itemconfig(tags_of_button[1],{'image':kwargs['image']})
  146. self.coords(tags_of_button[0], self.bbox(tags_of_button[1]))
  147. print 'rebox', self.bbox(tags_of_button[1])
  148. elif n in super(SCanvas,self).itemconfig(tags_of_button[0]).keys(): super(SCanvas,self).itemconfig(tags_of_button[0],{n:kwargs[n]})
  149. elif n in super(SCanvas,self).itemconfig(tags_of_button[1]).keys(): super(SCanvas,self).itemconfig(tags_of_button[1],{n:kwargs[n]})
  150. #elif n in super(SCanvas,self).itemconfig(tags_of_button[2]).keys(): super(SCanvas,self).itemconfig(tags_of_button[2],{n:kwargs[n]})
  151. elif n in ['disabledstipple', 'stipple', 'active_stiple', 'activefill', 'disabledfill', 'offset', 'width', 'tags', 'state']:
  152. raise TclError(n+' is ill-defined argument')
  153. else: raise TclError('unrecognised argument', n, 'in itemconfig')
  154. else:
  155. cnf=super(SCanvas,self).itemconfig(tags_of_button[0])
  156. cnf.update(super(SCanvas,self).itemconfig(tags_of_button[1]))
  157. cnf.update(super(SCanvas,self).itemconfig(tags_of_button[2]))
  158. return cnf
  159.  
  160. else: return super(SCanvas,self).itemconfig(tag, **kwargs)
  161.  
  162. '''def bbox(self, tag):
  163. """Internal"""
  164. bb=super(SCanvas,self).bbox(tag)
  165. print bb
  166. return (bb[0]-2, bb[1]-2, bb[2]+2, bb[3]+2)'''
  167.  
  168. def create_button(self,*args,**kwargs):
  169. self.numOfButtons+=1
  170. sticker="button"+str(self.numOfButtons)
  171. #['disabledstipple', 'stipple', 'active_stiple', 'activefill', 'disabledfill', 'offset'] are both ill-defined arguments of create_text and create_rectangle
  172. #'tags', 'state' is an argument for all of them
  173. subtype=''
  174. for n in kwargs.keys():
  175. if n in ['justify', 'text', 'font', 'underline', 'anchor']:
  176. if subtype=='':
  177. tag1=self.create_text(args)
  178. subtype='text'
  179. super(SCanvas,self).itemconfig(tag1,{n:kwargs[n]})
  180. kwargs.pop(n)
  181. if n in ['image', 'disabledimage', 'anchor', 'activeimage']:
  182. if subtype=='text': raise TclError('can''t identify subtype: there are arguments of both subtypes')
  183. else:
  184. if subtype=='':
  185. tag1=self.create_image(args)
  186. subtype='image'
  187. #if n=='image': bu=ImageTk.PhotoImage(kwargs[n])
  188. super(SCanvas,self).itemconfig(tag1,{n:kwargs[n]})
  189. kwargs.pop(n)
  190. if subtype=='': raise TclError('no subtype identified')
  191. tag2=self.create_rectangle(self.bbox(tag1))
  192. print tag2, 'is created at', self.bbox(tag1), 'of type', subtype
  193. self.tag_lower(tag2, tag1)
  194. #adding tag 'buttonX' to all of the 3 items
  195. for it in [tag1, tag2]:
  196. super(SCanvas, self).itemconfig(it, {'tags':sticker})
  197. self.addtag_withtag("button", sticker)
  198. if kwargs<>{}: self.itemconfig('button'+str(self.numOfButtons), **kwargs)
  199. #if 'bg' not in kwargs: kwargs.update({'bg':self.rectangle_fill})
  200. return sticker
  201.  
  202. class MsCanvas(SCanvas, object):
  203. '''
  204. Taken from google search of "canvas-moving-w-mouse.py"
  205. Extension of de scanvas with ability to move
  206. It can create text, image and rectangle and use options of any of them.
  207. Options of scanvas: the same as create_text, create_rectangle except {fill} + {command(buggy), bg, fg, image}
  208. Additional options: motion
  209. Author: Night Simon
  210. Date: 07.19.2017
  211. '''
  212. register={} #a dictionary of all clickables whose values are corresponding objects of button or edge class
  213. clickables={}
  214. numOfEdges=0
  215. button_color='orange' #please use pythonistic color format
  216. edge_color='green'
  217. select_color='blue'
  218.  
  219. def print_clickables(self, event, tag): print 'CLICKABLES:', mscanvas.clickables
  220. def print_register(self, event, tag): print 'REGISTER:', mscanvas.register
  221.  
  222. def button_menu_delete(self):
  223. print 'button_menu_delete'
  224. self.erase_button(True, self.find_withtag('popup')[0])
  225. self.dtag('popup')
  226.  
  227. def edge_menu_delete(self):
  228. print 'edge_menu_delete', self.find_withtag('popup')[0]
  229. self.erase_edge(True, self.find_withtag('popup')[0])
  230. self.dtag('popup')
  231.  
  232. def button_menu_color(self):
  233. buttonX=self.gettags(self.find_withtag('popup')[0])[0]
  234. self.register[buttonX].photo_active = ImageTk.PhotoImage(self.replace_color(self.register[buttonX].image_snap, self.mixcolor(self.button_color)))
  235. self.register[buttonX].photo = ImageTk.PhotoImage(self.replace_color(self.register[buttonX].image_snap, self.mixcolor(self.button_color+'!60')))
  236. self.itemconfig(buttonX, bg='its important to stay awake', outline=self.button_color, image=self.register[buttonX].photo)
  237. self.dtag('popup')
  238.  
  239. def edge_menu_color(self):
  240. edgeX=self.gettags(self.find_withtag('popup')[0])[0]
  241. self.itemconfig(edgeX, fill=self.button_color)
  242. self.dtag('popup')
  243.  
  244. def button_menu_open(self):
  245. buttonX=self.gettags(self.find_withtag('popup')[0])[0]
  246. #if 'link' in self.register[buttonX].__dict__.keys(): print 'assume I opened', self.register[buttonX].link
  247. print 'opening',self.register[buttonX].link,':'
  248. not(call(['C:\Users\Vartotojas\Desktop\mscanvas\\'+self.register[buttonX].link], shell=True))
  249. self.dtag('popup')
  250.  
  251. def button_menu_mathinput(self):
  252. formula=self.mathinput.get("1.0",'end-1c').encode('utf-8')
  253. self.temporary_formula=formula
  254. link=''
  255. self.templink=False
  256. #capturing commands used and formula
  257. normalsize, defaultstyle = False, False
  258. splitted=[m.start() for m in finditer('--', formula)]
  259. if len(splitted)<>0:
  260. commands=[formula[n+2:n+formula[n:].index(' ')] for n in splitted]
  261. formula=formula[splitted[-1]+formula[splitted[-1]:].index(' ')+1:]
  262. for x in commands:
  263. if '.' in x: #assuming it is linked content
  264. link=x
  265. self.templink=True
  266. if x=='ns': normalsize=True
  267. if x=='d': defaultstyle=True
  268.  
  269. if not(defaultstyle): formula=r"$\text{"+formula+"}$"
  270. if not(normalsize): formula=r"\Huge "+formula
  271.  
  272. if len(splitted)<>0:
  273. print 'external commands:', commands
  274. print 'external commands:', formula
  275. #formula=r"$\colorbox{"+"green"+r"}{\text{$"+formula+r"$}}$"
  276.  
  277. #formula=r"$"+formula+r"$"
  278. #formula=r"$\colorbox{"+color+r"}{\text{"+formula+r"}}$"#case when it text mode
  279. TEX = ( # use raw strings for backslash
  280. unicode("\documentclass{article}"),
  281. unicode("\usepackage[utf8]{inputenc}"),
  282. unicode("\usepackage[L7x]{fontenc}"),
  283. unicode("\usepackage[lithuanian]{babel}"),
  284. unicode("\usepackage{lmodern}"),
  285. unicode("\usepackage{amsmath}"),
  286. unicode("\usepackage{amsthm}"),
  287. unicode("\usepackage{amssymb}"),
  288. unicode("\usepackage{bm}"),
  289. unicode("\usepackage{xcolor}"),
  290. unicode("\usepackage{tikz}"),
  291. unicode("\usepackage[active,displaymath,textmath,tightpage]{preview}"),
  292. unicode("\pagestyle{empty}"),
  293. r"\begin{document}",
  294. #'$'+formula+'$',
  295. #$\colorbox{green!50!yellow}{\text{$\sqrt{841}$}}$
  296. formula,
  297. r"\end{document}")
  298. with open(r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.tex",'w') as out_snap:
  299. for T in TEX: out_snap.write("%s\n" % T)
  300. #print 'AFTER writing a file:', time()-t
  301. print 'temp file compiled succesfully:', not(call(['latex','--interaction', 'nonstopmode', r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.tex"]))
  302. print '.dvi converted to .png succesfully:', not(call(['dvipng','--interaction', 'nonstopmode', 'temptex.dvi','-o',r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png"]))
  303. #latex --interaction nonstopmode C:\Users\Vartotojas\Desktop\mintedex.tex
  304. #dvipng --interaction nonstopmode mintedex.dvi -o C:\Users\Vartotojas\Desktop\mintedex.png
  305. popupids=self.find_withtag('popup')
  306. if popupids:
  307. buttonX=self.gettags(popupids[0])[0]
  308. img=Image.open(r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png")
  309. self.temporary_photo=ImageTk.PhotoImage(img)
  310. self.register[buttonX].formula=formula
  311. self.update_formula(buttonX)
  312. self.itemconfig(buttonX, bg='its important to stay awake')
  313. self.dtag('popup')
  314. else:
  315. x,y=randint(400, 700-1), randint(100, 400-1)
  316. img=Image.open(r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png")
  317. self.temporary_photo=ImageTk.PhotoImage(img)
  318. buttonX=self.create_button(x,y, borderwidth=5, outline=self.button_color, image = self.temporary_photo)
  319.  
  320. if link: self.register[buttonX].link=link #adding linked content
  321. elif 'link' in self.register[buttonX].__dict__.keys():
  322. del self.register[buttonX].link
  323. print 'to delete'
  324. self.templink=False
  325. self.itemconfig(buttonX, image = self.register[buttonX].photo)
  326. print 'temp file removed succesfully:', not(call(['del','--interaction', 'nonstopmode', r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png"], shell=True))
  327.  
  328. #else:
  329. #image=Image.open(r"C:\Users\Vartotojas\Desktop\mintedex.png")
  330. #pixels=image.load()
  331. #print pixels
  332. #photo=ImageTk.PhotoImage(image)
  333. #self.register[buttonX].image=ImageTk.PhotoImage(file="C:\Users\Vartotojas\Desktop\mintedex.png")
  334. #x,y=randint(250, 1200-1), randint(50, 530-1)
  335. #buttonX=self.create_button(x,y, borderwidth=5, outline=self.button_color, image=ImageTk.PhotoImage(file="C:\Users\Vartotojas\Desktop\mintedex.png"))
  336. #self.register[buttonX].image=image
  337. #self.register[buttonX].image_replace=self.replace_color(image)
  338. #self.register[buttonX].formula=formula
  339.  
  340. #self.itemconfig(buttonX, image=photo)
  341.  
  342. def main_menu_save(self): pass
  343. def main_menu_painton(self): pass
  344. def main_menu_paintoff(self): pass
  345.  
  346. def __init__(self, root, **kwargs):
  347. SCanvas.__init__(self, root, kwargs)
  348.  
  349. #self.bind("<Button-1>", lambda event: self.info(event))
  350. #self.bind("<Double-Button-1>", lambda event: self.info_detailed(event))
  351. #self.bind("<B1-ButtonRelease>", lambda event: self.record(event)) #self.mouseDown is inside!
  352. self.bind("<Button-3>", lambda event: self.popup_main(event))
  353.  
  354.  
  355. self.main_menu=Menu(self, tearoff=0)
  356. self.main_menu.add_command(label='save scripts', command = self.main_menu_save)
  357. self.main_menu.add_command(label='begin paint', command = self.main_menu_painton)
  358. self.main_menu.add_command(label='end paint', command = self.main_menu_paintoff)
  359.  
  360. #rightclicks of buttons
  361. self.button_menu=Menu(self, tearoff=0)
  362. self.button_menu.add_command(label='delete button', command = self.button_menu_delete)
  363. self.button_menu.add_command(label='change color', command = self.button_menu_color)
  364. self.button_menu.add_command(label='edit formula', command = self.button_menu_mathinput)
  365. '''
  366. recttag=self.create_rectangle(0, 0, 40, 40, fill='yellow')
  367. self.tag_bind(recttag,"<Button-1>", lambda event, i=recttag: CLICK2(event, i))
  368.  
  369. clickables=self.create_text(70, 20, text='clickables')
  370. box=self.create_rectangle(self.bbox(clickables), fill="orange", tags='info')
  371. self.tag_lower(box, clickables)
  372. self.tag_bind(clickables,"<Button-1>", lambda event, i = clickables: self.print_clickables(event, i))
  373.  
  374. register=self.create_text(132, 20, text='register')
  375. box=self.create_rectangle(self.bbox(register), fill="orange", tags='info')
  376. self.tag_lower(box, register)
  377. self.tag_bind(register,"<Button-1>", lambda event, i=register: self.print_register(event, i))'''
  378.  
  379. for i in range(10):
  380. color=self.create_rectangle(0+20*i, 10, 0+20*(i+1), 30, fill=['blue', 'sienna', 'grey', 'magenta','maroon','red', 'orange','purple','darkgreen','firebrick'][i])
  381. self.tag_bind(color, "<Button-1>", lambda event, i=color: self.set_color(event, i))
  382. self.itemconfig(color, outline='white')
  383.  
  384. self.edge_menu=Menu(self, tearoff=0)
  385. self.edge_menu.add_command(label='delete edge', command = self.edge_menu_delete)
  386. self.edge_menu.add_command(label='change color', command = self.edge_menu_color)
  387. #leftpanel
  388. Button(root, text='formula', bg='#00ff00', command=lambda: self.button_menu_mathinput()).place(x=0, y=0, width=250, height=20)
  389. self.mathinput=Text(root)
  390. self.mathinput.place(x=0, y=20, width=250, height=200)
  391.  
  392. def set_color(self, event, tag):
  393. self.button_color = self.itemconfig(tag)["fill"][-1]
  394. self.edge_color = self.button_color
  395. self.itemconfig(tag, outline=self.select_color, width=3)
  396. self.tag_raise(tag)
  397. for n in self.find_withtag('activecolor'): self.itemconfig(n, outline='white', width=1)
  398. self.dtag('activecolor')
  399. self.addtag_withtag('activecolor', tag)
  400.  
  401. def update_formula(self, buttonX):
  402. if self.templink:
  403. im1=Image.open(r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png")
  404. im2=Image.open(r"docu.png")
  405. x=int(im2.size[0]*im1.size[1]/(1.*im2.size[1]))
  406. pic = Image.new("RGBA", (im1.size[0]+x, im1.size[1]))
  407. pic.paste(im1, (0, 0))
  408. pic.paste(im2.resize((x,im1.size[1])), (im1.size[0], 0))
  409. self.register[buttonX].image_snap=pic
  410. else: self.register[buttonX].image_snap=Image.open(r"C:\Users\Vartotojas\Desktop\mscanvas\temptex.png")
  411. self.register[buttonX].formula=self.temporary_formula
  412. #its is opened in button_menu_mathinput but we need reassign because we dont have button tag yet
  413. #self.replace_color(self.register[buttonX].image_snap, self.mixcolor(self.register[buttonX].color))
  414. self.register[buttonX].photo_active = ImageTk.PhotoImage(self.replace_color(self.register[buttonX].image_snap, self.mixcolor(self.register[buttonX].color)))
  415. self.register[buttonX].photo = ImageTk.PhotoImage(self.replace_color(self.register[buttonX].image_snap, self.mixcolor(self.register[buttonX].color+'!60')))
  416.  
  417. def popup(self, event, tag):
  418. self.addtag_withtag('popup', tag)
  419. print self.type(tag)
  420. if self.type(tag)=='button':
  421. buttonX=self.gettags(tag)[0]
  422. if 'link' in self.register[buttonX].__dict__.keys():
  423. try: self.button_menu.delete('open...')
  424. except TclError: pass
  425. self.button_menu.add_command(label='open...', command = self.button_menu_open)
  426. else:
  427. try: self.button_menu.delete('open...')
  428. except TclError: pass
  429. self.button_menu.tk_popup(event.x_root, event.y_root, 0)
  430. elif self.type(tag)=='edge':
  431. edgeX=self.gettags(tag)[0]
  432. self.edge_menu.tk_popup(event.x_root, event.y_root, 0)
  433.  
  434. def popup_main(self, event):
  435. self.main_menu.tk_popup(event.x_root, event.y_root, 0)
  436.  
  437. def record(self, event):
  438. if self.find_withtag(CURRENT)==(): #make sure the rightclick
  439. print 'dont know how to save you'
  440. for n in self.find_withtag('displayed'): self.delete(n)
  441.  
  442. def _boxify(self, tag, text):
  443. """Internal function that shows text over the tag"""
  444. C=self.coords(tag)
  445. temp=self.create_text((C[0]+C[2])/2, (C[1]+C[3])/2, text=text, tags='displayed')
  446. tempbox=self.create_rectangle(self.bbox(temp), fill="yellow", tags='displayed')
  447. self.tag_lower(tempbox, temp)
  448.  
  449. def info(self, event):
  450. if self.find_withtag(CURRENT)==(): #make sure no edgebutton is rightclicked
  451. for n in self.register.keys():
  452. txt=n
  453. if self.type(n)=='button': self._boxify(self.find_withbuttontag(n)[1],txt)
  454. elif self.type(n)=='edge': self._boxify(self.find_withtag(n),txt)
  455.  
  456. def info_detailed(self, event):
  457. if self.find_withtag(CURRENT)==():
  458. for n in self.register.keys():
  459. txt=n
  460. if n in self.clickables.keys(): txt+=';\n'+'self.clickables['+n+'] = '+str(self.clickables[n])
  461. else: txt+=';\nnot found in self.clickables'
  462. if type(self.register[n]) is StringType:
  463. txt+='\n'+'self.register['+n+'] = '+str(self.register[n])
  464. print 'IT SHOULDNT BE'
  465.  
  466. else:
  467. txt+='\n'+'self.register['+n+'] = {'
  468. for it in self.register[n].__dict__.keys():
  469. txt+='\n'+str(it)+': '+str(self.register[n].__dict__[it])
  470. txt+='}'
  471. if self.type(n)=='button': self._boxify(self.find_withbuttontag(n)[1],txt)
  472. elif self.type(n)=='edge': self._boxify(self.find_withtag(n),txt)
  473.  
  474. def _redraw(self, edgeX):
  475. """Internal function of self.redraw that uses recursion"""
  476. for m in self.clickables[edgeX]:
  477. coords=self.connect_bp(self.register[m].begin, edgeX)
  478. self.coords(self.register[m].id, coords)
  479. self._redraw(m)
  480.  
  481. def redraw(self, buttonX):
  482. """Internal function that redraws edge that relates buttonX"""
  483. for n in self.clickables[buttonX]:
  484. if self.register[n].begin==buttonX:
  485. if self.type(self.register[n].end)=='button': #edit edge between two buttons (forward direction)
  486. coords=self.connect_bb(self.register[n].begin, self.register[n].end)
  487. elif self.type(self.register[n].end)=='edge':
  488. coords=self.connect_bp(self.register[n].begin, self.register[n].end) #edit edge that ends up with another edge
  489. self.coords(self.register[n].id, coords)
  490. if self.register[n].end==buttonX: #edit edge between two buttons (backward direction)
  491. self.coords(self.register[n].id, self.connect_bb(self.register[n].begin, self.register[n].end))
  492. self._redraw(n)
  493.  
  494. def mouseDown(self, event, tag):
  495. # remember where the mouse went down
  496. self.lastx = event.x
  497. self.lasty = event.y
  498.  
  499. def mouseMove(self, event, tag):
  500. # if the mouse is over scanvas button tagged as CURRENT we move some objects:
  501. #1) motion of buttonFamily
  502. movetag=self.find_withbuttontag(tag)
  503. for n in movetag:
  504. self.move(n, event.x - self.lastx, event.y - self.lasty)
  505. #2) motion of corresponding edges
  506. buttonX=self.gettags(tag)[0]
  507. if self.type(buttonX)=='button':
  508. self.redraw(buttonX)
  509.  
  510. #3) motion of edges that are connected to midpoints of these corresponding edges:
  511. #elif self.type(edgebuttonX)=='edge':
  512. self.lastx = event.x
  513. self.lasty = event.y
  514.  
  515. def mouseSelectButton(self, event, buttonX):
  516. #OLD VERSION of mouseSelectButton(self, event, buttonX)
  517. if 'dangerous' not in self.gettags(self.find_withtag(CURRENT)):
  518. self.itemconfig(buttonX, outline=self.select_color)
  519. selected=self.gettags(self.find_withtag('selected_button')[0])[0]
  520. selection_edge=self.create_line(*self.connect_bb(selected, buttonX), arrow=LAST)
  521. self.itemconfig(selection_edge, fill=self.select_color, tags='selected_edge')
  522. #NEW VERSION of mouseSelectButton(self, event)
  523. '''print 'mscurrent:',self.find_withtag(CURRENT),
  524. buttonX=self.gettags(self.find_withtag(CURRENT))[0]
  525. print 'ms',buttonX
  526. if buttonX<>self.selection_button: #current selection differs from stored selection
  527. self.itemconfig(buttonX, outline='green')
  528. self.selection_edge=self.connect_bb(self.selection_button,buttonX)
  529. self.itemconfig(self.selection_edge, fill='green')
  530. print self.selection_edge'''
  531.  
  532. def mouseUnselectButton(self, event, buttonX):
  533. #OLD VERSION of mouseUnselectButton(self, event, buttonX)
  534. self.addtag_withtag('dangerous', CURRENT)
  535. self.itemconfig(buttonX, outline=self.register[buttonX].color)
  536. self.delete('selected_edge')
  537. #NEW VERSION of mouseUnselectButton(self, event)
  538. '''print 'mucurrent:',self.find_withtag(CURRENT),
  539. buttonX=self.gettags(self.find_withtag(CURRENT))[0]
  540. print 'mu',buttonX
  541. if buttonX<>self.selection_button:
  542. self.itemconfig(buttonX, outline='darkgreen')
  543. self.delete(self.selection_edge)
  544. self.update_idletasks()'''
  545.  
  546. def mouseSelectEdge(self, event, edgeX):
  547. #edgeX is id of edge object
  548. if 'dangerous' not in self.gettags(self.find_withtag(CURRENT)):
  549. self.itemconfig(edgeX, fill=self.select_color)
  550. selected=self.gettags(self.find_withtag('selected_button')[0])[0]
  551. selection_edge=self.create_line(*self.connect_bp(selected, edgeX), arrow=LAST)
  552. self.itemconfig(selection_edge, fill=self.select_color, tags='selected_edge')
  553.  
  554. def mouseUnselectEdge(self, event, tag):
  555. #tag is id of edge object
  556. self.addtag_withtag('dangerous', CURRENT)
  557. self.itemconfig(tag, fill=self.edge_color)
  558. self.delete('selected_edge')
  559.  
  560. def double_click(self, event, tag):
  561. #tag is any number that is catched
  562. #puts border on the button if double clicked and bind Select-Unselect construction to every button
  563. #OLD VERSION
  564. if not self.find_withtag('selected_button'):
  565. buttonZ=self.gettags(tag)[0]
  566. self.addtag_withtag('selected_button', buttonZ)
  567. for n in self.find_withtag('button'):
  568. buttonX=self.gettags(n)[0]
  569. if buttonX<>self.gettags(tag)[0]:
  570. self.tag_bind(n,"<Enter>", lambda event, i=buttonX: self.mouseSelectButton(event, i))
  571. self.tag_bind(n,"<Leave>", lambda event, i=buttonX: self.mouseUnselectButton(event, i))
  572. for n in self.find_withtag('edge'):
  573. self.tag_bind(n,"<Enter>", lambda event,i=n: self.mouseSelectEdge(event, i))
  574. self.tag_bind(n,"<Leave>", lambda event,i=n: self.mouseUnselectEdge(event, i))
  575. self.itemconfig(buttonZ, bg='its important to stay awake')
  576. self.itemconfig(tag, outline=self.select_color)
  577. #NEW VERSION
  578. '''if self.selection_button==None:
  579. self.selection_button=self.gettags(tag)[0]#of the form buttonX
  580. self.tag_bind('button',"<Any-Enter>", lambda event: self.mouseSelectButton(event))
  581. self.tag_bind('button',"<Any-Leave>", lambda event: self.mouseUnselectButton(event))
  582. self.itemconfig(tag, bg='red')
  583. self.itemconfig(tag, outline='green')'''
  584.  
  585. def join(self, event, tag):
  586. #constructs a new edge and configures shapes back
  587. if self.find_withtag('selected_button'):
  588. #adding an edge
  589. buttonX=self.gettags(self.find_withtag('selected_button')[0])[0]
  590. edgebuttonY=self.gettags(tag)[0] #EdgeY or ButtonY
  591. self.create_edge(buttonX, edgebuttonY)
  592. #reconfiguring connected objects back to previous settings
  593. self.itemconfig(buttonX, outline=self.register[buttonX].color) #configuring back to previous color
  594. if self.type(edgebuttonY)=='button': self.itemconfig(edgebuttonY, outline=self.register[edgebuttonY].color)
  595. elif self.type(edgebuttonY)=='edge': self.itemconfig(edgebuttonY, fill=self.edge_color)
  596. self.delete('selected_edge') #deleting selection_edge
  597. self.dtag('selected_button')#deleting tag
  598. self.dtag('selected_edge')#deleting tag
  599. #cleaning tag binds for button and edge selects/unselects
  600. for n in self.find_withtag('button'):
  601. #associated_tags=self.find_withbuttontag(n)
  602. buttonZ=self.gettags(n)[0]
  603. '''for m in associated_tags:
  604. print m, 'in', associated_tags, 'of'
  605. print self.register[buttonZ].photo_active
  606. print self.register[buttonZ].photo
  607. print self.mixcolor(self.register[buttonZ].color)
  608. self.tag_bind(m,"<Enter>", lambda event, i=associated_tags[1], config={'image':self.register[buttonZ].photo_active}: self.click(event,i,config))
  609. self.tag_bind(m,"<Leave>", lambda event, i=associated_tags[1], config={'image':self.register[buttonZ].photo}: self.click(event,i,config))
  610. self.tag_bind(m,"<Enter>", lambda event, i=associated_tags[0], config={'fill':self.mixcolor(self.register[buttonZ].color)}: self.click(event,i,config))
  611. self.tag_bind(m,"<Leave>", lambda event, i=associated_tags[0], config={'fill':self.mixcolor(self.register[buttonZ].color+'!60')}: self.click(event,i,config))'''
  612. self.itemconfig(buttonZ, bg='its important to stay awake')
  613. for n in self.find_withtag('edge'):
  614. self.tag_unbind(n, "<Enter>")
  615. self.tag_unbind(n, "<Leave>")
  616. #self.tag_unbind('edge',"<Enter>")
  617. #self.tag_unbind('edge',"<Leave>")
  618. else:
  619. #replace mathinput text after simple click (no double click selection done before)
  620. self.mouseDown(event, tag)
  621. buttonX=self.gettags(tag)[0]
  622. self.mathinput.delete('1.0', END)
  623. self.mathinput.insert(INSERT, self.register[buttonX].formula)
  624.  
  625. def _erase(self, erasable):
  626. """Internal function that removes edgeX and its connectives from CLICKABLES"""
  627. removed=self.clickables.pop(erasable) #removing edgeX from CLICKABLES
  628. for n in removed: self._erase(n)
  629. BEGIN=self.register[erasable].begin
  630. if self.type(BEGIN)=='button': self.clickables[BEGIN].remove(erasable) #removing edgeX from beginners of edgebuttonX
  631. END=self.register[erasable].end
  632. if self.type(END)=='button': self.clickables[END].remove(erasable) #removing edgeX from enders of edgebuttonX
  633. self.delete(self.register[erasable].id)
  634. self.register.pop(erasable) #removing edgeX from REGISTER
  635.  
  636. def erase_edge(self, event, tag):
  637. #obj is any object of edge class #WHAT IF POINTS?
  638. erasable=self.gettags(tag)[0] #edgeX
  639. self._erase(erasable)
  640. print 'after erase edge', self.clickables
  641.  
  642. def erase_button(self, event, tag):
  643. #tag is any number that is catched
  644. erasable=self.gettags(tag)[0]
  645. relatives=[n for n in self.clickables[erasable]]
  646. for n in relatives: self._erase(n)
  647. for n in self.find_withbuttontag(erasable): self.delete(n)
  648. #connected_to=self.clickables.pop(erasable)
  649. print 'after erase button', self.clickables
  650.  
  651. def create_edge(self, buttonX, edgebuttonY):
  652. #create edge between buttonX and (buttonY or tag of line) and return a new object of a class "edge"
  653. self.numOfEdges+=1
  654. sticker="edge"+str(self.numOfEdges)
  655. if self.type(edgebuttonY)=='button':
  656. new_edge = edge(self.create_line(*self.connect_bb(buttonX, edgebuttonY), arrow=LAST), buttonX, edgebuttonY)
  657. elif self.type(edgebuttonY)=='edge':
  658. new_edge = edge(self.create_line(*self.connect_bp(buttonX, edgebuttonY), arrow=LAST), buttonX, edgebuttonY)
  659. self.itemconfig(new_edge.id, tags=sticker, fill=self.edge_color, width=5)
  660. self.addtag_withtag("edge", sticker)
  661. self.tag_lower(new_edge.id)
  662. self.tag_bind(new_edge.id,"<Button-3>",lambda event, i=new_edge.id: self.popup(event,i))
  663. self.tag_bind(new_edge.id,"<Button-1>",lambda event, i=new_edge.id: self.join(event,i))
  664. #storing info: including a new edge to the keys (of clickables)
  665. self.register.update({sticker: new_edge})
  666. self.clickables.update({sticker: []})
  667. self.clickables[new_edge.begin].append(sticker)
  668. self.clickables[new_edge.end].append(sticker)
  669. return sticker
  670.  
  671. def create_button(self,*args,**kwargs):
  672. buttonX=super(MsCanvas,self).create_button(*args,**kwargs)
  673. self.register.update({buttonX: button(buttonX)})
  674. self.register[buttonX].color=self.button_color
  675. if 'image' in kwargs:
  676. self.update_formula(buttonX)
  677. self.clickables.update({buttonX:[]})
  678. self.itemconfig(buttonX, bg=self.button_color)
  679. button_tags=self.find_withbuttontag(buttonX)
  680. for n in button_tags:
  681. self.tag_bind(n,"<Button-1>",lambda event, i=n: self.join(event,i)) #self.mouseDown is inside!
  682. self.tag_bind(n,"<B1-Motion>",lambda event, i=n: self.mouseMove(event, i))
  683. self.tag_bind(n,"<Double-Button-1>",lambda event, i=n: self.double_click(event,i))
  684. self.tag_bind(n,"<Button-3>",lambda event, i=n: self.popup(event,i))
  685. #print 'after create button', self.clickables
  686. return buttonX
  687.  
  688. def connect_bb(self, button1, button2):
  689. #we use a math to get intersection points of edge between two buttons
  690. #B1,B2 are quadruples of cordinates of these buttons provided as (x1, y1, x2, y2)
  691. #segment that joins centers (a,b) and (c,d) is given as y-b=(x-a)*(d-b)/(c-a) with limitation {x in [a,c]}
  692. #SEGMENTS ARE:
  693. #segments 1,2,5,6 are involved in a first button; if these are hit, we assign a hitpoint to finalSTART
  694. #segments 3,4,7,8 are involved in a second button; if these are hit, we assign a hitpoint to finalEND
  695. #print 'connect_bbing', button1, 'and', button2
  696. rectangle1, rectangle2 = self.find_withbuttontag(button1)[0], self.find_withbuttontag(button2)[0]
  697. if rectangle1 not in self.find_overlapping(*self.coords(rectangle2)):
  698. B1,B2=self.coords(rectangle1), self.coords(rectangle2)
  699. C1 = ((B1[0]+B1[2])/2, (B1[1]+B1[3])/2) #center of button1
  700. C2 = ((B2[0]+B2[2])/2, (B2[1]+B2[3])/2) #center of button2
  701.  
  702. if C2[1]==C1[1]:
  703. if C1[0]<C2[0]: return (B1[2], C1[1], B2[0], C2[1])#edge from right center of 1 to left center of 2
  704. else: return (B2[2], C1[1], B1[0],C2[1])#reverse
  705.  
  706. if C2[0]==C1[0]:
  707. if C1[1]<C2[1]: return (C1[0], B1[3], C2[0], B2[1])#edge from bottom center of 1 to top center of 2
  708. else: return (C1[0], B2[3], C2[0], B1[1])#reverse
  709.  
  710. START,END=min(C1[0],C2[0]),max(C1[0],C2[0]) #these are x coordinates of both buttons such that START<END
  711.  
  712. def get_y(x):
  713. #given x coordinate in an edge that joins centers of button1 and button2, return y coordinate
  714. return C1[1]+(x-C1[0])*(C2[1]-C1[1])/(C2[0]-C1[0])
  715. def get_x(y):
  716. #given y coordinate in an edge that joins centers of button1 and button2, return x coordinate
  717. return C1[0]+(y-C1[1])*(C2[0]-C1[0])/(C2[1]-C1[1])
  718.  
  719. x,y=B1[0], get_y(B1[0])
  720. if B1[1]-0.001<y<B1[3]+0.001 and START<x<END: finalSTART=(x,y)
  721. ##print 'edge1: x =',x,'y =',y,'\n cond1: y is between',B1[1],'and',B1[3],'is',B1[1]<y<B1[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  722. x,y=B1[2], get_y(B1[2])
  723. if B1[1]-0.001<y<B1[3]+0.001 and START<x<END: finalSTART=(x,y)
  724. ##print 'edge2: x =',x,'y =',y,'\n cond1: y is between',B1[1],'and',B1[3],'is',B1[1]<y<B1[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  725. x,y=B2[0], get_y(B2[0])
  726. if B2[1]-0.001<y<B2[3]+0.001 and START<x<END: finalEND=(x,y)
  727. ##print 'edge3: x =',x,'y =',y,'\n cond1: y is between',B2[1],'and',B2[3],'is',B2[1]<y<B2[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  728. x,y=B2[2], get_y(B2[2])
  729. if B2[1]-0.001<y<B2[3]+0.001 and START<x<END: finalEND=(x,y)
  730. ##print 'edge4: x =',x,'y =',y,'\n cond1: y is between',B2[1],'and',B2[3],'is',B2[1]<y<B2[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  731. x,y=get_x(B1[1]), B1[1]
  732. if B1[0]-0.001<x<B1[2]+0.001 and START<x<END: finalSTART=(x,y)
  733. ##print 'edge5: x =',x,'y =',y,'\n cond1: x is between',B1[0],'and',B1[2],'is',B1[0]<x<B1[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  734. x,y=get_x(B1[3]), B1[3]
  735. if B1[0]-0.001<x<B1[2]+0.001 and START<x<END: finalSTART=(x,y)
  736. ##print 'edge6: x =',x,'y =',y,'\n cond1: x is between',B1[0],'and',B1[2],'is',B1[0]<x<B1[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  737. x,y=get_x(B2[1]), B2[1]
  738. if B2[0]-0.001<x<B2[2]+0.001 and START<=x<=END: finalEND=(x,y)
  739. ##print 'edge7: x =',x,'y =',y,'\n cond1: y is between',B2[0],'and',B2[2],'is',B2[0]<x<B2[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  740. x,y=get_x(B2[3]), B2[3]
  741. if B2[0]-0.001<x<B2[2]+0.001 and START<=x<END: finalEND=(x,y)
  742. ##print 'edge8: x =',x,'y =',y,'\n cond1: y is between',B2[0],'and',B2[2],'is',B2[0]<x<B2[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  743. return finalSTART+finalEND
  744.  
  745. def connect_bp(self, button, edge):
  746. #copied from connect_bb assuming B=B1 and C=C1 and P=(B2[0],B2[1])=(C2[0],C2[1]) where B2[0]=B2[2] and B2[1]=B2[3]
  747. rectangle = self.find_withbuttontag(button)[0]
  748. #if rectangle1 not in self.find_overlapping(*self.coords(rectangle2)):
  749. pos=self.coords(edge)
  750. midpoint_of_edge=((pos[0]+pos[2])/2, (pos[1]+pos[3])/2)
  751. B, P = self.coords(rectangle), midpoint_of_edge
  752. C = ((B[0]+B[2])/2, (B[1]+B[3])/2) #center of button
  753.  
  754. if P[1]==C[1]:
  755. if C[0]<P[0]: return (B[2], C[1], P[0], P[1])#edge from right center of 1 to left center of 2
  756. else: return (P[0], C[1], B[0], P[1])#reverse
  757.  
  758. if P[0]==C[0]:
  759. if C[1]<P[1]: return (C[0], B[3], P[0], P[1])#edge from bottom center of 1 to top center of 2
  760. else: return (C[0], P[1], P[0], B[1])#reverse
  761.  
  762. START,END=min(C[0],P[0]),max(C[0],P[0]) #these are x coordinates of both buttons such that START<END
  763.  
  764. def get_y(x): return C[1]+(x-C[0])*(P[1]-C[1])/(P[0]-C[0])
  765. def get_x(y): return C[0]+(y-C[1])*(P[0]-C[0])/(P[1]-C[1])
  766.  
  767. x,y=B[0], get_y(B[0])
  768. if B[1]-0.001<y<B[3]+0.001 and START<x<END: finalSTART=(x,y)
  769. ##print 'edge1: x =',x,'y =',y,'\n cond1: y is between',B1[1],'and',B1[3],'is',B1[1]<y<B1[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  770. x,y=B[2], get_y(B[2])
  771. if B[1]-0.001<y<B[3]+0.001 and START<x<END: finalSTART=(x,y)
  772. ##print 'edge2: x =',x,'y =',y,'\n cond1: y is between',B1[1],'and',B1[3],'is',B1[1]<y<B1[3], '\n cond2: x is between',START,'and',END,'is',START<x<END
  773. x,y=get_x(B[1]), B[1]
  774. if B[0]-0.001<x<B[2]+0.001 and START<x<END: finalSTART=(x,y)
  775. ##print 'edge5: x =',x,'y =',y,'\n cond1: x is between',B1[0],'and',B1[2],'is',B1[0]<x<B1[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  776. x,y=get_x(B[3]), B[3]
  777. if B[0]-0.001<x<B[2]+0.001 and START<x<END: finalSTART=(x,y)
  778. ##print 'edge6: x =',x,'y =',y,'\n cond1: x is between',B1[0],'and',B1[2],'is',B1[0]<x<B1[2], '\n cond2: x is between',START,'and',END,'is',START<x<END
  779. return finalSTART+P
  780.  
  781. class edge():
  782. def __init__(self, id, begin, end):
  783. self.id = id
  784. self.begin = begin
  785. self.end = end
  786.  
  787. class button():
  788. def __init__(self, tag):
  789. self.tag = tag
  790.  
  791. def click(event, tag):
  792. print 'tag is:', tag
  793.  
  794. def CLICK1(event, tag):
  795. x,y=randint(20, 680-1), randint(20, 180-1)
  796. scanvas.create_button(x, y, bg='red',fg='grey',text='HH', font="Helvetica 8 bold", outline='darkgreen')
  797.  
  798. def CLICK2(event, tag):
  799. x,y=randint(300, 600-1), randint(200, 400-1)
  800. mscanvas.create_button(x, y, borderwidth=5, outline='red', bg='red', image=xc)#activeimage=xd
  801. #this case command=click doesnt work cus event = <Button-1> of each button is rebbinded
  802.  
  803.  
  804. xc=ImageTk.PhotoImage(file='docu.png')
  805.  
  806. #scanvas = SCanvas(root, width="7i", height="2i", bg='lightblue')
  807. #scanvas.pack()
  808. mscanvas = MsCanvas(root, width="14i", height="6i", bg='#122108')
  809. mscanvas.place(x = 250, y = 0, width=1000, height=600)
  810. #RECIPE2
  811. '''
  812. for i in range(1,jo):
  813. x, y = array_of_positions[i][0],array_of_positions[i][1]
  814. scanvas.create_button(x, y, bg='red',fg='grey',text='HH',font="Helvetica 5 bold", width=2, outline='darkgreen', image=xc, command=click)
  815. for i in range(1,jo):
  816. x, y = array_of_positions[i][0],array_of_positions[i][1]
  817. mscanvas.create_button(x, y, bg='red',fg='grey',text='HH',font="Helvetica 5 bold", width=2, outline='darkgreen', image=xc, command=click)'''
  818.  
  819.  
  820. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement