from tkinter import *
win = Tk()
some = Label(win, text="Tk's job!!", width="30", height="5")
some.pack()
win.mainloop()
源自於
http://www.kaiching.org/2012/12/Python-Basic-Concept-of-GUI.html
Tk 包含各種常用的 GUI 元件,如下列表
| 元件名稱 | 類別名稱 |
|---|---|
| 視窗區域 | TkFrame |
| 標籤 | TkLabel |
| 按鈕 | TkButton |
| 核取方塊 | TkCheckButton |
| 選取方塊 | TkRadioButton |
| 下拉式列表 | TkComboBox |
| 列表 | TkListbox |
| 捲動軸 | TkScrollbar |
| 文字方塊 | TkEntry |
| 文字區域 | TkText |
以下是個簡單例子
1
2
3
4
5
6
7
8
9
10
11
12
13
| from tkinter import *root = Tk()some = Label(root, text="Tk's job!!", width="30", height="5")some.pack()root.mainloop()# 《程式語言教學誌》的範例程式# 檔名:tkdemo.py# 功能:示範 Python 程式# 作者:張凱慶# 時間:西元 2012 年 12 月 |
使用 Tk 首先要 import tkinter ,這裡用 from , import 之後星號表示所有名稱,這使我們在程式中可以直接使用 tkinter 中的名稱,不需要連帶寫出 tkinter
1
| from tkinter import * |
然後建立 Tk 物件 (object)
3
| root = Tk() |
這個視窗只有一個文字標籤, Label 型態的 some
4
| some = Label(root, text="Tk's job!!", width="30", height="5") |
這裡用了幾個參數 (parameter) ,文字標籤的文字 text 為 Tk's job!! ,高 height 為 5 ,寬 width 為 30 。

沒有留言:
張貼留言