bloggerads

2017年8月13日 星期日

Python : Tkinter (Button, Label, Entry, Listbox)

Tkinter 並不是一個很強大的GUI library, 但優點是, 他是Python官方支援的module, 且大部分的OS都有預先安裝它<Note 1>。 之後陸續出現的第三方GUI library相信都是由他啟發的。因此如果不是開發太複雜的GUI tool, 又想了解這個歷史悠久的library, 那就值得來了解他。

話不多說,直接上code:

step 1. 先來創一個空的視窗

from Tkinter import *
    
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")

win.mainloop()




step 2. 在視窗加一些Button

元件放的位置是由obj.grid來控制,至於column和row的大小,是根據每個元件文字長度自動決定的。

from Tkinter import *

def clickOkButton():
    print 'Button Clicked!'
    
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")

button1=Button(win, text="button 1 @ row=0, column=0", command=clickOkButton)
button1.grid(row=0, column=0)

button2=Button(win, text="button 2 @ row=0, column=1", command=clickOkButton)
button2.grid(row=0, column=1)

button3=Button(win, text="button 3 @ row=1, column=0", command=clickOkButton)
button3.grid(row=1, column=0)

button4=Button(win, text="button 4 @ row=1, column=1", command=clickOkButton)
button4.grid(row=1, column=1)


win.mainloop()



step 3. 在視窗再加個可供輸入的edit元件

from Tkinter import *

def clickOkButton():
    print 'You typed: ' + edit1.get()  # Get what you typed
    edit1.delete(0,END)  # Delete edit contents
    
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")

button1=Button(win, text="button 1 @ row=0, column=0", command=clickOkButton)
button1.grid(row=0, column=0)

button2=Button(win, text="button 2 @ row=0, column=1", command=clickOkButton)
button2.grid(row=0, column=1)

button3=Button(win, text="button 3 @ row=1, column=0", command=clickOkButton)
button3.grid(row=1, column=0)

button4=Button(win, text="button 4 @ row=1, column=1", command=clickOkButton)
button4.grid(row=1, column=1)

edit1 = Entry(win)
edit1.grid(row=2, column=0)


win.mainloop()



step 4. 最後在加個 label 基本的 GUI 就出來了

from Tkinter import *

def clickOkButton():
    print 'You typed: ' + edit1.get()  # Show what you typed
    edit1.delete(0,END)  # Delete edit contents
    
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")

button1=Button(win, text="button 1 @ row=0, column=0", command=clickOkButton)
button1.grid(row=0, column=0)

button2=Button(win, text="button 2 @ row=0, column=1", command=clickOkButton)
button2.grid(row=0, column=1)

button3=Button(win, text="button 3 @ row=1, column=0", command=clickOkButton)
button3.grid(row=1, column=0)

button4=Button(win, text="button 4 @ row=1, column=1", command=clickOkButton)
button4.grid(row=1, column=1)

edit1 = Entry(win)
edit1.grid(row=2, column=1)

Label(win, text="Type Something here: ").grid(row=2, column=0)

win.mainloop()

step 4. 加個 Listbox, 調整 Listbox 的 size

from Tkinter import *

def clickOkButton():
    t = edit1.get()
    lb.insert(END,t)
    edit1.delete(0,'end')

def clickDeleteButton():
    try:
        index = lb.curselection()
        lb.delete(index[0])
    except IndexError:
        print 'Nothing is selected!'

win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x500")

button1=Button(win, text="ADD @ row=0, column=0", command=clickOkButton)
button1.grid(row=0, column=0)

button2=Button(win, text="DELETE @ row=0, column=1", command=clickDeleteButton)
button2.grid(row=0, column=1)

Label(win, text="Type Something here: ").grid(row=1, column=0)

edit1 = Entry(win)
edit1.grid(row=1, column=1)

lb = Listbox(win, height=20, width=40)
lb.grid(row=2, column=0,columnspan=2)
lb.insert('end', 'First item')

win.mainloop()



<Note 1>
  • 在我的Windows系統中,我安裝的是 Python 2.7, 有pre-install Tkinter 
  • 在我的Centos系統中, 預設已安裝了 Python 2.7.5, 但沒有pre-install Tkinter, 請打以下指令安裝: 
    • yum -y install tkinter
  • 在我的Ubuntu系統中, 預設已安裝了 Python 2.7.12, 但沒有pre-install Tkinter, 請打以下指令安裝: 
    • sudo apt-get install python-tk

沒有留言:

張貼留言