開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.7 (プログラミング言語)
Head First はじめてのプログラミング ―頭とからだで覚えるPythonプログラミング入門 (Eric Freeman(著)、嶋田 健志(監修)、木下 哲也(翻訳)、株式会社オライリー・ジャパン)を11章(ウィジェット、イベント、創発的な振る舞い - インタラクティブにする)のエクササイズ(503ページ)の解答を求めてみる。
コード
Python 3
#!/usr/bin/env python3
from tkinter import *
import model
root: Tk = Tk()
start_button: Button = Button(root, text='Start', width=12)
clear_button: Button = Button(root, text='Clear', width=12)
start_button.pack()
clear_button.pack()
is_running: bool = False
def update() -> None:
print('update')
pass
def start_handler(event) -> None:
print('start_handler')
global is_running, start_button
if is_running:
is_running = False
start_button.configure(text='Start')
else:
is_running = True
start_button.configure(text='Pause')
update()
def clear_handler(event) -> None:
print('clear_handler')
global is_running, start_button
is_running = False
model.grid_model = [[0 for _ in range(model.width)]
for _ in range(model.height)]
start_button.configure(text='Start')
update()
start_button.bind('<Button-1>', start_handler)
clear_button.bind('<Button-1>', clear_handler)
if __name__ == '__main__':
mainloop()
入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))
$ ./views.py start_handler update start_handler clear_handler update start_handler update clear_handler update $
0 コメント:
コメントを投稿