Reputation: 330
I am using tkinter to generate a gui.
from Tkinter import *
root = Tk()
root.title("explorer")
f=Canvas(root, width=1200, height=768)
f.grid()
main_line = f.create_line(200,300,300,300, tags="main_line_tag", width=5)
mainloop()
in the above sample i want to print tag and id beside the line. how can i do it.
Update
main_line = f.create_line(200,300,300,300, tags="main_line_tag", width=5)
tags_text = ', '.join(f.gettags(main_line))
line_text = "%s: %s" % (main_line, tags_text)
f.create_text(220,320, text=line_text)
solution working but my actual requirement is
f=Canvas(root, width=1200, height=768)
f.grid()
class Create:
def __init__(self,xy,t):
self.xy=self.xy
self.t=t
for i in range(1, self.t+1):
exec 'main_line%d = f.create_line(200,300,300,300, tags="main_line_tag%d")' %(i,i)
#end of class Create
def update(t,newxy):
for i in range(1,t+1):
exec'f.coord(main_line%d, *newxy)'%i
mainloop()
in the above sample code iam trying to update coordinates of the line when i run the code i am getting main_line1 not defined.. even for tags also. how to resolve this.
Thanks
Upvotes: 0
Views: 2524
Reputation: 20679
Just draw a text item, and use the ID and the gettags
Canvas method to create your text.
main_line = f.create_line(200,300,300,300, tags="main_line_tag", width=5)
tags_text = ', '.join(f.gettags(main_line))
line_text = "%s: %s" % (main_line, tags_text)
f.create_text(220,320, text=line_text)
Update
The problem with exec 'main_line%d = ...
is that you are creating the line, but not storing the reference. However, using exec
is a non recommendable solution and there are another ways to do the same as you want with a simple list:
lines = []
def update(t, newxy):
for line in lines:
f.coord(line, *newxy)
class Create:
def __init__(self, xy, t):
self.xy = xy
self.t = t
for i in xrange(self.t):
new_line = f.create_line(200,300,300,300, tags="main_line_tag%d" % (i+1))
lines.append(new_line)
Upvotes: 1