本手冊翻譯自http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
摘要
本手冊描述了用於在Python程序語言中構建圖形用戶界面(GUIs)的Tkinter組件,包括ttk主題的組件。
本手冊的發行物可以 在線形式 或者是 PDF 文檔形式 閱讀。請發送評論意見和評論到 [email protected]
.
1. 一個爲Python建造圖形用戶界面的交互式平臺
Tkinter 是爲Python設置的一個 GUI (圖形用戶界面) 組件。本手冊的內容是面向運行在Linux下 X Window 系統的 Python 2.7 和 Tkinter 8.5。 你的版本可能與此不同。
參考文獻:
-
Fredrik Lundh, Tkinter的作者, 他的 An Introduction to Tkinter有兩個版本: 更完整的 1999 version and 2005 version 其中呈現了一些新的特性。
-
Python 2.7 quick reference: 關於 Python 語言的一般信息.
-
拿一個相當大的應用程序(大約1000行代碼)舉例說明, 參考 huey: A color and font selection tool. 這個程序的設計示範了怎麼樣去建立屬於你的複合組。
我們將通過查看 Tkinter 的可見部分開始Tkinter: 創建小部件,並安排他們在屏幕上。稍後我們將討論如何聯繫表面——應用程序的“前面板”和它背後的邏輯。
2. 一個最小的程序
這是一個很微不足道的 Tkinter 程序,它只包含一個退出按鈕:
#!/usr/bin/env python 1 import Tkinter as tk 2 class Application(tk.Frame): 3 def __init__(self, master=None): tk.Frame.__init__(self, master) 4 self.grid() 5 self.createWidgets() def createWidgets(self): self.quitButton = tk.Button(self, text='Quit', command=self.quit) 6 self.quitButton.grid() 7 app = Application() 8 app.master.title('Sample application') 9 app.mainloop() 10
1 | 假設你的系統中正確安裝了Python,此行使腳本自動執行。 |
2 | 此行導入Tkinter 的模塊到你的程序的名字空間,但將其重命名爲 tk
。 |
3 | 您的Application類必須繼承自Tkinter的Frame 類。 |
4 | 調用構造函數的父類, Frame 。 |
5 | 必須使應用程序實際上出現在屏幕上。 |
6 | 創建標籤爲“Quit”的按鈕。 |
7 | 把這個按鈕放置在應用程序中。 |
8 | 主程序通過實例化 Application 類,在此開始。 |
9 | 此方法調用設置的了應用程序的標題爲 “Sample application”. |
10 | 啓動應用程序的主循環,等待鼠標和鍵盤事件。 |