[Tutor] creating a mspaint utility

Alan Gauld alan.gauld at btinternet.com
Thu Jan 14 19:50:32 EST 2016


On 14/01/16 10:58, Whom Isac wrote:
> Hi, I was wondering if it is possible to make a similar drawing tool with
> basic functionality to draw lines, circles or square with python canvas.

Yes of course and there are at least a couple of online tutorials
on how to do that. Google is your friend.

> I did not think it would have been a difficult work but I have spent 3-4
> hours and out of luck. I get my functions to give me the mouse position
> while moving, when pressed, original position or current position. But
> don't know how to store them as a value. 

Use variables just like any other value.
GUI work is always harder than you expect and ful;l of
frustrating details. 3-4 hours on a GUI project is not
long at all.

> I am not a genius and is not used to tkinter very well (to use command
> function or anything) to store a value.

Which tkinter tutorial are you reading?

> import tkinter as tk
> from tkinter import *
> import sys

> #app GUI
> app=tk.Tk()
> app.title("MSPAINT By Shams")
> app.geometry('400x450')
> 
> #
> show_event=app.winfo_pointerxy()
> (X,Y)=show_event
> 
> #Mouse events
> def original_mouse_position():
>     show_event=app.winfo_pointerxy()
>     (X,Y)=show_event
>     print("The mouse are on: X:{0} Y:{1}".format(X,Y))
>     label0=Label(frame1,text="Original Position",
> relief=RAISED).pack(side=TOP,anchor="ne")

pavck() and grid() both return None so if you want to store a reference
to the widget you MUST split it over two lines

widgetVar = Widget()
widgetVar.pack()   # or grid()

Otherwise your widgetVar will just store None.

>     label=Label(frame1,text="X    Y",relief=GROOVE).pack(side=BOTTOM,
> anchor="ne")
>     label1=Label(frame1,text=str(show_event),
> relief=SUNKEN).pack(side=BOTTOM, anchor="ne")
>     return

This is confusing because your function is called
original_mouse_position() but it does a lot more than
that - it creates labels and all sorts. You should
either split this into several smaller functions
or change the name to reflect what the function
actually does.


> # Continuous Mouse Movement
> 
> def motion(event):
>     x, y = event.x, event.y
>     currentMousePosition=(x,y)
>     print('MousePos: X:{0} Y:{1}'.format(x, y))
>     return currentMousePosition
> ###app.bind('<Motion>', motion)-->WORKS but disabled from running

You return the x,y coordinates but the values are not
used by Tkinter when it calls the function in
response to a Motion event. You probably want
to store them in global variables - remember
to use the global keyword.

> 
> #Mouse Update And Position
> def mouse_position():
>     show_event=app.winfo_pointerxy()
>     (X,Y)=show_event

You've used these two lines several times. Probably
better to make them into a function.

>     if '<Motion>'!=show_event :

This will always be true since you are comparing
a literal string ('<Motion>') with an (x,y) tuple.
They can never be equal.

>         show_event=app.winfo_pointerxy()
>         (X,Y)=show_event

and yet again you repeat these lines.

>         print("Current mouse are on: X:{0} Y:{1}".format(X,Y))
>     label2=Label(frame1,text=str(show_event),
> relief=GROOVE).pack(side=RIGHT)
> 
> #app.bind(mouse_position(),'Show')
> 
> #Mouse pressed
> def Mouse_pressed(event):
>     print("Right Click has been pressed.")
>     initialpos=(X,Y)
>     initialpos=app.winfo_pointerxy()
>     #finalpos=motion(event)
>     """
>     while initialpos!=(0,0):
>         initialpos=app.winfo_pointerxy()                   #Explain me why
> it does not work.Should not it work?it's logical to do/call recursive

There is nothing recursive going on.
You set initialpos to the mouse position in the 3rd line
of this function. You then loop round doing it until the
mouse is on position 0,0 - ie extreme top left.

>         finalpos=(0,0)
>         if initialpos !=finalpos:
>             if Mouse_pressed(event):
>                 finalpos=app.winfo_pointerxy()

I've no idea what you think this bit is doing.

>     print(initialpos)
>     """
>     print(initialpos)
>     return initialpos
> 

That's as far as I went because there are enough big issues
to fix already that finding more would not be useful.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list