1 Forum Post by "pybrah"
Python Tkinter Problem
Posted March 21st, 2014 in Programming
Hey guys, I'm having a problem with Tkinter. My program creates a box and moves where ever you click. But on the clicked location it jutters. I know it has something to do with the x and y velocity but I can't figure it out. Here is the code.
import tkinter as tk
import time
def mouse_clicked(event):
print(event.x, " ", event.y)
global target_x
global target_y
target_x = event.x
target_y = event.y
target_x = 100
target_y = 100
BOX_WIDTH = 30
BOX_HEIGHT = 40
box_x = 20
box_y = 30
x_velocity = 0
y_velocity = 0
window = tk.Tk()
canvas = tk.Canvas(window, width=600, height=500)
canvas.pack()
canvas.update()
canvas.bind("<Button 1>", mouse_clicked)
canvas.focus_set()
my_rectangle = canvas.create_rectangle(box_x, box_y, box_x + BOX_WIDTH, box_y + BOX_HEIGHT, fill="blue")
while True:
time.sleep(0.02)
canvas.move(my_rectangle, x_velocity, y_velocity)
box_x += x_velocity
box_y += y_velocity
if box_x > target_x:
x_velocity = -5
if box_x < target_x:
x_velocity = 5
if box_y > target_y:
y_velocity = -5
if box_y < target_y:
y_velocity = 5
if box_x and box_y...
canvas.update()
window.mainloop()

