程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Tkinter - Chapter 4.2 tag attributes

編輯:Python

4.2 Tag attributes
This section describes the properties of the tag . Each type of control has many properties . The properties of the label control are shown in the following table :

attribute describe activebackground The status of the tag is ACTIVE When the background color .activeforeground The status of the tag is ACTIVE Text color when .anchor How to anchor text or pictures . General is to use CENTER( Centered mode ). You can choose the following methods : N, NE, E, SE, S, SW, W, NW, or CENTER.background (bg) The background color of the label . The default is the system specified color bitmap Show on label bitmap. If set images attribute , It ignores bitmap attribute .borderwidth(bd) The border width of the label . It's usually 1~2 Pixel values .compound Set how to display text and pictures on labels at the same time :
CENTER: Overlay the text in the center of the picture
BOTTOM: Show text below the picture
LEFT: Show text on the left side of the picture
RIGHT: Show text on the right side of the picture
TOP: Show text above picture
NONE: Don't show text cursor When the mouse moves over the tag , Displayed cursor style disabledforeground The background color when the label is prohibited font The font of the text on the label . Only one font can be selected to display .foreground
fg The words on the label or bitmap The color of the ( Not including pictures )height Set the height of the label . If display text , Numerical values are literal units . If it is a display picture , Values are in pixels . If not set , The system automatically calculates the height of the label .highlightbackground When the tag doesn't get the focus , The highlight color of the border highlightcolor When the tag gets the focus , The highlight color of the border highlightthickness Set the width of the highlighted border , The default value is 0image Set the picture displayed by the label . If this option is set , Will ignore text or bitmap Options .justify When a label has multiple lines of text , Set alignment of text . The settable values are :
LEFT,RIGHT,CENTERpadx In the horizontal direction , The inner margin between the border and the text or image inside the label pady In the vertical direction , The inner margin between the border and the text or image inside the label relief Border beautification effect . The default value is FLAT, Other options include :SUNKEN, RAISED, GROOVE, RIDGE and SOLIDstate The status of the tag , Include :NORMAL, ACTIVE perhaps DISABLEDtakefocus Identify whether the user can use Tab Key to move the focus to the label .text The text displayed on the label . If used bitmap perhaps image, This option is ignored textvariable take StringVar Variables are associated with labels . If the contents of a variable change , The text of the label is also updated .underline Identify which character to underline . The default value is -1, No underline width Set the width of the label . Reference resources height attribute wraplength Make sure that the text on the label exceeds the display range , The text will be folded into multiple lines . Unit is pixel . The default value is 0.4.2.1 activebackground and activeforeground

Define label in ACTIVE In the state of , Background color and text color . These two properties can be used to highlight text , Or some special effect .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Background and text color change ',
activebackground='yellow',
activeforeground='red',state=tk.ACTIVE)
b1.pack()
root.mainloop()

result :

4.2.2 anchor
Specify how to align text . In general , The size of the tag matches the content to be displayed . However, there may be cases where the label is very large , It can be used at this time anchor To specify how to display text .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Text alignment :SW',
anchor=tk.SW,width=20,height=5,bg='blue')
b1.pack()
root.mainloop()

result :

SW It means the southwest corner , That's the lower left corner . Other situations , As long as change anchor You can see the effect of the parameter . For a detailed description, see 3.3.4 section .

4.2.3 background & bg
Is to set the background color of the label , In the example in the previous section , This option is used to set the background of the label to blue . Please refer to the previous section .
4.2.4 bitmap
bitmap Not at all windows The bitmap inside , But in another format .tkinter Built in some bitmap picture , It can be used in the program .

import tkinter as tk
root=tk.Tk()
root.geometry('240X160')
b1 = tk.Label(root,bitmap='@x.xbm')
b1.pack()
root.mainloop()

result :

notes :1) Use the system's own bitmap, Just enter your name . such as bitmap=’error’
2) Use custom bitmap, Add the ’@’ that will do .
4.2.5 borderwidth & bd
Set the width of the label .bd Is an abbreviation .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Border width ',
bd=20,bg='blue')
b1.pack()
root.mainloop()

result :

4.2.6 compound
compound Is to set how to display text on the picture . There are detailed descriptions in the table . For specific usage, refer to 4.1.3 Examples in .
4.2.7 cursor
When the mouse passes the label , The figure of the cursor . For specific cursor definitions, see .6 section

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Mouse shape ',bd=20,bg='blue',cursor="watch")
b1.pack()
root.mainloop()

4.2.8 disabledforeground
Display the text color when the label is disabled . Be sure to use state=tk.DISABLED, Set the status of the tag to DISABLED, To make this option work .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Label prohibition ',
disabledforeground='red',state=tk.DISABLED)
b1.pack()
root.mainloop()

result :

4.2.9 font
The font on the label . Only one font can be selected in a label . If you want to use multiple Fonts , Multiple tags can be used to implement . Description and use of fonts , Please see the .3 section .

4.2.10 foreground & fg
Settings are normal (NORMAL) Under the circumstances , The color of the text in the label .fg Is an abbreviation .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1=tk.Label(root,text=' Label text color ',fg='blue')
b1.pack()
root.mainloop()

result :

4.2.11 height
Set the height of the label . It should be noted that , In the case of text and images , The units of height are different . Text is set according to the number of lines of text , Image mode , It is set according to the size of pixels .
1. Text is set according to the number of lines

import tkinter as tk
root=tk.Tk()
b1=tk.Label(root,text=' The label height =10',
bg='blue',height=10)
b1.pack()
b2=tk.Label(root,text=' The label height =3',
bg='red',height=3)
b2.pack()
root.mainloop()

result :

2. Images
If an image is placed in the label , The default size is set in pixels . Other units of measurement can also be used . See .1 Section .

import tkinter as tk
root=tk.Tk()
p = tk.PhotoImage(file='a.gif')
b1 = tk.Label(root,image=p,height=100)
b1.pack()
root.mainloop()

result :

4.2.12 highlightbackground,highlightcolor and highlightthickness
Corresponding to the color of the border when the label does not get focus (highlightbackground)、 The color of the border when the label gets focus (highlightcolor) And the width of the border (highlightthickness). However, because the tag does not support input , So this 3 Parameters are actually useless .
4.2.13 image
Display images . And bitmap The format of is different . It is recommended to use gif Format . For detailed usage, see 4.1.2
4.2.14 justify
Text alignment function . When the label is wider than the displayed text , You can use alignment to adjust the display of text . Supported alignments are :
LEFT
RIGHT
CENTER

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text=' Text alignment 1234\nabcdefg',
anchor='e',width=20,height=5,
justify=tk.RIGHT,bg='blue')
b1.pack()
root.mainloop()

result :

justify Is the alignment of multiple lines of text .anchor Is the alignment of the text . If not used anchor, The text is centered . The right alignment of the above figure will not appear .
Then don't use justify Is it possible to right align multiple lines of text ? Except in special circumstances , That is, all the text is the same length on each line , Otherwise, rows that are not right aligned will appear . actually ,anchor Just make sure that the first line can be right aligned or any other alignment , The multiline approach must use justify To control alignment .
4.2.15 padx and pady
Margins of labels and other controls .padx It's horizontal ,pady It's vertical . See the following figure for the detailed relationship .

4.2.16 relief
Border beautification effect . The supported formats are :FLAT,SUNKEN, RAISED, GROOVE, RIDGE and SOLID. For details, see 3.3.5 section

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text='FLAT Format ',
relief='flat',bg='blue',bd=5)
b1.pack()
b2 = tk.Label(root,text='SUNKEN Format ',
relief='sunken',bg='yellow',bd=5)
b2.pack()
b3 = tk.Label(root,text='RAISED Format ',
relief='raised',bg='red',bd=5)
b3.pack()
b4 = tk.Label(root,text='GROOVE Format ',
relief='groove',bg='green',bd=5)
b4.pack()
b5 = tk.Label(root,text='RIDGE Format ',
relief='ridge',bg='purple',bd=5)
b5.pack()
b5 = tk.Label(root,text='SOLID Format ',
relief='solid',bg='gray',bd=5)
b5.pack()
root.mainloop()

result :

4.2.17 state
Set the status of the label . The default is NORMAL. The other two states are ACTIVE and DISABLED
4.2.18 takefocus
The value is True perhaps False. The default value is False. The function of this option is through Tab Key to move the focus to the label .
4.2.19 text
Set the text to be displayed on the label .
4.2.20 textvariable
It's a tkinter Variable , If the contents of this variable change , The content of the label also changes . Using this attribute requires the introduction of tkintervar modular .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
label_str=tk.StringVar()
label_str.set('FLAT Format ')
b1 = tk.Label(root,text='FLAT Format ',relief='flat',
bg='blue',bd=5,textvariable=label_str)
b1.pack()
def change_b1():
label_str.set(' Changed the content of the label ')
b2 = tk.Button(root,text='b1',command=change_b1)
b2.pack()
root.mainloop()

result :


This function is useful for dynamically changing the contents of tags . Of course , You can also set the label text Option to change the contents of the tag .
4.2.21 underline
Show an underline under a letter .

import tkinter as tk
root=tk.Tk()
root.geometry('300x240')
b1 = tk.Label(root,text='Underline',underline=1)
b1.pack()
root.mainloop()

result :

4.2.22 width
Set the width of the label . Specific usage and height similar , Please see the 4.2.11 section .
4.2.23 wraplength
If beyond a certain range , Wrap the label text . Note that this parameter is in pixels , Instead of the usual text width . See for specific usage 4.1.3 section .
4.3 Dynamic modification of properties
In the running of the program , It is unavoidable to dynamically modify the properties of the control . How to modify ? Very simple :
label[‘ The attribute name ’]= New properties

For example, to modify the text of the label , It only needs :
label[‘text’]=‘ New label text ’
All control properties can be modified through this method . I won't repeat it later . However, not all control properties can be dynamically modified , Some properties can only be set during initialization . In the life cycle of the control , Do not modify .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved