Python 7 Levels, L7, Unit 84, Simple Drawing Tool II

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

Exercise 2

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(side = "left",padx=10)
        
        #Create button for Rectangle
        self.btn_rect = tk.Button(self.root,text = "Rectangle",\
                                  command=self.rect_button)
        self.btn_rect.pack(side = "left",padx=10)

        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 rect_button(self):
        self.drawpad.bind("<B1-Motion>",self.drawrect)

    def drawrect(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_rectangle(self.x,self.y,\
                                               event.x,event.y,outline="white")
    
    
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 3

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(side = "left",padx=10)
        
        #create button for Rectangle
        self.btn_rect = tk.Button(self.root,text = "Rectangle",\
                                  command=self.rect_button)
        self.btn_rect.pack(side = "left",padx=10)

        #create button for Eclipse
        self.btn_oval = tk.Button(self.root,text = "Eclipse",\
                                  command=self.oval_button)
        self.btn_oval.pack(side = "left",padx=10)

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

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

    def drawline(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,fill="white")

    def rect_button(self):
        self.drawpad.bind("<B1-Motion>",self.drawrect)

    def drawrect(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_rectangle(self.x,self.y,\
                                               event.x,event.y,outline="white")
    def oval_button(self):
        self.drawpad.bind("<B1-Motion>",self.drawoval)

    def drawoval(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_oval(self.x,self.y,\
                                               event.x,event.y,outline="white")

    def startdraw(self,event):
        self.drawpad.delete(self.drawID)
        if self.startDrawFlag == False:
            self.x = event.x
            self.y = event.y
            self.startDrawFlag = True
    
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 4

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",\
                                  name="line")
        self.btn_line.pack(side = "left",padx=10)
        
        #create button for Rectangle
        self.btn_rect = tk.Button(self.root,text = "Rectangle",\
                                  name="rect")
        self.btn_rect.pack(side = "left",padx=10)

        #create button for Eclipse
        self.btn_oval = tk.Button(self.root,text = "Eclipse",\
                                  name="oval")
        self.btn_oval.pack(side = "left",padx=10)

        #bind button class, button is clicked
        self.drawpad.bind_class("Button","<ButtonPress-1>",self.eventManager)
        self.drawpad.bind("<ButtonRelease-1>",self.stopdraw)

    def eventManager(self,event):
        name = event.widget.winfo_name()
        print(name)
        if name == "line":
            self.drawpad.bind("<B1-Motion>",self.drawline)
        elif name == "rect":
            self.drawpad.bind("<B1-Motion>",self.drawrect)
        elif name == "oval":
            self.drawpad.bind("<B1-Motion>",self.drawoval)
    
    def drawline(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,fill="white")

    def drawrect(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_rectangle(self.x,self.y,\
                                               event.x,event.y,outline="white")

    def drawoval(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_oval(self.x,self.y,\
                                               event.x,event.y,outline="white")

    def startdraw(self,event):
        self.drawpad.delete(self.drawID)
        if self.startDrawFlag == False:
            self.x = event.x
            self.y = event.y
            self.startDrawFlag = True
    
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 5

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 Pen Brush
        self.btn_pen = tk.Button(self.root,text = "Pen Brush",\
                                  name="pen")
        self.btn_pen.pack(side = "left",padx=10)
        
        #create button for Line
        self.btn_line = tk.Button(self.root,text = "Line",\
                                  name="line")
        self.btn_line.pack(side = "left",padx=10)

        #create button for arrowed Line
        self.btn_lineArrow = tk.Button(self.root,text = "Arrow Line",\
                                  name="lineArrow")
        self.btn_lineArrow.pack(side = "left",padx=10)
        
        #create button for Rectangle
        self.btn_rect = tk.Button(self.root,text = "Rectangle",\
                                  name="rect")
        self.btn_rect.pack(side = "left",padx=10)

        #create button for Eclipse
        self.btn_oval = tk.Button(self.root,text = "Eclipse",\
                                  name="oval")
        self.btn_oval.pack(side = "left",padx=10)

        self.drawpad.bind_class("Button","<ButtonPress-1>",self.eventManager)
        self.drawpad.bind("<ButtonRelease-1>",self.stopdraw)

    def eventManager(self,event):
        name = event.widget.winfo_name()
        print(name)
        if name == "pen":
            self.drawpad.bind("<B1-Motion>",self.mypen)
        elif name == "line":
            self.drawpad.bind("<B1-Motion>",self.drawline)
        elif name == "lineArrow":
            self.drawpad.bind("<B1-Motion>",self.drawlineArrow)
        elif name == "rect":
            self.drawpad.bind("<B1-Motion>",self.drawrect)
        elif name == "oval":
            self.drawpad.bind("<B1-Motion>",self.drawoval)

    def mypen(self,event):
        self.startdraw(event)
        self.drawpad.create_line(self.x,self.y,\
                                    event.x,event.y,fill="white")
        self.x = event.x
        self.y = event.y
    
    def drawline(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,fill="white")

    def drawlineArrow(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,arrow = "last",fill="white")

    def drawrect(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_rectangle(self.x,self.y,\
                                               event.x,event.y,outline="white")

    def drawoval(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_oval(self.x,self.y,\
                                               event.x,event.y,outline="white")

    def startdraw(self,event):
        self.drawpad.delete(self.drawID)
        if self.startDrawFlag == False:
            self.x = event.x
            self.y = event.y
            self.startDrawFlag = True
    
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

Exercise 6

import tkinter as tk
from tkinter import colorchooser

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.pen_color = "white"
        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 Pen Brush
        self.btn_pen = tk.Button(self.root,text = "Pen Brush",\
                                  name="pen")
        self.btn_pen.pack(side = "left",padx=10)
        
        #create button for Line
        self.btn_line = tk.Button(self.root,text = "Line",\
                                  name="line")
        self.btn_line.pack(side = "left",padx=10)

        #create button for arrowed Line
        self.btn_lineArrow = tk.Button(self.root,text = "Arrow Line",\
                                  name="lineArrow")
        self.btn_lineArrow.pack(side = "left",padx=10)
        
        #create button for Rectangle
        self.btn_rect = tk.Button(self.root,text = "Rectangle",\
                                  name="rect")
        self.btn_rect.pack(side = "left",padx=10)

        #create button for Eclipse
        self.btn_oval = tk.Button(self.root,text = "Eclipse",\
                                  name="oval")
        self.btn_oval.pack(side = "left",padx=10)

        #create button for eraser
        self.btn_erasor = tk.Button(self.root,text = "Eraser",\
                                  name="erasor")
        self.btn_erasor.pack(side = "left",padx=10)

        #create button for clear screen
        self.btn_clear = tk.Button(self.root,text = "Clear",\
                                  name="clear")
        self.btn_clear.pack(side = "left",padx=10)

        #create button for picking color
        self.btn_color = tk.Button(self.root,text = "Color",\
                                  name="color")
        self.btn_color.pack(side = "left",padx=10)

        self.drawpad.bind_class("Button","<ButtonPress-1>",self.eventManager)
        self.drawpad.bind("<ButtonRelease-1>",self.stopdraw)

    def eventManager(self,event):
        name = event.widget.winfo_name()
        print(name)
        if name == "pen":
            self.drawpad.bind("<B1-Motion>",self.mypen)
        elif name == "line":
            self.drawpad.bind("<B1-Motion>",self.drawline)
        elif name == "lineArrow":
            self.drawpad.bind("<B1-Motion>",self.drawlineArrow)
        elif name == "rect":
            self.drawpad.bind("<B1-Motion>",self.drawrect)
        elif name == "oval":
            self.drawpad.bind("<B1-Motion>",self.drawoval)
        elif name == "eraser":
            self.drawpad.bind("<B1-Motion>",self.myerasor)
        elif name == "clear":
            self.drawpad.delete("all")
        elif name == "color":
            c = tk.colorchooser.askcolor(color = "white",title="Choose Pen Brush Color")
            self.pen_color = c[1]
            

    def mypen(self,event):
        self.startdraw(event)
        self.drawpad.create_line(self.x,self.y,\
                                    event.x,event.y,fill=self.pen_color)
        self.x = event.x
        self.y = event.y
    
    def drawline(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                               event.x,event.y,fill=self.pen_color)

    def drawlineArrow(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_line(self.x,self.y,\
                                    event.x,event.y,arrow = "last",fill=self.pen_color)

    def drawrect(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_rectangle(self.x,self.y,\
                                               event.x,event.y,outline=self.pen_color)

    def drawoval(self,event):
        self.startdraw(event)
        self.drawID = self.drawpad.create_oval(self.x,self.y,\
                                               event.x,event.y,outline=self.pen_color)

    def myerasor(self,event):
        self.startdraw(event)
        self.drawpad.create_rectangle(self.x-4,self.y-4,\
                                        event.x+4,event.y+4,fill="black")
        self.x = event.x
        self.y = event.y

    def startdraw(self,event):
        self.drawpad.delete(self.drawID)
        if self.startDrawFlag == False:
            self.x = event.x
            self.y = event.y
            self.startDrawFlag = True
    
    def stopdraw(self,event):
        self.startDrawFlag = False
        self.drawID = -1
        
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

dahan1999

1 Comment

  1. […] |____Python Level 1| |____Unit 1 Hello World| |____Unit 2 Print| |____Unit 3 Magic Variable| |____Unit 4 Basic Operations| |____Unit 5 Data Types| |____Unit 6 Data Input| |____Unit 7 GUI design I| |____Unit 8 GUI design II|____Python Level 2| |____Unit 9 bool data type and relational operators| |____Unit 10 if statement| |____Unit 11 Logical Operators| |____Unit 12 Priority of Operators| |____Unit 13 if else statement| |____Unit 14 Multiple if Statements| |____Unit 15 Nested if Statements| |____Unit 16 Practice| |____Unit 17 Rock Paper Scissor Shoot I| |____Unit 18 Rock Paper Scissor Shoot II|____Python Level 3| |____Unit 19 while loop| |____Unit 20 while loop practice| |____Unit 21 guess number game| |____Unit 22 for loop| |____Unit 23 break and continue| |____Unit 24 nested loop| |____Unit 25 Pycharm and turtle| |____Unit 26 mimic banking system I| |____Unit 27 mimic banking system II| |____Unit 28 Perpetual Calendar I| |____Unit 29 Perpetual Calendar II|____Python Level 4| |____Unit 30 First Glance at List| |____Unit 31 More Operations on Lists| |____Unit 32 Common Algorithms for Lists| |____Unit 33 Bubble Sort and Selection Sort| |____Unit 34 Multidimensional Lists| |____Unit 35 Student Performance Management System (Part 1)| |____Unit 36 Student performance management system (Part 2)| |____Unit 37 Student Information Management System (Part 1)| |____Unit 38 Student Information Management System (Part 2)| |____Unit 39 Tuple| |____Unit 40 Dictionaries| |____Unit 41 English-Chinese Dictionary| |____Unit 42 String Operations|____Python Level 5| |____Unit 43 Glance at Functions| |____Unit 44 Advanced functions| |____Unit 45 Use of functions| |____Unit 46 Object-oriented basics| |____Unit 47 Object-oriented Example Exercises| |____Unit 48 Student Information Management System| |____Unit 49 Mall Shopping Management System (Part 1)| |____Unit 50 Mall Shopping Management System (Part 2)| |____Unit 51 Object-oriented advanced| |____Unit 52 Library Management System (Part 1)| |____Unit 53 Library Management System (Part 2)| |____Unit 54 Object Oriented Advanced| |____Unit 55 Modules|____Python Level 6| |____Unit 56 Windows and Basic Graphics| |____Unit 57 Graphics and Basic Animation| |____Unit 58 Animation Design| |____Unit 59 Animated Sprites| |____Unit 60 Billiards Mini Game (Part 1)| |____Unit 61 Billiards Mini Game (Part 2)| |____Unit 62 Event Input| |____Unit 63 Table Tennis Game (Part 1)| |____Unit 64 Table Tennis Game (Part 2)| |____Unit 65 Airplane Game (Part 1)| |____Unit 66 Airplane Game (Part 2)| |____Unit 67 Pygame Sounds| |____Unit 68 Air Fight (Part 1)| |____Unit 69 Air Fight (Part 2)| |____Unit 70 Air Fight (Part 3)|____Python Level 7| |____Unit 71 exception| |____Unit 72 documents| |____Unit 73 First Glance at tkinter| |____Unit 74 tkinter common components| |____Unit 75 Layout Manager| |____Unit 76 GUI interface example exercises| |____Unit 77 event handling| |____Unit 78 Remainder Gadget| |____Unit 79 tkinter version guessing number game| |____Unit 80 tkinter more components| |____Unit 81 Simple Notepad (Part 1)| |____Unit 82 Simple Notepad (Part 2)| |____Unit 83 Simple drawing tools (Part 1)| |____Unit 84 Simple drawing tools (Part 2) […]

Leave a Reply

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

Related Posts