Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.