SlideShare a Scribd company logo
1 of 15
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

Similar to 200918 hannari-python

ギークを目指すエンジニャーの 情報収集方法 mohikan Slack
ギークを目指すエンジニャーの 情報収集方法 mohikan Slackギークを目指すエンジニャーの 情報収集方法 mohikan Slack
ギークを目指すエンジニャーの 情報収集方法 mohikan SlackKota Kanbe
 
PyScriptの紹介
PyScriptの紹介PyScriptの紹介
PyScriptの紹介2bo 2bo
 
211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解する211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解するTakuya Nishimoto
 
WebSig冬期講習2020 開校資料
WebSig冬期講習2020 開校資料WebSig冬期講習2020 開校資料
WebSig冬期講習2020 開校資料WebSig24/7
 
Google Home をつかって おじいちゃんにGoogle Home の使い方を教える
Google Home をつかって おじいちゃんにGoogle Home の使い方を教えるGoogle Home をつかって おじいちゃんにGoogle Home の使い方を教える
Google Home をつかって おじいちゃんにGoogle Home の使い方を教えるTomoyuki Sugita
 
イベント企画運営の経験と実際 / The history of organizing events by me
イベント企画運営の経験と実際 / The history of organizing events by meイベント企画運営の経験と実際 / The history of organizing events by me
イベント企画運営の経験と実際 / The history of organizing events by mewhywaita
 
Py datameetup1
Py datameetup1Py datameetup1
Py datameetup1shiroyagi
 
171209 nishimoto-mpy-esp32
171209 nishimoto-mpy-esp32171209 nishimoto-mpy-esp32
171209 nishimoto-mpy-esp32Takuya Nishimoto
 
CoderDojo五日市 OSChiroshima2017
CoderDojo五日市 OSChiroshima2017CoderDojo五日市 OSChiroshima2017
CoderDojo五日市 OSChiroshima2017朋紀 髙橋
 
東方ゲームAIとその歴史
東方ゲームAIとその歴史東方ゲームAIとその歴史
東方ゲームAIとその歴史ide_an
 
Rubyだけでチンチロをつくる
RubyだけでチンチロをつくるRubyだけでチンチロをつくる
RubyだけでチンチロをつくるK K
 
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけ
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけPythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけ
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけLina Katayose
 
第一回テクニカルアーティストラウンドテーブル・セッション
第一回テクニカルアーティストラウンドテーブル・セッション第一回テクニカルアーティストラウンドテーブル・セッション
第一回テクニカルアーティストラウンドテーブル・セッションfumoto kazuhiro
 
Pythonの10年と今、これから
Pythonの10年と今、これからPythonの10年と今、これから
Pythonの10年と今、これからHaruo Sato
 
Python for Beginners ( #PyLadiesKyoto Meetup )
Python for Beginners ( #PyLadiesKyoto Meetup )Python for Beginners ( #PyLadiesKyoto Meetup )
Python for Beginners ( #PyLadiesKyoto Meetup )Ai Makabi
 
Git for Begineers GitHub ハンズオン
Git for Begineers GitHub ハンズオンGit for Begineers GitHub ハンズオン
Git for Begineers GitHub ハンズオンEmma Haruka Iwao
 
YAPCレポートの舞台裏
YAPCレポートの舞台裏YAPCレポートの舞台裏
YAPCレポートの舞台裏Masahiro Honma
 

Similar to 200918 hannari-python (20)

ギークを目指すエンジニャーの 情報収集方法 mohikan Slack
ギークを目指すエンジニャーの 情報収集方法 mohikan Slackギークを目指すエンジニャーの 情報収集方法 mohikan Slack
ギークを目指すエンジニャーの 情報収集方法 mohikan Slack
 
ieLT in 2017 fall
ieLT in 2017 fall ieLT in 2017 fall
ieLT in 2017 fall
 
PyScriptの紹介
PyScriptの紹介PyScriptの紹介
PyScriptの紹介
 
181006 nishimoto-kaggle
181006 nishimoto-kaggle181006 nishimoto-kaggle
181006 nishimoto-kaggle
 
200208 osh-nishimoto-v2
200208 osh-nishimoto-v2200208 osh-nishimoto-v2
200208 osh-nishimoto-v2
 
211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解する211120 他人の書いたPythonスクリプトをステップ実行で理解する
211120 他人の書いたPythonスクリプトをステップ実行で理解する
 
WebSig冬期講習2020 開校資料
WebSig冬期講習2020 開校資料WebSig冬期講習2020 開校資料
WebSig冬期講習2020 開校資料
 
Google Home をつかって おじいちゃんにGoogle Home の使い方を教える
Google Home をつかって おじいちゃんにGoogle Home の使い方を教えるGoogle Home をつかって おじいちゃんにGoogle Home の使い方を教える
Google Home をつかって おじいちゃんにGoogle Home の使い方を教える
 
イベント企画運営の経験と実際 / The history of organizing events by me
イベント企画運営の経験と実際 / The history of organizing events by meイベント企画運営の経験と実際 / The history of organizing events by me
イベント企画運営の経験と実際 / The history of organizing events by me
 
Py datameetup1
Py datameetup1Py datameetup1
Py datameetup1
 
171209 nishimoto-mpy-esp32
171209 nishimoto-mpy-esp32171209 nishimoto-mpy-esp32
171209 nishimoto-mpy-esp32
 
CoderDojo五日市 OSChiroshima2017
CoderDojo五日市 OSChiroshima2017CoderDojo五日市 OSChiroshima2017
CoderDojo五日市 OSChiroshima2017
 
東方ゲームAIとその歴史
東方ゲームAIとその歴史東方ゲームAIとその歴史
東方ゲームAIとその歴史
 
Rubyだけでチンチロをつくる
RubyだけでチンチロをつくるRubyだけでチンチロをつくる
Rubyだけでチンチロをつくる
 
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけ
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけPythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけ
Pythonで ハードウェアを動かす楽しさと ハードウェアハック始めたきっかけ
 
第一回テクニカルアーティストラウンドテーブル・セッション
第一回テクニカルアーティストラウンドテーブル・セッション第一回テクニカルアーティストラウンドテーブル・セッション
第一回テクニカルアーティストラウンドテーブル・セッション
 
Pythonの10年と今、これから
Pythonの10年と今、これからPythonの10年と今、これから
Pythonの10年と今、これから
 
Python for Beginners ( #PyLadiesKyoto Meetup )
Python for Beginners ( #PyLadiesKyoto Meetup )Python for Beginners ( #PyLadiesKyoto Meetup )
Python for Beginners ( #PyLadiesKyoto Meetup )
 
Git for Begineers GitHub ハンズオン
Git for Begineers GitHub ハンズオンGit for Begineers GitHub ハンズオン
Git for Begineers GitHub ハンズオン
 
YAPCレポートの舞台裏
YAPCレポートの舞台裏YAPCレポートの舞台裏
YAPCレポートの舞台裏
 

More from Takuya Nishimoto

221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ている221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ているTakuya Nishimoto
 
220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集Takuya Nishimoto
 
220126 python-datalake-spark
220126 python-datalake-spark220126 python-datalake-spark
220126 python-datalake-sparkTakuya Nishimoto
 
211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10Takuya Nishimoto
 
210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれから210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれからTakuya Nishimoto
 
210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPython210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPythonTakuya Nishimoto
 
210526 Power Automate Desktop Python
210526 Power Automate Desktop Python210526 Power Automate Desktop Python
210526 Power Automate Desktop PythonTakuya Nishimoto
 
191208 python-kansai-nishimoto
191208 python-kansai-nishimoto191208 python-kansai-nishimoto
191208 python-kansai-nishimotoTakuya Nishimoto
 
191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimoto191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimotoTakuya Nishimoto
 
190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjp190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjpTakuya Nishimoto
 
190719 nishimoto nvda talk
190719 nishimoto nvda talk190719 nishimoto nvda talk
190719 nishimoto nvda talkTakuya Nishimoto
 

More from Takuya Nishimoto (20)

221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ている221217 SwiftはPythonに似ている
221217 SwiftはPythonに似ている
 
220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集220427-pydata 統計・データ分析 特集
220427-pydata 統計・データ分析 特集
 
220126 python-datalake-spark
220126 python-datalake-spark220126 python-datalake-spark
220126 python-datalake-spark
 
211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10211020 すごい広島 with OSH 2021.10
211020 すごい広島 with OSH 2021.10
 
210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれから210917 オープンセミナー@広島のこれまでとこれから
210917 オープンセミナー@広島のこれまでとこれから
 
210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPython210911 これから始める電子工作とMicroPython
210911 これから始める電子工作とMicroPython
 
210728 mpy
210728 mpy210728 mpy
210728 mpy
 
210630 python
210630 python210630 python
210630 python
 
210526 Power Automate Desktop Python
210526 Power Automate Desktop Python210526 Power Automate Desktop Python
210526 Power Automate Desktop Python
 
210428 python
210428 python210428 python
210428 python
 
200429 python
200429 python200429 python
200429 python
 
200325 flask
200325 flask200325 flask
200325 flask
 
191208 python-kansai-nishimoto
191208 python-kansai-nishimoto191208 python-kansai-nishimoto
191208 python-kansai-nishimoto
 
191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimoto191101 nvda-sightworld-nishimoto
191101 nvda-sightworld-nishimoto
 
191114 iotlt-nishimoto
191114 iotlt-nishimoto191114 iotlt-nishimoto
191114 iotlt-nishimoto
 
191030 anna-with-python
191030 anna-with-python191030 anna-with-python
191030 anna-with-python
 
190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjp190916 nishimoto-nvda-pyconjp
190916 nishimoto-nvda-pyconjp
 
190925 python-windows
190925 python-windows190925 python-windows
190925 python-windows
 
190731 chalice
190731 chalice190731 chalice
190731 chalice
 
190719 nishimoto nvda talk
190719 nishimoto nvda talk190719 nishimoto nvda talk
190719 nishimoto nvda talk
 

200918 hannari-python