Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Last active July 29, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishimotz/ca2d5eac2002b908a7da3aaa2f162f48 to your computer and use it in GitHub Desktop.
Save nishimotz/ca2d5eac2002b908a7da3aaa2f162f48 to your computer and use it in GitHub Desktop.
200729-tkinter

tkinter

「お絵描きソフト」を作る

以前やりかけた「お絵描きソフト」を作る準備。

ウィンドウを開いて、マウスで左ボタンを押してドラッグしている間は輪郭が表示されて、ボタンを離すと長方形を塗りつぶす。

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()

ゲームを作る

非同期処理

Thread

多重継承

  • Fluent Python (日本語版)377ページあたり
  • 20年前の設計という評価
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment