2019年5月31日金曜日

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

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



#coding:utf-8

import tkinter as tk


def click(event):
    if editbox1.get() == '':
        pass
    else:
        set_size = int(editbox1.get())
        canvas.create_oval(event.x - set_size, event.y - set_size, event.x + set_size, event.y + set_size, fill = set_color, width = 0)

def click2(event):
    if editbox1.get() == '':
        pass
    else:
        set_size = int(editbox1.get())
        canvas.create_rectangle(event.x - set_size, event.y - set_size, event.x + set_size, event.y + set_size, 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)
label1 = tk.Label(root,text = 'サイズを入力', font = ('Courier',14))
editbox1 = tk.Entry(width = 4, font = ('Courier',14))

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)
label1.place(x = 620, y = 120)
editbox1.place(x = 650, y = 150)

canvas.place(x = 0, y = 0)

set_color = 'red'

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

root.mainloop()


メモ

・丸や四角のサイズを変更できるようにしました。
・サイズを入力していない間は、何も描かれないようにしました。

0 件のコメント:

コメントを投稿