Moving 1 Picture in a Programm - How to?

Rick Johnson rantingrickjohnson at
Thu Jan 1 20:50:21 CET 2015

On Thursday, January 1, 2015 12:44:13 PM UTC-6, lucas mvm wrote:
> Hey Guys! I need your help! So i want to make a program
> like the app called "Feed Me" There are 2 Pictures (I got
> that) than you can click the food picture and drag it on
> the boys mouth and than the sad boys face disappears and a
> happy face shows up.

This interactivity will require mastering a few basic
events. The Tkinter canvas offers a few extra methods of
binding events besides the normal "widget level" event
bindings. Since you are beginning you should focus on the
ubiquitous bindings at the "widget level" -- master those
before moving on to "tag level bindings".

First step: FORGET ABOUT PICTURES, because they will only
get in your way at this time. First step is to create three
events that map mouse clicks, mouse drags, and mouse
releases to print messages to stdout. THEN, and only then
you can move to the next step!

    DIVIDE AND CONQUER... in teeny tiny baby steps!
But first allow me to critique your code.

> Heres the code ive wrtiten
> from tkinter import * 

Oh no... I would not suggest importing everything (even
though the library can be) since doing so pollutes your
namespace with many names you'll never use. Instead do:
"import tkinter as tk" and prefix every class with "tk.". For
the constants you do either "from tkinter.constants import
*" or individually import only the names you will use (the
second option is obviously better) But for now, let's just
get this code working, shall we?

> f = Tk()

Why would you use "f" as a variable for a tkinter window? A
better symbol would be "root" or "topwin". Make sure your
symbols are describing succinctly what they reference (THIS
IS VERY IMPORTANT!) Even though Tkinter allows you to use a
"frame" as a "window" (frame.mainloop()), it only works
because there is some "magic" going on behind the scenes.
You must understand that "frames != windows".

> f.title('Give the boy the apple.')
> f.geometry('500x500')
> c = Canvas(master=f,width=500,height=500,bg='white')

The "place" geometry manager should only be use in
*extremely* specific circumstances. And since your code does
not require such "needs" i suggest you replace ""
with "c.pack(fill=BOTH, expand=YES)". There are three
geometry managers in Tkinter:

    1. Pack
    2. Grid
    3. Place
When your trying to decide which manager to use, start with
the "pack manager" first, if it cannot solve your needs then
contemplate the "grid manager", and only after deciding
that the "pack manager" and "grid manager" won't work, then
you can us the "place manager".

> p = PhotoImage(file='traurigsmiley.png') #sad smiley
> i = c.create_image(250,320,image=p) #position
> p2 = PhotoImage(file='essen.png') #food
> i2 = c.create_image(70,100,image=p2)
> f.geometry('500x500')
> f.mainloop()
> Please Help!

First, rewrite your code in the manner i suggest, then
expand it with the following.

1. Bind three events to your canvas object:

    c.bind("<Button>", evtButtonDown)
    c.bind(...) # < -- homework!
    c.bind(...) # < -- homework!

1. Create three functions that act as callback for the events:

    def evtButtonDown(evt):
        # print the number of the button pressd here!
    def evtButtonUp(evt):
        # print the number of the button pressd here!
    def evtMouseMove(evt):
        # print the x,y location of the cursor here!


More information about the Python-list mailing list