python tkinter フォント(font)の設定方法
python tkinter フォント(font)の設定方法
tkinterのフォントの設定について整理する。
tkinterでは、フォントタイプ、サイズ、太字、斜体、取消線の設定が可能だ。
参考ページ(英語):http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html
python tkinter 色(color)の設定方法
python tkinter 色(color)の設定方法
python tkinterにおける色(color)の設定方法がいくつかあるので整理する。
参考ページ(英語):http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/colors.html
python クラスの定義と継承とは
python クラスの定義と継承とは
前回の記事は、tkinterのドキュメントのサンプルスクリプトとして記載されている"A Simple Hello World Program"が難しすぎる。という記事を投稿した。
前の記事
python tkinterのクラス化手法によるGUI作成 - memopy
今回は、pythonのクラスとはなにか。クラスの継承とはなにかについてまとめたいと思う。
特に、クラスの継承はpython2とpython3で方法が一部異なるため、この点を整理したい。
python tkinterのクラス化手法によるGUI作成
python tkinterのドキュメント"A Simple Hello World Program"が難しすぎる
皆さんはpython tkinterのドキュメントをご覧になったことがあるだろうか。
※python3の場合
25.1. tkinter — Python interface to Tcl/Tk — Python 3.6.1 documentation
※python2の場合
24.1. Tkinter — Python interface to Tcl/Tk — Python 2.7.13 documentation
A Simple Hello World Program
この中にA Simple Hello World Programというサンプルスクリプトがある。
しかし、このサンプルスクリプトはpython初心者にとって非常に難しい。
※python3の場合
import tkinter as tk class Application(tk.Frame): def __init__(self, master=None): super().__init__(master) self.pack() self.create_widgets() def create_widgets(self): self.hi_there = tk.Button(self) self.hi_there["text"] = "Hello World\n(click me)" self.hi_there["command"] = self.say_hi self.hi_there.pack(side="top") self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy) self.quit.pack(side="bottom") def say_hi(self): print("hi there, everyone!") root = tk.Tk() app = Application(master=root) app.mainloop()
※python2の場合
from Tkinter import * class Application(Frame): def say_hi(self): print "hi there, everyone!" def createWidgets(self): self.QUIT = Button(self) self.QUIT["text"] = "QUIT" self.QUIT["fg"] = "red" self.QUIT["command"] = self.quit self.QUIT.pack({"side": "left"}) self.hi_there = Button(self) self.hi_there["text"] = "Hello", self.hi_there["command"] = self.say_hi self.hi_there.pack({"side": "left"}) def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.createWidgets() root = Tk() app = Application(master=root) app.mainloop() root.destroy()
いずれにせよ、このスタンドアロンスクリプトを実行すると、次のGUIが作成され、ボタンをクリックすると、インタプリタコンソールにhi there, everyone!と、いかにも英語圏の挨拶が表示されるプログラムとなっている。
このサンプルスクリプトは、A Simple Hello World Programという位置付けでありながら、次の点で初心者にとっては非常に難しいものである。
また次のような遠因も輪をかけてこのA Simple Hello World Programを難しくしている。
- そもそも、tkinterの日本語ドキュメントや文献が少ない
- 文献によって、手続き型で記述しているものと、オブジェクト指向によって記述しているものがあり、両者を混在させて参考にできない
私もそうであったが、最初は、インターネットや書籍に公開されているスクリプトをコピーして、自分のパソコンで動かすであろう。
そして、そのあと、自分なりにいろいろ変更を加えてみて、GUIがどのように変化するのか学習するものである。
しかし、pythonはご存知のとおり、日本語のドキュメントが少ないし、さらにtkinterはもっと日本語ドキュメントが少ない。そして、python2系と3系では、クラス化の手法がかなり異なるため、文献によっては自分の環境では動作せず行き詰る。
私もこのtkinterは特に行き詰ったため、その点を整理したいと思う。
⑫ 画面遷移の処理を定義(最終回)【python tkinter sqlite3で家計簿を作る】
⑫ 画面遷移の処理を定義(最終回)【python tkinter sqlite3で家計簿を作る】
家計簿アプリもほとんど完成に近づいてきた。
今回は各画面の遷移の処理を実装する。
メインスクリプトに対して、2つの画面を表示する関数し、その関数の中にさらに各ボタンが押下されたときのコールバック関数を定義している。
関数の状況を概観すると次のスクリプトになる。
# -*- coding:utf-8 -*- def create_gui(): "登録画面を表示する関数" def select_button(): "表示ボタンが押下されたときに表示画面を表示する" pass def qui_button(): "終了ボタンが押下されたときにアプリを終了する" pass def qui_button(): "登録ボタンが押下されたときにデータをDBに登録する" pass # ここに登録画面のGUI部分の定義 pass def select_gui(): "表示画面を表示する関数" def create_button(): "登録ボタンが押下されたときに表示画面を表示する" pass def qui_button(): "終了ボタンが押下されたときにアプリを終了する" pass def select_button(): "表示ボタンが押下されたときに表にデータを表示する" pass # ここに表示画面のGUI部分の定義 pass # このスクリプトが実行されたときに最初に登録画面を表示する create_gui()
それぞれ、passと記載した箇所に、今までに作成したコードを転記していく。