since 2018-12-27
Python 3.7.2 で「お絵描きソフト」を作る準備。
ウィンドウを開いて、マウスで左ボタンを押してドラッグしている間は輪郭が表示されて、ボタンを離すと長方形を塗りつぶす。
import tkinter canvas = None begin_x = begin_y = None rect = None def on_button_press(ev): global begin_x, begin_y begin_x = ev.x begin_y = ev.y def on_button_motion(ev): global rect if rect: canvas.delete(rect) rect = canvas.create_rectangle(begin_x, begin_y, ev.x, ev.y) def on_button_release(ev): global begin_x, begin_y canvas.create_rectangle(begin_x, begin_y, ev.x, ev.y, fill='green') begin_x = begin_y = None def main(): global canvas root = tkinter.Tk() root.title('hello tkinter') root.geometry('800x450') canvas = tkinter.Canvas(root) canvas.pack(expand=1, fill='both') root.bind('<Button-1>', on_button_press) root.bind('<B1-Motion>', on_button_motion) root.bind('<ButtonRelease-1>', on_button_release) root.mainloop() if __name__ == '__main__': main()
since 2020-07-29
since 2020-07-29
since 2020-07-29