wewewewe

wrwrwrwr
mail@pastecode.io avatar
unknown
python
7 months ago
3.6 kB
11
Indexable
Never
from tkinter import *
from tkinter.colorchooser import askcolor
from tkinter import messagebox

class DrawRubberShapes(object):
    def __init__(self):
        window = Tk()
        window.title("Lab 09: Drawing Rubber Shapes")
        frame1 = Frame(window)
        frame1.pack()

        self.fillColor = StringVar()
        self.fillColor.set('red')
        def colorCommand():
            (rgb,color) = askcolor()
            if color != None:
                self.fillColor.set(color)
                colorButton["bg"] = color
        colorButton = Button(frame1, text = "Color", command=colorCommand, bg=self.fillColor.get())
        colorButton.grid(row=1,column=1,columnspan=2)

        self.v1 = StringVar()
        rbRectangle = Radiobutton(frame1, text = "Rectangle", variable = self.v1, value = 'R', command = self.processRadiobutton)
        rbRectangle.grid(row=1, column=3)
        rbLine = Radiobutton(frame1, text = "Line", variable = self.v1, value = 'L', command = self.processRadiobutton)
        rbLine.grid(row=1, column=4)
        rbOval = Radiobutton(frame1, text = "Oval", variable = self.v1, value = 'O', command = self.processRadiobutton)
        rbOval.grid(row=1, column=5)

        clearButton = Button(frame1, text = "Clear", command = self.clearCanvas)
        clearButton.grid(row=1, column=6)

        self.canvas = Canvas(window, width=480, height=380)
        self.canvas.pack()
        self.canvas.bind('<ButtonPress-1>', self.onStart)
        self.canvas.bind('<B1-Motion>', self.onGrow)
        self.canvas.bind('<ButtonPress-3>', self.startMove)
        self.canvas.bind('<B3-Motion>', self.moving)

        window.bind('<d>', self.deleteShape)
        window.bind('<h>', self.showHelp)

        self.object = None
        self.shape = self.canvas.create_rectangle

        window.mainloop()

    def processRadiobutton(self):
        shape = self.v1.get()
        if shape == 'R':
            self.shape = self.canvas.create_rectangle
        elif shape == 'L':
            self.shape = self.canvas.create_line
        elif shape == 'O':
            self.shape = self.canvas.create_oval

    def clearCanvas(self):
        self.canvas.delete('all')

    def onStart(self, event):
        self.start = event
        self.object = None

    def startMove(self, event):
        self.startMoving = event
        objectTuple = self.canvas.find_closest(event.x, event.y)
        if objectTuple != ():
            self.object = objectTuple[0]

    def moving(self, event):
        canvas = event.widget
        if self.object:
            canvas.move(self.object, event.x - self.startMoving.x, event.y - self.startMoving.y)
            self.startMoving = event

    def onGrow(self, event):
        canvas = event.widget
        if self.object:
            canvas.delete(self.object)
        if self.v1.get() == 'L':
            objectId = self.shape(self.start.x, self.start.y, event.x, event.y, fill=self.fillColor.get())
        else:
            objectId = self.shape(self.start.x, self.start.y, event.x, event.y, fill=self.fillColor.get(), outline=self.fillColor.get())
        self.object = objectId

    def deleteShape(self, event):
        if self.object:
            self.canvas.delete(self.object)
            self.object = None

    def showHelp(self, event):
        messagebox.showinfo("Help", "Left click and drag to draw a shape.\nRight click and drag to move a shape.\nPress 'd' to delete the selected shape.")

if __name__ == '__main__':
    DrawRubberShapes()
Leave a Comment