0

So I am making an Tkinter painting application but I keep getting a Value:Error which is blocking me from proceeding and I don't know how to solve it.

here is my code

from tkinter import *
from tkinter import Scale
from tkinter import colorchooser, filedialog, messagebox
import PIL.ImageGrab as ImageGrab


class Paint():          # the class adds the screen for all the widgets to be added on
    def __init__(self,root):
        global color
        self.root = root
        self.root.title("Paint")
        self.root.geometry("800x520")
        self.root.configure(background='white')
        self.root.resizable(100,100)

        self.pen_color ="black"
        self.eraser_color = "white"


        # Adding the widgets
        self.color_frame = LabelFrame(self.root, text='Color Palette', font=("Lucida's Caligraphy",15),bd=10, relief=RIDGE, bg='pink')
        self.color_frame.place(x=0,y=0,width=145,height=190)

        colors = ["#ff00ff", "#ff0000", "#ffa600", "#ffff00", "#80ff80", "#00ff00", "#09ff90", "#0ff8c0", "#00d0ff", "#00ffff", "#ffffff", "#fff3d4", "#808080", "#404040", "#202020", "#000000"]
        i=j=0

        for color in colors: # this is the creation for the positioning of the colors
            Button(self.color_frame,bg=color,bd=2,relief=RIDGE,width=3,command=lambda col=color:self.select_color(col)).grid(row=i,column=j) # this is the creation for the color buttons
            i+=1
            if i==4:
                i=0
                j+=1

        self.eraser_button = Button(self.root,text='Eraser',bg='violet',bd=2,relief=GROOVE,width=3,command=self.eraser)     # this is the creation for the eraser button
        self.eraser_button.place(x=0, y=187, width=60, height=30)

        self.clear_button = Button(self.root,text='Clear',bg='light blue',bd=2,relief=GROOVE,width=3,command=lambda : self.canvas.delete("all"))     # this is the creation for the clear button
        self.clear_button.place(x=0, y=217, width=60, height=30)

        self.save_button = Button(self.root,text='Save',bg='light green',bd=2,relief=GROOVE,width=3,command=self.save_paint)        # this is the creation for the save button
        self.save_button.place(x=0, y=247, width=60, height=30)

        self.canvas_button = Button(self.root,text="Canvas",bg='light cyan', bd=4, width=8,relief=GROOVE,command=self.canvas_color)     # this is the creation for the canvas button
        self.canvas_button.place(x=0, y=277)

        self.pen_scale_frame = LabelFrame(self.root,text='size', bg='white', bd=5, font=('arial', 15), relief=RIDGE,)   # this is the creation for the box that contains the colors
        self.pen_scale_frame.place(x=0, y=310, width=70, height=200)

        self.pen_scale = Scale(self.pen_scale_frame, orient=VERTICAL,from_=100, to=0,length=150)    # this is the creation for the scale
        self.pen_scale.set(1)
        self.pen_scale.place(x=5,y=10)

        self.canvas = Canvas(self.root, bg='light cyan', bd=4, relief=GROOVE, width=1105, height=630)   # this is the creation for the canvas
        self.canvas.place(x=150, y=0)

        self.canvas.bind("<B1-Motion>",self.paint)      # this binds the mouse motion with the canvas

    def paint(self,event): # this is the function for the ink to be shown on the canvas
        x1,y1 = (event.x-0),(event.y-0)
        x2,y2 = (event.x+0), (event.y+0)

        self.canvas.create_oval(x1,y1,x2,y2,fill=self.pen_color,outline=self.pen_color,width=self.pen_scale.get())



    def select_color(self, col):    # this is the function for selecting colors for my pen
        self.pen_color = col


    def eraser(self):       # this is the function for copying the colors that my canvas has for my eraser
        self.pen_color = self.eraser_color

    def canvas_color(self):     # this is the function for selecting colors for my canvas
        color = colorchooser.askcolor()
        self.canvas.configure(background=color[1])
        self.eraser_color = color[1]
        self.pen_color = color[1]

    def save_paint(self):       # this is the function for screenshotting whatever is on the canvas
        global filename

        filename = filedialog.asksaveasfilename(initialdir='C:/',filetypes=[('All files','.*'), ('Picture Files','.png*')])
        print(filename)
        cx = self.root.winfo_rootx() + self.canvas.winfo_x()
        print(cx, self.root.winfo_rootx())
        cy = self.root.winfo_rooty() + self.canvas.winfo_y()
        print(cy)
        cx1 = cx + self.canvas.winfo_width()
        print(cx1)
        cy1 = cy + self.canvas.winfo_height()
        print(cy1)
        filename = filename + '.jpeg'
        ImageGrab.grab().crop((cx1, cy1, cx, cy)).save(filename)    # <-- The value:Error redirects me here
        messagebox.showinfo('paint sys','image is saved as '+ str(filename))



if __name__ == "__main__":
    root = Tk()
    p = Paint(root)
    root.mainloop()

the error

**ImageGrab.grab().crop((cx1, cy1, cx, cy)).save(filename) line 1171, in crop raise ValueError("Coordinate 'right' is less than 'left'") ValueError: Coordinate 'right' is less than 'left' **

Whats wrong with my code?

0 Answers0