Python 7 Levels, L7, Unit 83, Simple Drawing Tool I

Powered By EmbedPress

Exercise 1

import tkinter as tk

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Drawing Tool")
        self.root.geometry("300x200+100+100")
        self.createWidget()

    def createWidget(self):
        self.canvas = tk.Canvas(self.root,height=200,width=300,bg="black")
        self.canvas.pack()
        #self.canvas.create_line(20,20,20,100,150,100,fill="white",width = 3)
        #self.canvas.create_rectangle(50,50,100,100,outline="white",width=3)
        #self.canvas.create_oval(50,50,100,100,outline="white",width=3)
        global img
        img = tk.PhotoImage(file="mathton.gif")
        self.canvas.create_image(150,100,image=img)

    def run(self):
        pass

app = Application()
app.run()

Drawing Tool

import tkinter as tk

class Application:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Drawing Tool")
        self.root.geometry("900x450+100+100")
        self.drawID = 0
        self.x = 0
        self.y = 0
        self.startDrawFlag = False
        self.createWidget()
    def createWidget(self):
        self.drawpad = tk.Canvas(self.root,width=900,\
                                 height=450*0.9,bg="black")
        self.drawpad.pack()
        #Create button for line
        self.btn_line = tk.Button(self.root,text = "Line",\
                                  command=self.line_button)
        self.btn_line.pack()

        self.drawpad.bind("<ButtonRelease-1>",self.stopdraw)

    def line_button(self):
        self.drawpad.bind("<B1-Motion>",self.drawline)

    def drawline(self,event):
        self.drawpad.delete(self.drawID)
        if self.startDrawFlag == False:
            self.x = event.x
            self.y = event.y
            self.startDrawFlag = True
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,fill="white")
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts