Python GUIプログラミング
Pythonは、グラフィカルユーザーインターフェイス(GUI)を開発するためのさまざまなオプションを提供します。最も重要なものは以下のとおりです。
- Tkinter: Tkinterは、Pythonに同梱されているTk GUIツールキットへのPythonインターフェイスです。この章ではこのオプションを検討します。
- wxPython:これは、wxWindows http://wxpython.org用のオープンソースのPythonインターフェイスです。
- JPython: JPythonは、Pythonスクリプトにローカルマシンhttp://www.jython.orgのJavaクラスライブラリへのアクセスを提供するPythonのJava用ポートです。
利用可能な他の多くのインタフェースがあり、ネット上でそれらを見つけることができます。
Tkinterプログラミング
TkinterはPython用の標準GUIライブラリです。PythonをTkinterと組み合わせると、GUIアプリケーションを素早く簡単に作成できます。Tkinterは、Tk GUIツールキットに対する強力なオブジェクト指向のインターフェースを提供します。
Tkinterを使ってGUIアプリケーションを作成するのは非常に簡単です。
- Tkinterモジュールをインポートします。
- GUIアプリケーションのメインウィンドウを作成します。
- 上記のウィジェットの1つ以上をGUIアプリケーションに追加します。
- メインイベントループを入力して、ユーザーによってトリガーされた各イベントに対してアクションを実行します。
例
#!/usr/bin/python import tkinter top = tkinter.Tk() # Code to add widgets will go here... top.mainloop()
これにより、次のウィンドウが作成されます。
Tkinterウィジェット
Tkinterは、GUIアプリケーションで使用されるボタン、ラベル、テキストボックスなどのさまざまなコントロールを提供します。これらのコントロールは一般的にウィジェットと呼ばれます。
Tkinterには現在15種類のウィジェットがあります。次の表にこれらのウィジェットと簡単な説明を示します。
オペレーター | 説明 |
---|---|
ボタン | ボタンウィジェットは、アプリケーションにボタンを表示するために使用されます。
import Tkinter import tkMessageBox top = Tkinter.Tk() def helloCallBack(): tkMessageBox.showinfo( "Hello Python", "Hello World") B = Tkinter.Button(top, text ="Hello", command = helloCallBack) B.pack() top.mainloop() |
キャンバス | キャンバスウィジェットは、アプリケーション内に線、楕円、多角形、四角形などの図形を描画するために使用されます
import Tkinter import tkMessageBox top = Tkinter.Tk() C = Tkinter.Canvas(top, bg="blue", height=250, width=300) coord = 10, 50, 240, 210 arc = C.create_arc(coord, start=0, extent=150, fill="red") C.pack() top.mainloop() |
チェックボタン | Checkbuttonウィジェットは、いくつかのオプションをチェックボックスとして表示するために使用されます。ユーザーは一度に複数のオプションを選択できます。
from Tkinter import * import tkMessageBox import Tkinter top = Tkinter.Tk() CheckVar1 = IntVar() CheckVar2 = IntVar() C1 = Checkbutton(top, text = "Music", variable = CheckVar1, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) C2 = Checkbutton(top, text = "Video", variable = CheckVar2, \ onvalue = 1, offvalue = 0, height=5, \ width = 20) C1.pack() C2.pack() top.mainloop() |
エントリ | Entryウィジェットは、ユーザーからの値を受け入れるための1行のテキストフィールドを表示するために使用されます。
from Tkinter import * top = Tk() L1 = Label(top, text="User Name") L1.pack( side = LEFT) E1 = Entry(top, bd =5) E1.pack(side = RIGHT) top.mainloop() |
フレーム | フレームウィジェットは、他のウィジェットを整理するためのコンテナウィジェットとして使用されます。
from Tkinter import * root = Tk() frame = Frame(root) frame.pack() bottomframe = Frame(root) bottomframe.pack( side = BOTTOM ) redbutton = Button(frame, text="Red", fg="red") redbutton.pack( side = LEFT) greenbutton = Button(frame, text="Brown", fg="brown") greenbutton.pack( side = LEFT ) bluebutton = Button(frame, text="Blue", fg="blue") bluebutton.pack( side = LEFT ) blackbutton = Button(bottomframe, text="Black", fg="black") blackbutton.pack( side = BOTTOM) root.mainloop() |
ラベル | Labelウィジェットは、他のウィジェットに対して単一行のキャプションを提供するために使用されます。画像を含むこともできます。
from Tkinter import * root = Tk() var = StringVar() label = Label( root, textvariable=var, relief=RAISED ) var.set("Hey!? How are you doing?") label.pack() root.mainloop() |
リストボックス | リストボックスウィジェットは、ユーザーにオプションのリストを提供するために使用されます。
from Tkinter import * import tkMessageBox import Tkinter top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "あ") Lb1.insert(2, "い") Lb1.insert(3, "う") Lb1.insert(4, "え") Lb1.insert(5, "お") Lb1.insert(6, "か") Lb1.pack() top.mainloop() |
メニューボタン | Menubuttonウィジェットは、アプリケーションにメニューを表示するために使用されます。
from Tkinter import * import tkMessageBox import Tkinter top = Tk() mb= Menubutton ( top, text="condiments", relief=RAISED ) mb.grid() mb.menu = Menu ( mb, tearoff = 0 ) mb["menu"] = mb.menu mayoVar = IntVar() ketchVar = IntVar() mb.menu.add_checkbutton ( label="mayo", variable=mayoVar ) mb.menu.add_checkbutton ( label="ketchup", variable=ketchVar ) mb.pack() top.mainloop() |
メニュー | メニューウィジェットは、ユーザーにさまざまなコマンドを提供するために使用されます。これらのコマンドはMenubutton内に含まれています。
from Tkinter import * def donothing(): filewin = Toplevel(root) button = Button(filewin, text="Do nothing button") button.pack() root = Tk() menubar = Menu(root) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="New", command=donothing) filemenu.add_command(label="Open", command=donothing) filemenu.add_command(label="Save", command=donothing) filemenu.add_command(label="Save as...", command=donothing) filemenu.add_command(label="Close", command=donothing) filemenu.add_separator() filemenu.add_command(label="Exit", command=root.quit) menubar.add_cascade(label="File", menu=filemenu) editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label="Undo", command=donothing) editmenu.add_separator() editmenu.add_command(label="Cut", command=donothing) editmenu.add_command(label="Copy", command=donothing) editmenu.add_command(label="Paste", command=donothing) editmenu.add_command(label="Delete", command=donothing) editmenu.add_command(label="Select All", command=donothing) menubar.add_cascade(label="Edit", menu=editmenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="Help Index", command=donothing) helpmenu.add_command(label="About...", command=donothing) menubar.add_cascade(label="Help", menu=helpmenu) root.config(menu=menubar) root.mainloop() |
メッセージ | メッセージウィジェットは、ユーザーからの値を受け入れるための複数行のテキストフィールドを表示するために使用されます。
from Tkinter import * root = Tk() var = StringVar() label = Message( root, textvariable=var, relief=RAISED ) var.set("Hey!? How are you doing?") label.pack() root.mainloop() |
ラジオボタン | Radiobuttonウィジェットは、ラジオボタンとしていくつかのオプションを表示するために使用されます。ユーザーは、一度に選択できるオプションは1つだけです。
from Tkinter import * def sel(): selection = "選んでね " + str(var.get()) label.config(text = selection) root = Tk() var = IntVar() R1 = Radiobutton(root, text="Option 1", variable=var, value=1, command=sel) R1.pack( anchor = W ) R2 = Radiobutton(root, text="Option 2", variable=var, value=2, command=sel) R2.pack( anchor = W ) R3 = Radiobutton(root, text="Option 3", variable=var, value=3, command=sel) R3.pack( anchor = W) label = Label(root) label.pack() root.mainloop() |
規模 | Scaleウィジェットは、スライダウィジェットを提供するために使用されます。
from Tkinter import * def sel(): selection = "Value = " + str(var.get()) label.config(text = selection) root = Tk() var = DoubleVar() scale = Scale( root, variable = var ) scale.pack(anchor=CENTER) button = Button(root, text="Get Scale Value", command=sel) button.pack(anchor=CENTER) label = Label(root) label.pack() root.mainloop() |
スクロール・バー | スクロールバーウィジェットは、スクロール機能をリストボックスなどのさまざまなウィジェットに追加するために使用されます。
from Tkinter import * root = Tk() scrollbar = Scrollbar(root) scrollbar.pack( side = RIGHT, fill=Y ) mylist = Listbox(root, yscrollcommand = scrollbar.set ) for line in range(100): mylist.insert(END, "This is line number " + str(line)) mylist.pack( side = LEFT, fill = BOTH ) scrollbar.config( command = mylist.yview ) mainloop() |
テキスト | テキストウィジェットは、複数の行にテキストを表示するために使用されます。
from Tkinter import * def onclick(): pass root = Tk() text = Text(root) text.insert(INSERT, "Hello.....") text.insert(END, "Bye Bye.....") text.pack() text.tag_add("here", "1.0", "1.4") text.tag_add("start", "1.8", "1.13") text.tag_config("here", background="yellow", foreground="blue") text.tag_config("start", background="black", foreground="green") root.mainloop() |
トップレベル | Toplevelウィジェットは、別々のウィンドウコンテナを提供するために使用されます。
from Tkinter import * root = Tk() top = Toplevel() top.mainloop() |
スピンボックス | Spinboxウィジェットは標準のTkinter Entryウィジェットの変種であり、一定数の値から選択するために使用できます。
from Tkinter import * master = Tk() w = Spinbox(master, from_=0, to=10) w.pack() mainloop() |
PanedWindow | PanedWindowは、水平または垂直に配置された任意の数のペインを含むコンテナウィジェットです。
from Tkinter import * m1 = PanedWindow() m1.pack(fill=BOTH, expand=1) left = Label(m1, text="left pane") m1.add(left) m2 = PanedWindow(m1, orient=VERTICAL) m1.add(m2) top = Label(m2, text="top pane") m2.add(top) bottom = Label(m2, text="bottom pane") m2.add(bottom) mainloop() |
LabelFrame | labelframeは単純なコンテナウィジェットです。その主な目的は、複雑なウィンドウレイアウト用のスペーサーまたはコンテナとして機能することです。
from Tkinter import * root = Tk() labelframe = LabelFrame(root, text="This is a LabelFrame") labelframe.pack(fill="both", expand="yes") left = Label(labelframe, text="Inside the LabelFrame") left.pack() root.mainloop() |
tkMessageBox | このモジュールは、アプリケーションにメッセージボックスを表示するために使用されます。
import Tkinter import tkMessageBox top = Tkinter.Tk() def hello(): tkMessageBox.showinfo("Say Hello", "Hello World") B1 = Tkinter.Button(top, text = "Say Hello", command = hello) B1.pack() top.mainloop() |