Python通過調用tkinter庫來實現圖形化。Python中的窗口更加內容大小自動縮放。
- 例1:創建一個簡單的窗口:
-
from tkinter import * #引入tkinter庫 root = Tk() #創建一個主窗口,Tk(className='aaa')定義一下參數值 root.mainloop() #主窗口的成員函數,主窗口運作起來,開始接受鼠標和鍵盤的操作。
- 例2:添加一些控件,比如label,button
-
from tkinter import * root = Tk(className='aaa') label = Label(root) label['text'] = 'This is a label.' label.pack() #和控件的布局排版有關的設置 root.mainloop()
-
- label1 = Lable(root) #參數為root表明這個控件是root主窗口的成員控件
- label1['text'] = 'This is a label' #設置標簽的text屬性值
- 例3:控件的事件,由函數來處理
-
from tkinter import * def on_click(): button['text'] = 'It is changed.' root = Tk(className='aaa') button = Button(root) button['text'] = 'change it' button['command'] = on_click #事件關聯函數 button.pack() root.mainloop()
- 變量是在運行時關聯而不是在代碼前后行的關系,所以button['text'] = 'It is changed.' 不會報告未定義的錯誤。
參考文章:
- Python完全新手教程:http://www.cnblogs.com/taowen/articles/11239.html