VBA 使えるソースコード

つたないソースコードを載せます。これは、他人の書いたソースコードを読む練習に最適です。初心者の方は、どうしたらきれいになるかなど考えながら活用してください。

Python お絵描きしてみよう2

from Circle import Circle
from Rectangle import Rectangle
from Line import Line
import tkinter
import random

class AddShapePanelv4:

def __init__(self):
print("AddShapePanelv4オブジェクトの作成")
self.create_window()
self.listOfObject=[]
self.IDforCircleRed = 1
self.IDforRectangleBlue = 2
self.IDforLineGreen = 3

def create_window(self):
self.window = tkinter.Tk()
self.canvas = tkinter.Canvas(self.window, bg = "white", width = 320, height = 240)
self.canvas.pack()

circle_button = tkinter.Button(self.window, text = "NewCircle1", command = lambda:self.createShape(self.IDforCircleRed))
circle_button.pack(side = tkinter.LEFT)

retangle_button = tkinter.Button(self.window, text = "NewRetangle1", command = lambda:self.createShape(self.IDforRectangleBlue))
retangle_button.pack(side = tkinter.LEFT)

line_button = tkinter.Button(self.window, text = "Newline1", command = lambda:self.createShape(self.IDforLineGreen))
line_button.pack(side = tkinter.LEFT)


def createShape(self, drawID):
print("createShapeメソッドの実行")
x = random.randint(0,320)
y = random.randint(0,240)
wid = random.randint(0,90) + 10
len = random.randint(0,90) + 10

if(drawID == self.IDforCircleRed):
color ="Red"
self.listOfObject.append(Circle(x,y,wid,len,color))
elif(drawID == self.IDforRectangleBlue):
color = "Blue"
self.listOfObject.append(Rectangle(x,y,wid,len,color))
elif(drawID == self.IDforLineGreen):
color = "Green"
self.listOfObject.append(Line(x,y,wid,len,color))
self.repaint()

def repaint(self):
print("repaintメソッドの実行:画面をクリアしてからpaintメソッドを呼び出す")


self.canvas.delete("all")
self.paint()

def paint(self):
print("paintメソッドの実行")
for drawObject in self.listOfObject:
drawObject.draw(self.canvas)

def run(self):
self.window.mainloop()

AddShapePanelv4().run()

f:id:yoronx:20200302135709p:plain

 

必要なクラスはここから!

 

yoronx.hatenablog.com