SlideShare a Scribd company logo
tkinter の話
2020-09-18
西本 卓也(株式会社シュアルタ)
@nishimotz / @24motz
1
おまえ誰よ
• 京都(1996年~2002年)→ 現在は広島
• NVDA日本語チーム www.nvda.jp
• PyCon mini Hiroshima → 2020年10月10日オンライン開催
• PyCon JP 2020 でコミュニティの話をした
• すごい広島 with Pythonで「標準ライブラリなLT会」を紹介された
• ソース
• tkinterの話題
• https://gist.github.com/nishimotz/ca2d5eac2002b908a7da3aaa2f162f48
• まわり将棋
• https://gist.github.com/nishimotz/67b1e1cd245eadb2b2597db650a8c772
2
動機
• ゲームセンターあらしと学ぶ プログラミング入門
• 知り合いの息子さんと2回ほど読書会
• この本のアプローチ
• 公式 Python
• エディタは IDLE
• 標準ライブラリだけでスカッシュゲーム
• tkinter
• 扱わないこと
• クラス定義
• データ構造
3
4
Python で予約語の一覧を出す方法
• 51ページに import keyword が紹介されている
5
tkinter
• https://docs.python.org/ja/3/library/tkinter.html
• Tcl/Tk の Tk
• Tk の仕様書にたらいまわしにされる
6
tkinter/__init__.py より
7
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame,text="Exit",command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()
8
9
10
多重継承による実装
• Fluent Python 第1版 第12章 継承の功罪
• 「標準ライブラリで多重継承を極端に用いている例」として紹介
11
>>> import tkinter
>>> def print_mro(cls):
... print(', '.join(c.__name__ for c in cls.__mro__))
...
>>> print_mro(tkinter.Toplevel)
Toplevel, BaseWidget, Misc, Wm, object
>>> print_mro(tkinter.Canvas)
Canvas, Widget, BaseWidget, Misc, Pack, Place, Grid, XView, YView, object
>>>
使い方(公式)
12
import tkinter as tk
class Application(tk.Frame):
(中略)
root = tk.Tk()
app = Application(master=root)
app.mainloop()
canvas が便利
13
def create_widgets(self):
self.cv = tk.Canvas(self, width=440, height=440)
# omitted
self.cv.create_text(60, 60, text="歩", tag="player")
self.cv.pack(side="top")
def set_player_pos(self, x, y):
self.cv.coords("player", x, y)
def set_player_text(self, text):
self.cv.itemconfigure("player", text=text)
def button_pressed(self):
# omitted
self.set_player_text("香")
ttk = テーマ付きウィジェット
• https://docs.python.org/ja/3/library/tkinter.ttk.html
14
>>> from tkinter import ttk
>>> import tkinter
>>> root = tkinter.Tk()
>>> ttk.Style().configure("TButton", padding=6, relief="flat", background="#ccc")
>>> btn = ttk.Button(text="Hello")
>>> btn.pack()
これから
• 「こんにちはPython」で勉強した人に...
• さらに tkinter を深掘りしてもらう?
• 割り切りが必要
• 別の GUI ツールキット?
• レトロゲームエンジン Pyxel
• https://github.com/kitao/pyxel/blob/master/README.ja.md
15

More Related Content

PDF
Steins;Gitについて
PDF
技術系同人誌を作ってコミケで頒布しよう ―― Steins;Git の実例
PPTX
kintone Café 名古屋 Vol.3 「寂しくないKintone」
PDF
Gitを使いこなしてみよう!
PPTX
【kintone Cafe 名古屋】kintone×○○○の可能性
PDF
新しい世界の学び方 by @masaru_b_cl #nds55
PPTX
221217 SwiftはPythonに似ている
PPTX
220427-pydata 統計・データ分析 特集
Steins;Gitについて
技術系同人誌を作ってコミケで頒布しよう ―― Steins;Git の実例
kintone Café 名古屋 Vol.3 「寂しくないKintone」
Gitを使いこなしてみよう!
【kintone Cafe 名古屋】kintone×○○○の可能性
新しい世界の学び方 by @masaru_b_cl #nds55
221217 SwiftはPythonに似ている
220427-pydata 統計・データ分析 特集

More from Takuya Nishimoto (20)

PPTX
220126 python-datalake-spark
PPTX
211120 他人の書いたPythonスクリプトをステップ実行で理解する
PPTX
211020 すごい広島 with OSH 2021.10
PPTX
210917 オープンセミナー@広島のこれまでとこれから
PPTX
210911 これから始める電子工作とMicroPython
PPTX
210728 mpy
PPTX
210630 python
PPTX
210526 Power Automate Desktop Python
PPTX
210428 python
PPTX
200429 python
PPTX
200325 flask
PPTX
200208 osh-nishimoto-v2
PPTX
191208 python-kansai-nishimoto
PPTX
191101 nvda-sightworld-nishimoto
PPTX
191114 iotlt-nishimoto
PPTX
191030 anna-with-python
PPTX
190916 nishimoto-nvda-pyconjp
PPTX
190925 python-windows
PPTX
190731 chalice
PPTX
190719 nishimoto nvda talk
220126 python-datalake-spark
211120 他人の書いたPythonスクリプトをステップ実行で理解する
211020 すごい広島 with OSH 2021.10
210917 オープンセミナー@広島のこれまでとこれから
210911 これから始める電子工作とMicroPython
210728 mpy
210630 python
210526 Power Automate Desktop Python
210428 python
200429 python
200325 flask
200208 osh-nishimoto-v2
191208 python-kansai-nishimoto
191101 nvda-sightworld-nishimoto
191114 iotlt-nishimoto
191030 anna-with-python
190916 nishimoto-nvda-pyconjp
190925 python-windows
190731 chalice
190719 nishimoto nvda talk
Ad

200918 hannari-python