VBA 使えるソースコード

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

Python お絵描きしてみよう1

from SimpleCircle import SimpleCircle
import tkinter
import random

class AddShapePanel:

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

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 = "NewCircle2", command = lambda:self.createShape(self.IDforCircleBlue))
retangle_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(SimpleCircle(x,y,wid,len,color))
elif(drawID == self.IDforCircleBlue):
color = "Blue"
self.listOfObject.append(SimpleCircle(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()

AddShapePanel().run()

 

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

 

yoronx.hatenablog.com