tkinter筆記003-添加文本標簽-Label


【文本標簽】

在GUI中是常見的小控件,用途非常廣

 

 1 import tkinter as tk
 2 from tkinter import ttk
 3 win=tk.Tk()
 4 
 5 win.title('Python Gui')
 6 
 7 #win.resizable(0,0) #阻止Python GUI的大小調整
 8 
 9 #增加一個文本標簽
10 ttk.Label(win,text='A1 Label').grid(column=0,row=0)
11 lab2=ttk.Label(win,text='A2 Label',background='red')
12 lab2.grid(column=1,row=0)
13 ttk.Label(win,text='B1 Label',font=30).grid(column=0,row=1)
14 ttk.Label(win,text='B2 Label',foreground='red').grid(column=1,row=1)
15 
16 win.mainloop()

 

 

Label是ttk里的一個類,它的標准屬性有

activebackground=
What background color to use when the label is active (set with the state option). The default is platform specific. (the option database name is activeBackground, the class is Foreground)
標簽處於活動狀態時使用的背景顏色(使用狀態選項進行設置)。默認是平台特定的。(選項數據庫名稱是activeBackground,類是Foreground)

activeforeground
= What foreground color to use when the label is active. The default is platform specific. (activeForeground/Background) 激活標簽時使用什么前景色。默認是平台特定的。(activeForeground /背景)

anchor
= Controls where in the label the text (or image) should be located. Use one of N, NE, E, SE, S, SW, W, NW, or CENTER. Default is CENTER. (anchor/Anchor)
控件在標簽中的文本(或圖像)應該位於何處。使用N,NE,E,SE,S,SW,W,NW或CENTER中的一個。默認為中心CENTER

background
= The background color. The default is platform specific. (background/Background)
背景色。默認是同於平台的。
bitmap= The bitmap to display in the widget. If the image option is given, this option is ignored. (bitmap/Bitmap)
要在小部件中顯示的位圖。如果給定圖像選項,則忽略此選項。

borderwidth
= The width of the label border. The default is system specific, but is usually one or two pixels. (borderWidth/BorderWidth)
標簽邊框的寬度。默認值是同於系統的,但通常是一個或兩個像素。
compound= Controls how to combine text and image in the label. By default, if an image or bitmap is given, it is drawn instead of the text. If this option is set to CENTER, the text is drawn on top of the image. If this option is set to one of BOTTOM, LEFT, RIGHT, or TOP, the image is drawn besides the text (use BOTTOM to draw the image under the text, etc.). Default is NONE. (compound/Compound)
控制如何將文本和圖像組合在標簽中。默認情況下,如果給定一個圖像或位圖,則將其繪制為文本。如果將此選項設置為中心,則在圖像頂部繪制文本。如果該選項設置為底部、左側、右側或頂部,則除文本之外繪制圖像(使用底部繪制文本下的圖像等)。默認沒有。

cursor
= What cursor to show when the mouse is moved over the label. The default is to use the standard cursor. (cursor/Cursor)
鼠標移動到標簽上時顯示什么光標。默認值是使用標准游標。

disabledforeground
= What foreground color to use when the label is disabled. The default is system specific. (disabledForeground/DisabledForeground)
當標簽被禁用時使用什么前景色。默認值是特定於系統的。

font
= The font to use in the label. The label can only contain text in single font. The default is system specific. (font/Font)
標簽中使用的字體。標簽只能包含單個字體的文本。默認值是特定於系統的。

foreground
= The label color, used for for text and bitmap labels. The default is system specific. (foreground/Foreground)
標簽的顏色,文字和位圖的標簽。默認的是系統特定的
height= The height of the label. If the label displays text, the size is given in text units. If the label displays an image, the size is given in pixels (or screen units). If the size is set to 0, or omitted, it is calculated based on the label contents. (height/Height) highlightbackground= What color to use for the highlight border when the widget does not have focus. The default is system specific, but usually the same as the standard background color. (highlightBackground/HighlightBackground) highlightcolor= What color to use for the highlight border when the widget has focus. The default is system specific. (highlightColor/HighlightColor) highlightthickness= The width of the highlight border. The default is 0 (no highlight border). (highlightThickness/HighlightThickness) image= The image to display in the widget. The value should be a PhotoImage, BitmapImage, or a compatible object. If specified, this takes precedence over the text and bitmap options. (image/Image) justify= Defines how to align multiple lines of text. Use LEFT, RIGHT, or CENTER. Note that to position the text inside the widget, use the anchor option. Default is CENTER. (justify/Justify) padx= Extra horizontal padding to add around the text. The default is 1 pixel. (padX/Pad) pady= Extra vertical padding to add around the text. The default is 1 pixel. (padY/Pad) relief= Border decoration. The default is FLAT. Other possible values are SUNKEN, RAISED, GROOVE, and RIDGE. (relief/Relief) state= Label state. This option controls how the label is rendered. The default is NORMAL. Other possible values are ACTIVE and DISABLED. (state/State) takefocus= If true, the widget accepts input focus. The default is false. (takeFocus/TakeFocus) text= The text to display in the label. The text can contain newlines. If the bitmap or image options are used, this option is ignored. (text/Text) textvariable= Associates a Tkinter variable (usually a StringVar) with the label. If the variable is changed, the label text is updated. (textVariable/Variable) underline= Used with the text option to indicate that a character should be underlined (e.g. for keyboard shortcuts). Default is -1 (no underline). (underline/Underline) width= The width of the label. If the label displays text, the size is given in text units. If the label displays an image, the size is given in pixels (or screen units). If the size is set to 0, or omitted, it is calculated based on the label contents. (width/Width) wraplength= Determines when a label’s text should be wrapped into multiple lines. This is given in screen units. Default is 0 (no wrapping). (wrapLength/WrapLength)

【小程序--來自網絡】

運行后自動加1,直到按下stop鍵

 1 import tkinter as tk
 2 
 3 counter = 0 
 4 def counter_label(label):
 5   def count():
 6     global counter
 7     counter += 1
 8     label.config(text=str(counter))
 9     label.after(1000, count)
10   count()
11  
12  
13 root = tk.Tk()
14 root.title("Counting Seconds")
15 label = tk.Label(root, fg="green")
16 label.pack()
17 counter_label(label)
18 button = tk.Button(root, text='Stop', width=25, command=root.destroy)
19 button.pack()
20 root.mainloop()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM