How do I work with the geometry of Tkinter in Python? -
i've been having trouble wrapping head around geometry of tkinter. have range of questions hoping kind enough clear me.
when making rectangle in below code passed in 4 parameters. i'm aware they're x1, y1, x2, y2 parameter how work? can't seem create rectangle of specific size , place want. also, 20+20 thing mean in 3rd , fourth parameter exactly?
how detect specific coordinate? able detect line on screen , execute code based when rectangle touches i'm clueless. can't position line in position want either emphasize how little tkinter's geometry.
window = tk() window.geometry("400x200+450+300") canvas1 = canvas(window, width = 600, height = 300, bg='white') canvas1.pack() canvas1.create_line(25, 50, 50, 50) robot = canvas1.create_rectangle(10, 10, 20+20, 20+20) x1, y1, x2, y2 = canvas1.coords(robot) def right(event): x1, y1, x2, y2 = canvas1.coords(robot) canvas1.coords(robot, x1+10, y1, x2+10, y2) def left(event): x1, y1, x2, y2 = canvas1.coords(robot) canvas1.coords(robot, x1-10, y1, x2-10, y2) def up(event): x1, y1, x2, y2 = canvas1.coords(robot) canvas1.coords(robot, x1, y1-10, x2, y2-10) def down(event): x1, y1, x2, y2 = canvas1.coords(robot) canvas1.coords(robot, x1, y1+10, x2, y2+10) canvas1.focus_set() canvas1.bind("<right>", right) canvas1.bind("<left>", left) canvas1.bind("<up>", up) canvas1.bind("<down>", down) thanks help.
tkinter canvas coordinates start 0,0 @ upper left corner of canvas. x gets bigger point moves right. y gets bigger point moves down.
consider line of code:
robot = canvas1.create_rectangle(10, 10, 20+20, 20+20) this creates rectangle formed coordinates (10,10) , (40,40). starting upper left corner, tkinter count ten pixels on , ten pixels down draw first corner of rectangle. opposite corner of rectangle (40,22). (not scale):
point 1 (10,10) + + point 2 (40, 40) tkinter create rectangle computing missing corners wich @ (40,10) , (10,22)
point 1 (10,10) +--------------+ | | | | +--------------+ point 2 (40,40) as second question (which should separate question), can determine if 1 object overlaps canvas find_overlapping method.
Comments
Post a Comment