以前やりかけた「お絵描きソフト」を作る準備。
ウィンドウを開いて、マウスで左ボタンを押してドラッグしている間は輪郭が表示されて、ボタンを離すと長方形を塗りつぶす。
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()
- https://d.nishimotz.com/archives/2332
- https://docs.python.org/ja/3/library/tkinter.html
- https://gist.github.com/nishimotz/67b1e1cd245eadb2b2597db650a8c772
- https://pyvideo.org/pycon-us-2013/using-futures-for-async-gui-programming-in-python.html
- https://github.com/fluentpython/asyncio-tkinter
- Fluent Python (日本語版)18章の最後
- https://qiita.com/gotta_dive_into_python/items/973cb088e673949c5f1a
- https://campkougaku.com/2019/12/31/gui-thread/
- https://karupoimou.hatenablog.com/entry/20200427/1587975580
- https://www.haya-programming.com/entry/2019/01/20/012926
- Fluent Python (日本語版)377ページあたり
- 20年前の設計という評価