close

基本用法如下:

import tkinter as tk

root = tk.Tk()

text = tk.Label(root, text='Hello tkinter!').pack()

root.mainloop()

可變動修改文字內容方法:

import tkinter as tk

root = tk.Tk()

var = tk.StringVar()

var.set('Hello tkinter!!') #設定文字內容, 可依據條件做修改

text = tk.Label(root, textvariable=var, bg='green', fg='yellow', font=('Arial', 16)).pack()

root.mainloop()

在Label中顯示圖片

import tkinter as tk

root = tk.Tk()

img = tk.PhotoImage(file='test.gif')

showimg = tk.Label(root, image=img).pack()

root.mainloop()

基本結構:

label = Label ( master, option, ... )

tkinter提供Label共19個參數如下所示:

1. justify: 顯示多行文字的時候, 設置不同行之間的對齊方式, 有3種模式: LEFT, RIGHT, CENTER

2. compound: 設定圖片顯示在相對應的位置上, 有4種模式: LEFT,BOTTOM, RIGHT, TOP

3. padx: 在x軸方向上的內邊距(padding), 是指內容與Label邊緣的距離

4. pady: 在y軸方向上的內邊距(padding), 是指內容與Label邊緣的距離

5. text: 文字內容

6. image: 圖片影像

7. fg: 選項的前景顏色

8. bg: 背景顏色

9. font: 文字字體

10. width: 標籤寬度

11. height: 標籤高度

12. textvariable: 可使用textvariable代替text, 文字內容隨著變數值設置而改變

13. anchor: This options controls where the text is positioned if the widget has more space than the text needs. The default is anchor=CENTER, which centers the text in the available space.

14. bitmap: Set this option equal to a bitmap or image object and the label will display that graphic.

15. bd: The size of the border around the indicator. Default is 2 pixels.

16. cursor: If you set this option to a cursor name (arrow, dot etc.), the mouse cursor will change to that pattern when it is over the checkbutton.

17. relief: Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.

18. underline: You can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline=-1, which means no underlining.

19. wraplength: You can limit the number of characters in each line by setting this option to the desired number. The default value, 0, means that lines will be broken only at newlines.

 

 

 

※查詢其他Tkinter GUI元件用法如下※

http://jennaweng0621.pixnet.net/blog/category/6758171

arrow
arrow
    文章標籤
    python tkinter label
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()