| ■ | メッセージダイアログの表示 |
from Tkinter import *
import tkMessageBox, os.path, string
def j(str, encoding="japanese.shift_jis"):
return unicode(str, encoding).encode("utf8")
class App(Frame):
def init(self):
self.master.title("ディレクトリに含まれるファイル数")
a = Label(self, text=j("ディレクトリ名:"))
self.ea = Entry(self, width=40)
f = Frame()
b = Button(f, text=j("検索"), command=self.search)
c = Button(f, text=j("終了"), command=self.quit)
for e in (b, c): e.pack(side=LEFT, padx=3, pady=3)
a.pack(side=TOP, anchor=W, pady=5)
self.ea.pack(side=TOP, pady=5)
f.pack(side=TOP, anchor=E, pady=5)
def quit(self):
self.master.destroy()
def search(self):
# getメソッドで取得します。
dirname = self.ea.get()
# メッセージダイアログはこのように1行書くだけで表示できます。
if os.path.isdir(dirname):
tkMessageBox.showinfo(title="Found:",
message=str(len(os.listdir(dirname))) + " file(s) exists.")
else:
tkMessageBox.showerror(title="Ootto!",
message="Ootto! " + dirname + " is not a directory.")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(); self.init()
if __name__ == "__main__": app = App(); app.mainloop()
# end.
|
メッセージダイアログは、モジュールtkMessageBox で定義されているので、これをインポートします。 最も簡単にメッセージダイアログを出すには、 tkMessageBoxモジュールのshowinfoや showerrorなどの関数を使います。 これらはそれぞれの意味に即したアイコンなどを自動的につけてくれるので、 プログラマはtitle=でタイトルを、message=で表示させたい文字列を指定するだけで OKです。
| ■ | textvariableと変数オブジェクト |
a = Label(self, text=j("ディレクトリ名:"))
self.ea = Entry(self, width=40)
f = Frame()
b = Button(f, text=j("検索"), command=self.search)
|
dirname = self.ea.get() |
val="文字列" a = Entry(self, width=30, textvariable=val) |
self.var = StringVar() a = Entry(self, width=30, textvariable=self.var) |
self.var.set("/usr/local/bin/python")
path = self.var.get()
|
from Tkinter import *
import tkMessageBox, os.path, string
def j(str, encoding="japanese.shift_jis"):
return unicode(str, encoding).encode("utf8")
class App(Frame):
def init(self):
self.master.title("ディレクトリに含まれるファイル数")
a = Label(self, text=j("ディレクトリ名:"))
self.dirnamevar = StringVar()
self.dirnamevar.set("C:/usr/lang/python")
ea = Entry(self, width=40, textvariable=self.dirnamevar)
f = Frame()
b = Button(f, text=j("検索"), command=self.search)
c = Button(f, text=j("終了"), command=self.quit)
for e in (b, c): e.pack(side=LEFT, padx=3, pady=3)
a.pack(side=TOP, anchor=W, pady=5)
ea.pack(side=TOP, pady=5)
f.pack(side=TOP, anchor=E, pady=5)
def quit(self):
self.master.destroy()
def search(self):
# getメソッドで取得します。
dirname = self.dirnamevar.get()
# メッセージダイアログはこのように1行書くだけで表示できます。
if os.path.isdir(dirname):
tkMessageBox.showinfo(title="Found:",
message=str(len(os.listdir(dirname))) + " file(s) exists.")
else:
tkMessageBox.showerror(title="Ootto!",
message="Ootto! " + dirname + " is not a directory.")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(); self.init()
if __name__ == "__main__": app = App(); app.mainloop()
# end.
|
なお、エントリのtextvariable=と同様に、チェックボタン(Checkbutton) やラジオボタン(Radiobutton)にはvariable=というオプションがありますが、 これらにもこのStringVarやその一族が同様に使えます。
セクションのサブメニューに戻る
(first uploaded 2001/08/05 last updated 2002/03/11)