3
votes

I am making a tutorial to explain things to others. For that tutorial i am trying to make a python program (which is like the paint app)

Which we all use in windows. To draw with pen,brush and to draw shapes like square,circle and have a option for color piker to choose colour to draw.

I already tried with the from tkinter import choosecolor to create paint like software in python.

But with that it draws only on a tkinter canvas.

But i don't want to draw on a canvas i want to draw it on live screen while i make the tutorial.

example image is shown below

enter image description here

I am trying to make a gui window like this to choose color and pen tool to draw on the screen (eg.desktop,web browser etc).

enter image description here

Can anyone give me some suggestion on how can i draw like this on my desktop screen or on any window.

1
So just make a screenshot and put it in a tkinter.Canvas is okay.jizhihaoSAMA
@jizhihaoSAMA no i want to draw live like this (youtu.be/UNarqPBpWsw?t=300) see this exampleamit9867
Put it in a Canvas is the first step.You also need to create a Toplevel() to show the tool(adding word,drawing,creating line and so on).jizhihaoSAMA

1 Answers

4
votes

Although in your video,it seems that " draw directly on screen ".Actually,I think it didn't.

There is a easy example to "draw on the screen",You can modify it:

import tkinter as tk
from PIL import ImageGrab,ImageTk
import ctypes

ctypes.windll.shcore.SetProcessDpiAwareness(2) # windows 10

class ToolWin(tk.Toplevel):
    def __init__(self):
        tk.Toplevel.__init__(self)
        self._offsetx = 0
        self._offsety = 0
        self.wm_attributes('-topmost',1)
        self.penSelect = tk.BooleanVar()
        self.overrideredirect(1)
        self.geometry('200x200')
        self.penModeId = None
        self.bind('<ButtonPress-1>',self.clickTool) 
        self.bind('<B1-Motion>',self.moveTool) # bind move event

        draw = tk.Checkbutton(self,text="Pen",command=self.penDraw,variable=self.penSelect)
        draw.pack()
        cancel = tk.Button(self,text="Quit",command=root.destroy)
        cancel.pack()

    def moveTool(self,event):
        self.geometry("200x200+{}+{}".format(self.winfo_pointerx()-self._offsetx,self.winfo_pointery()-self._offsety))

    def clickTool(self,event):
        self._offsetx = event.x
        self._offsety = event.y

    def penDraw(self):
        if self.penSelect.get():
            self.penModeId = root.bind("<B1-Motion>",Draw)
        else:
            root.unbind('<B1-Motion>',self.penModeId)

def Draw(event):# r = 3
    fullCanvas.create_oval(event.x-3,event.y-3,event.x+3,event.y+3,fill="black")

def showTool(): # the small tool window
    toolWin = ToolWin()
    toolWin.mainloop()

root = tk.Tk()
root.state('zoomed')
root.overrideredirect(1)

fullCanvas = tk.Canvas(root)
background = ImageTk.PhotoImage(ImageGrab.grab(all_screens=True)) # show the background,make it "draw on the screen".
fullCanvas.create_image(0,0,anchor="nw",image=background)
fullCanvas.pack(expand="YES",fill="both")

root.after(100,showTool)

root.mainloop()

Also,you can move the toolbar by dragging it. (PS: I think you nearly finished it.)