Mouse support in TurtleArt
Here is how mouse support can be added to Turtle Art V84. In this example, mouse left click events are registered by setting a flag, the x and y at the mouse click can be read.
[note as of Turtle Blocks V87 no patching of tawindow.py is required and the python block code is in the sample myblock file]
It requires some modification of the program code, three new lines need to be added to the file tawindow.py near the beginning:
and three new lines about 1/4 way into the file:
Then a bit of code in the programmable block tamyblock.py makes it all happen.
Help understanding the detail of Turtle Art's internal Object Oriented Python programming is at Python_code_block
File:Turtle_Art_Activity_mouse.ta
(note: blog does not preserve indents, correct Python indenting is required)
[note as of Turtle Blocks V87 no patching of tawindow.py is required and the python block code is in the sample myblock file]
It requires some modification of the program code, three new lines need to be added to the file tawindow.py near the beginning:
def __init__(self, win, path, parent=None, mycolors=None):
self.mouseflag=0
self.mouse_x=0
self.mouse_y=0
self.win = None
and three new lines about 1/4 way into the file:
def _buttonpress_cb(self, win, event):These changes are necessary so that the programmable block tamyblock.py can "see" the mouse event. Turtle Art's files can be modified with Browse and Pippy or with the Linux command line editor vi see Modifing_an_Activity
""" Button press """
self.window.grab_focus()
x, y = xy(event)
self.mouseflag=1
self.mouse_x=x
self.mouse_y=y
self.button_press(event.get_state()>k.gdk.CONTROL_MASK, x, y)
return True
Then a bit of code in the programmable block tamyblock.py makes it all happen.
def myblock(lc, x):
if lc.tw.mouseflag==1:
lc.heap.append(lc.tw.mouse_y)
lc.heap.append(lc.tw.mouse_x)
lc.heap.append(1)
lc.tw.mouseflag=0
else:
lc.heap.append(0)
return
Help understanding the detail of Turtle Art's internal Object Oriented Python programming is at Python_code_block
File:Turtle_Art_Activity_mouse.ta
(note: blog does not preserve indents, correct Python indenting is required)
2 Comments:
Nice!
The modifications to tawindow.py have been incorporated into Version 87.
Post a Comment
<< Home