2019年5月30日木曜日

クリックしたところに円と四角が描かれるプログラム その4

クリックしたところに円と四角が描かれるプログラム その4です。



#coding:utf-8

import tkinter as tk


def click(event):
    canvas.create_oval(event.x - 20, event.y -20, event.x +20, event.y +20, fill = set_color, width = 0)

def click2(event):
    canvas.create_rectangle(event.x - 20, event.y -20, event.x +20, event.y +20, fill = set_color, width = 0)

def reset():
    canvas.create_rectangle(0, 0, 600, 400, fill = 'white', width = 0)

def blue():
    global set_color
    set_color = 'blue'

def red():
    global set_color
    set_color = 'red'

def green():
    global set_color
    set_color = 'green'

def white():
    global set_color
    set_color = 'white'

def yellow():
    global set_color
    set_color = 'yellow'

def black():
    global set_color
    set_color = 'black'

root = tk.Tk()
root.geometry('900x400')
root.title('クリックすると円と四角が描かれる')

button1 = tk.Button(root,text = 'リセット', font = ('Courier',14),command = reset)
b_blue = tk.Button(root,text = ' 青 ', font = ('Courier',14),command = blue)
b_red = tk.Button(root,text = ' 赤 ', font = ('Courier',14),command = red)
b_green = tk.Button(root,text = ' 緑 ', font = ('Courier',14),command = green)
b_white = tk.Button(root,text = ' 白 ', font = ('Courier',14),command = white)
b_yellow = tk.Button(root,text = ' 黄 ', font = ('Courier',14),command = yellow)
b_black = tk.Button(root,text = ' 黒 ', font = ('Courier',14),command = black)
canvas = tk.Canvas(root, width = 600, height = 400, bg = 'white')

button1.place(x = 700, y = 350)
b_blue.place(x = 620, y = 20)
b_red.place(x = 710, y = 20)
b_green.place(x = 800, y = 20)
b_white.place(x = 620, y = 70)
b_yellow.place(x = 710, y = 70)
b_black.place(x = 800, y = 70)
canvas.place(x = 0, y = 0)

set_color = 'red'

canvas.bind('<Button-1>', click)
canvas.bind('<Button-3>', click2)

root.mainloop()


メモ

・色を選択できるようにしました。

0 件のコメント:

コメントを投稿