def move_circle(event): if not active_tag: print('no tag selected') return # calculate the current position of the current shape: width = utilities.get_width(canvas, active_tag) height = utilities.get_height(canvas, active_tag) left = utilities.get_left(canvas, active_tag) top = utilities.get_top(canvas, active_tag) current_x = left + (width / 2) current_y = top + (height / 2) # calculate the delta of the current shape: delta_x = -1 * (current_x - event.x) delta_y = -1 * (current_y - event.y) # move the shape: utilities.update_position_by_tag(canvas, active_tag, x=delta_x, y=delta_y)
def move_circle(event): distance = 10 if event.keycode == keycodes.get_up_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=0, y=-distance) elif event.keycode == keycodes.get_down_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=0, y=distance) elif event.keycode == keycodes.get_left_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=-distance, y=0) elif event.keycode == keycodes.get_right_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=distance, y=0) else: print('Keycode:', event.keycode, 'not handled by this if/elif/else statement.')
def move_circle(event): # NOTE: Because Windows and Mac have different keycodes, use the functions # from the keycode module to detect the different keys distance = 10 if event.keycode == keycodes.get_up_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=0, y=-distance) elif event.keycode == keycodes.get_down_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=0, y=distance) elif event.keycode == keycodes.get_left_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=-distance, y=0) elif event.keycode == keycodes.get_right_keycode(): utilities.update_position_by_tag(canvas, tag='circle', x=distance, y=0) else: print('Keycode:', event.keycode, 'not handled by this if/elif/else statement.')
def move_circle(event): distance = 10 if event.keycode == keycodes.get_up_keycode(): utilities.update_position_by_tag(canvas, tag=active_tag, x=0, y=-distance) elif event.keycode == keycodes.get_down_keycode(): utilities.update_position_by_tag(canvas, tag=active_tag, x=0, y=distance) elif event.keycode == keycodes.get_left_keycode(): utilities.update_position_by_tag(canvas, tag=active_tag, x=-distance, y=0) elif event.keycode == keycodes.get_right_keycode(): utilities.update_position_by_tag(canvas, tag=active_tag, x=distance, y=0) else: print(event.keycode)
window_height = gui.winfo_screenheight() canvas = Canvas(gui, width=window_width, height=window_height, background='blue') canvas.pack() helper.make_turtle(canvas,(750, 180), 100, tag="turtle") def make_turtle_by_click(event): tag = 'turtle_' + str(len(data)) helper.make_turtle(canvas, (event.x, event.y), random.choice(range(0,100)), tag=tag, fill="green") data.append({ 'tag': tag, 'speed': random.uniform(1, 5) }) canvas.bind(MOUSE_DRAG, make_turtle_by_click) for item in data: tag = item['tag'] speed = -1* item['speed'] if utilities.get_bottom(canvas, tag) < 0: reset_position = window_height + utilities.get_width(canvas, tag) utilities.update_position_by_tag(canvas, tag=tag, y=reset_position) utilities.update_position_by_tag(canvas, tag=tag, x=0, y=speed) gui.update() time.sleep(0.002) for item in data: tag = item["turtle"] speed = -1 * item['speed'] if utilities.get_bottom(canvas, tag) < 0: reset_position = window_height + utilities.get_width(canvas, tag) utilities.update_position_by_tag(canvas, tag="turtle", y=reset_position) utilities.update_position_by_tag(canvas, tag="turtle", x=0, y=speed) gui.update() time.sleep(0.002) canvas.mainloop()
# 3. Initialize close button: # read about buttons: # https://www.labspoint.com/python3/tk_button.htm my_name = StringVar() Entry(mainframe, textvariable=my_name).grid(column=0, row=0, sticky=E) Button(mainframe, padx=5, pady=5, text='ADD MESSAGE', command=add_message_to_canvas).grid(column=1, row=0, sticky=E) # 4. initialize canvas: canvas_width = gui.winfo_screenwidth() canvas_height = gui.winfo_screenheight() - 100 canvas = Canvas(gui, width=canvas_width, height=canvas_height, background='white') canvas.grid(column=0, row=1, sticky=W) utilities.make_circle(canvas, (200, 200), 20, color='hotpink', tag='my_circle') while True: utilities.update_position_by_tag(canvas, 'my_circle', 2, 0) time.sleep(0.01) gui.update() ########################## YOUR CODE ABOVE THIS LINE ############################## # makes sure the canvas keeps running: canvas.mainloop()
tags=tag, outline=outline) ########################## YOUR CODE BELOW THIS LINE ############################## palette = ['#f0a202', '#f18805', '#d95d39', '#0e1428', '#7b9e89'] # first: create all of the squares: for i in range(0, 100): x = random.randint(0, 500) y = random.randint(0, 500) radius = random.randint(1, 30) fill = random.choice(palette) tag_name = 'sq_' + str(i) # give each square a unique tag make_rect(canvas, (x, y), radius, color=fill, tag=tag_name) gui.update() time.sleep(0.001) tags_of_squares_i_want_to_animate = ['sq_97', 'sq_98', 'sq_99'] # if you later want to animate anything: while True: for tag in tags_of_squares_i_want_to_animate: utilities.update_position_by_tag(canvas, tag, x=0, y=2) gui.update() time.sleep(0.001) ########################## YOUR CODE ABOVE THIS LINE ############################## # makes sure the canvas keeps running: canvas.mainloop()
tag='creature_1', fill='#f0a202') creature.make_creature(canvas, (100, 400), size=50, tag='creature_2', fill='#f18805') creature.make_creature(canvas, (50, 400), size=75, tag='creature_3', fill='#f0a202') creature.make_creature(canvas, (400, 100), size=250, tag='creature_4', fill='#d95d39') creature.make_creature(canvas, (350, 350), size=150, tag='creature_5', fill='#7b9e89') palette = ['#f0a202', '#f18805', '#d95d39', '#0e1428', '#7b9e89'] while True: utilities.update_position_by_tag(canvas, 'creature_1', x=1, y=1) utilities.update_position_by_tag(canvas, 'creature_4', x=-1, y=0) gui.update() time.sleep(0.01) ########################## YOUR CODE ABOVE THIS LINE ############################## # makes sure the canvas keeps running: canvas.mainloop()
import math import time gui = Tk() gui.title('Circle') canvas = Canvas(gui, width=500, height=500, background='white') canvas.pack() ########################## YOUR CODE BELOW THIS LINE ############################## center_x = 200 center_y = 200 distance_from_center = 120 center = (center_x, center_y) center_creature = (center_x, center_y - distance_from_center) make_creature(canvas, center_creature, 60, fill='hotpink', tag='creature') make_circle(canvas, center, 10, color='black') counter = 0 slow_factor = 250 while True: # calculate new position of x and y radians = counter / slow_factor dy = distance_from_center / slow_factor * math.sin(radians) dx = distance_from_center / slow_factor * math.cos(radians) update_position_by_tag(canvas, tag='creature', x=dx, y=dy) counter += 1 gui.update() time.sleep(.01) ########################## YOUR CODE ABOVE THIS LINE ############################## canvas.mainloop()