視窗中的文字標籤 the text labels of window
視窗中的文字標籤 the text labels of window
Example Code
- import sys
- if sys.version_info[0] < 3:
- import Tkinter as tk
- else:
- import tkinter as tk
- win=tk.Tk()
- win.title("Tk GUI")
- label=tk.Label(win, text="The first line!")
- label.pack() #show the label object to win
- label2=tk.Label(win, text="The second line!")
- label2.pack() #show the label2 object to win
- win.mainloop()
Note and Comments
- label=tk.Label(win, text="The first line!") 宣告一個名為label的標籤物件, 此Label物件屬於tk函式庫,所以呼叫的語法爲tk.Label. 其中這個物件是屬於win(名稱爲win的tk視窗物件), 文字內容爲"The first line!"
- label.pack() 用pack()函數依序顯示label物件
result
文字標籤大小字型設定 the setting of text labels
文字標籤大小字型設定 the setting of text labels
Example Code
- import tkinter as tk
- win=tk.Tk()
- win.title("Tk GUI")
- win.geometry('800x150')
- label = tk.Label(win, text="Red color", fg="red")
- label.pack()
- label2 = tk.Label(win, text="Blue Helvetica", font=("Helvetica", 24), fg="blue")
- label2.pack()
- label3 = tk.Label(win, text="white Arial with black background", font=("Arial", 24), fg="white",bg="black")
- label3.pack()
- label4 = tk.Label(win, text="RGB a22 color", fg="#a22")
- label4.pack()
- win.mainloop()
Note and Comments
- fg="red" 將文字(前景)顏色設爲紅色, 也可使用RGB色彩
- font=("Arial", 24) 將字型設定爲Arial,大小設爲24
- bg="black" 將文字背景顏色設爲黑色
result