2014年11月23日 星期日

python 3.4 tkinter widgets (整理中)

以下是我用 dir(module name)撈出來整理的widgets資料,人工過濾了一些底層不大會直接用到的 class,仍可能有一些還沒濾掉。目前至少可以看出有哪些可能可以使用的widgets。

在簡易說明中,只有提示簡單的用法。如果完全沒使用過tkinter,可能要先去找Hello world之類的範例來起步。如果要看widgets有什麼屬性,可以用 widget_obj.config() 來顯示。

使用tix的widgets時,要記得使用tkinter.tix.Tk(),不然會有錯誤訊息。

其它可參考之資訊:
http://effbot.org/tkinterbook/tkinter-index.htm (有點舊,但是有助於觀念)
http://www.tkdocs.com/  (有持續更新,如果能夠看英文,建議看它的Tutorial)

tkinterttktix簡易說明或範例(日後補充)
Balloon 例如滑鼠移至按鍵時,冒出說明文字。
ba = Balloon()
ba.bind_widget(btn, balloonmsg='Hello', statusmsg='Hi')
Button btn = Button(text='Hello')
ButtonBox 方便一次新增多個按鍵
btnbox=ButtonBox()
btnbox.add('ok', text='OK', command=lambda : root.destroy())
btnbox.add('close', text='Cancel', command=lambda : root.destroy())
Canvas 此處有不錯的範例
CheckList chklist=CheckList()
chklist.hlist.add('choice1', text='This is Choice1') chklist.hlist.add('choice2', text='This is Choice2') checklist.setstatus('choice1', 'on') # or 'off' checklist.getstatus('choice1') # 回傳 'on', 'off'
Checkbutton ckbtn_var = IntVar()
ckbtn=Checkbutton(text='btn1', variable=ckbtn_var)
ckbtn_var.get() # 之後取值,值為 0, 1
ComboBox str_var = StringVar()
cb = ComboBox(variable=str_var)
cb.insert(tix.END, '一月')
cb.insert(tix.END, '二月')
cb.insert(tix.END, '三月')
str_var.get() # 讀取值
Combobox str_var = StringVar()
cb = comboBox(textvariable=str_var)
cb['values'] = ('一月', '二月', '三月')
str_var.get() # 讀取值
Control 一個輸入數字的文字框,附加上下鍵按紐。
v1 = IntVar()
ctrl = Control(variable=v1)
v1.get() # 讀取值
DialogShell 會跳出一個視窗。 ds = DialogShell() ds.popup() # 跳出 ds.popdown() # 消失
DirList Segmentation fault....
DirSelectBox Segmentation fault....
DirSelectDialog Segmentation fault....
DirTree Segmentation fault....
Entry 單行的文字輸入框
str_var = StringVar()
entry = Entry(textvariable=str_var)
str_var.get() # 讀取值
entry.get() # 也可以
ExFileSelectBox Segmentation fault....
ExFileSelectDialog Segmentation fault....
FileEntry Segmentation fault....
FileSelectBox Segmentation fault....
FileSelectDialog Segmentation fault....
Frame f1 = Frame() # container功用
Grid Layout manager
gd = Grid()
for x in range(3):
  for y in range(3):
    Label(text='{0}-{1}'.format(x,y), master=gd).grid(row=x, column=y)
gd.pack()
HList hl = HList()
hl.add('Item1', text='Item1 Text') # 最上層項目
hl.add('Item1.SubItem1', text='SubItem1 Text') # 第二層
# CheckList當中也有HList,可作出階層效果
InputOnly Segmentation fault....
Label Label(text='要顯示的文字')
LabelEntry Label + Entry。
le = LabelEntry()
le.label['text'] = '請輸入帳號'
le.entry.get() # 之後用這行來讀取值
LabelFrame Label + Frame
ttklf = LabelFrame()
ttklf['text'] = '標籤'
#### (tix.LabelFrame測試時不大能正常顯示)
tixlf = LabelFrame()
tixlf.label['text'] = '標籤'
LabeledScale 用Scrollbar的方式讓人輸入數字。
l_s = LabeledScale() # 預設是0~10的整數
l_s.scale['from'] = -10 #改成 -10~10
l_s.scale['to'] = 100        # -10~100
l_s.label['text']                # 取得目前的數字
Labelframe 參考LabelFrame
ListNoteBook
Listbox
Menu
Menubutton
Message
Meter
Misc
NoteBook
NoteBookFrame
Notebook nb = ttk.Notebook()
#新增兩個tab
nb.add(ttk.Label(nb, 'Hello'), text='Tab 1')
nb.add(ttk.Label(nb, 'Hi'), text='Tab 2')
#目前所在的tab id
tab_id = nb.index(nb.select())
# 刪除 tab
nb.forget(tab_id)
OptionMenu
PanedWindow
Panedwindow
PopupMenu
Progressbar
Radiobutton
ResizeHandle
Scale
Scrollbar
ScrolledGrid
ScrolledHList
ScrolledListBox
ScrolledTList
ScrolledText
ScrolledWindow
Select
Separator
Shell
Sizegrip
Spinbox
StdButtonBox
Studbutton
TList
Text
Toplevel
Tree
Treeview
Tributton

  • 其它: (root = Tk())
    • 使用 Label 來顯示png檔:
      • 安裝 PIL (for python3)
        • sudo apt-get install python3-pil
        • sudo apt-get install python3-pil.imagetk
      • 程式碼:
        • from PIL import ImageTk, Image
          resized_image = Image.open(檔名) 
          img = ImageTk.PhotoImage(resized_image)
          label = Label(root, image=img
      • 注意事項:
        • 上方程式碼中的img,必須要是global variable或是依附在不會消失的物件上(例如 label.image = img),才不會在garbage collection時被清掉,導致無法顯示。
      • 同時顯示圖和文字:Label(text='....', image=img, compound=TOP/BOTTOM/LEFT/RIGHT/CENTER)
    • 修改 Label 的字型、大小、與加強效果
      • label = tk.Label(root, text='Hello')
      • label['font'] = (字型, 大小, 效果)
        • 字型:字串,例如 Times, arial
        • 大小:數字
        • 效果:normal, bold, italic, underline, overstrike,可一次使用多個
      • Example: label['font'] = ('Times', 20, 'bold italic')
    • 視窗最大化/全螢幕:
      • root.attributes('-zoomed', True)  # 最大化
      • root.attributes('-fullscreen', True) # 全螢幕
    •