[Tutor] Fwd: Re: creating a mspaint utility

Alan Gauld alan.gauld at btinternet.com
Fri Jan 15 03:34:37 EST 2016


forwarding to tutor list... please use ReplyAll when responding to the list.

-------- Forwarded Message --------
Subject: 	Re: [Tutor] creating a mspaint utility
Date: 	Fri, 15 Jan 2016 16:07:36 +1000
From: 	Whom Isac <wombingsac at gmail.com>
To: 	Alan Gauld <alan.gauld at btinternet.com>



Hi, Alan Gauld, in regards to the response I got from you, I am using
python 3.4 while writing the code but normally use 3.5. And this is just
a practice tutorial that I thought about my self to make it because I
know GUI-canvas can let you draw if you know the coordinates. I thought
maybe you can get the mouse positions and put them in order to draw a
line,etc.
I see you identified some issues which I have problem with because I
know mistakes already but You see if you have run the code into an
interpreter or IDLE it would show you the mouse co-ordinates each time
you right-click on the canvas (not the tkinter, intentionally) to draw.
I did not want to use global variable because I am not used to this
function in python or any lambda function because it's quite hard. 

And for original Mouse position function:

>     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

 I wanted to show the mouseposition under a label with X and Y (at the
GUI-startup). Then if mouse position changes then make another label
underneath with the current mouse position with MousePosition method:

>def mouse_position():
>    show_event=app.winfo_pointerxy()
>    (X,Y)=show_event
>    if '<Motion>'!=show_event :
>        show_event=app.winfo_pointerxy()
>        (X,Y)=show_event
>        print("Current mouse are on: X:{0} Y:{1}".format(X,Y))
>    label2=Label(frame1,text=str(show_event),
relief=GROOVE).pack(side=RIGHT)

And you are right that I have to call the same code to find the
mouse-position again and again. Because I think if you don't right the
variable first or inside the function it will not work.

Then again I don't know what got me but I thought that I could write a
function for mouse pressed which will print(Right Click has been
pressed.) and take a variable to store the mouse position. Now I think I
can modify the code to use separetely to find the coordinates but I
don't know how to do that. Because in each right click the initialpos
and finalpos of the mouse will change and they would have the same
value. Should not they??

That's why I got so confused and made  a plan to write a small code with
it so it will compare final position and initial position with this:
 >finalpos=(0,0)
>         if initialpos !=finalpos:
>             if Mouse_pressed(event):
>                 finalpos=app.winfo_pointerxy()

It's not possible for final position to be (0,0) which I know better so
it would make the function to store a new finalposition. This attempt
failed so I comment them out. This is the same trick I used for getting
the initialposition which you did not include. So no point in arguing
about who is wrong.

If you had tested the code you might have notice that each time I am
pressing the show button the command keep printing or making a label to
the right. How do I keep only a single label and delele the old label.
And each time I click on the canvas initialposition and finalposition
become the same value. And even though I made sure the label-Original
Mouse Position should be followed by(" X Y ") and the mouse position
underneath the order is not right when I run the GUI.

So any idea how should I store the initial and final position e.g.
inside a function or as global value.?? or An advice to what should I do
about mouse position functions? Should I reduce any of them? Is it a
problem to put labels inside them?

If you have not run the GUI yet, here are some of the snapshot I took:
 


​

On Fri, Jan 15, 2016 at 10:50 AM, Alan Gauld <alan.gauld at btinternet.com
<mailto:alan.gauld at btinternet.com>> wrote:

    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


    _______________________________________________
    Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
    To unsubscribe or change subscription options:
    https://mail.python.org/mailman/listinfo/tutor






More information about the Tutor mailing list