[Tutor] creating a mspaint utility

Whom Isac wombingsac at gmail.com
Thu Jan 14 05:58:13 EST 2016


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.
I know how to draw them with canvas very well but the problem is the way I
am getting my mouse positions (initial& final).
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. Should I write a function to store
them inside of it. I don't know because I had tried doing just that
recursively and failed and unfortunately I erased that part in the process.
I am not a genius and is not used to tkinter very well (to use command
function or anything) to store a value.
Here is my code. I kind of left some reduntant codes to show my try and
fail situation. Thus my code is quite long.

__author__ = 'WHOM ISAC'
import tkinter as tk
from tkinter import *
import sys
#import time

#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")
    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

# 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



#Mouse Update And Position
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)

#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
        finalpos=(0,0)
        if initialpos !=finalpos:
            if Mouse_pressed(event):
                finalpos=app.winfo_pointerxy()
    print(initialpos)
    """
    print(initialpos)
    return initialpos


#Mouse coordination
"""
initial_pos=Mouse_pressed
print(initial_pos)
time.sleep(1)
final_pos=Mouse_pressed
print(final_pos)
"""

#SOME WIDGETS:
lbl0=Label(app, text="This is a program that I have build Using Python.
Please Use it.", fg='blue', font='Times 9 bold').pack(fill=BOTH,anchor='nw')
####Frame for Original Mouse Position
frame1=Frame(app, bg='red', width=2).pack(fill=BOTH,anchor='ne')




#Canvas tools
"""CanvasFrame=Frame(app, width=300, height=200)"""
canvas_GUI=Canvas(app, height=300, width=300, bg='white')
       #canvas_draw_tool=canvas_GUI.create_line(20,0,100,200)
canvas_draw_tool=canvas_GUI.create_line(20,0,(X,Y)) #I know it won't work
unless I could get the initial position and final position but I don't have
a clue

##CanvasFrame Binding
canvas_GUI.bind("<Button-1>", Mouse_pressed)
canvas_GUI.bind('<Motion>', motion)
canvas_GUI.pack()

#QUIT Function
def quit():
    print("Quit function has been called. So I am quitting.")
    sys.exit()



#CLEAR Function
def clear():
    canvas_GUI.delete("all")
    print("Everything has been flushed.")



#Buttons
Button(app, text='Quit', command=quit).pack(anchor='sw',side=LEFT)
Button(app, text='Clear', command=clear).pack(anchor='sw',side=LEFT)
Button(app, text='Show', command=mouse_position).pack(anchor='sw',side=LEFT)
"""
initial_pos=Label(app,text="Initial
pos:{}".format(app.winfo_pointerxy())).pack()
final_pos=Label(app,text="Final
pos:{}".format(app.winfo_pointerxy())).pack()
canvas_GUI.bind(Mouse_pressed,initial_pos)
canvas_GUI.bind(Mouse_pressed,final_pos)
"""
#Mainloop running
original_mouse_position()
app.mainloop()


More information about the Tutor mailing list