[Tkinter Series 16/45] Entry, Frame, Label of ttk component library

32. ttk.Entry

The purpose of the widget Entry is to allow the user to enter or edit a single line of text. This is the ttk version of Section 10 “Widgets”. Entry

To create a ttk.Entry widget as a child of the given widget parent item:

w = ttk.Entry( parent , option = value , ...)

Options:

Table 40. ttk.EntryOptions

class_ Widget class name. This can be specified when the widget is created, but cannot be changed later. See Section 27, “Standardized Appearance” for a description of widget classes.
cursor Appears when the mouse is hovering over a check button cursor; see Section 5.8 “Cursors”.
exportselection By default, if you are in Entry Select text in the widget and it will be automatically exported to the clipboard. To avoid this export, use exportselection=0.
font Use this option to specify the font for text displayed in the widget; see Section 5.4, “Type Fonts”. For reasons unknown to the author, this option cannot be specified using styles.
invalidcommand You can set this option as a callback function, every This function is called when validation fails (that is, when validatecommand0 is returned). See Section 10.2, “Adding Validation Entries to Widgets”.
justify This option specifies when text does not completely fill the input area How the text will be positioned within the input area. Values can be tk.LEFT left-justified; tk.CENTER to the center; or tk.RIGHT right-justified.
show To prevent fields such as passwords from being visible on the screen, please Set this option to a string whose first character replaces every actual character in the field. For example, if the field contains “sesame” but you have specified show='*', the field will appear as “****** “.
style The style to use when rendering this checkbox; please See Section 49, “Using and Customizing ttk Styles”.
takefocus By default, focus traversal will contain a < em>ttk ;.CheckbuttonSee Section 53, “Focus: Routing Keyboard Input”. To remove a widget from focus traversal, use takefocus=False.
textvariable Variable that controls the text displayed in the input area ;See Section 52, “Control Variables: Values Behind Widgets”.
validate You can use this option to specify a dynamic validation widget Callback function for text content. See Section 10.2, “Adding Validation Entries to Widgets”.
validatecommand See Section 10.2, “Adding widgets Verify Entry”.
width This option specifies the width of the input area as a number of characters . The actual width will be that number multiplied by the average width of the characters in the valid font. The default value is 20.
xscrollcommand If the widget has an associated horizontal scroll bar, please Set this option .set to the scrollbar method.

The ttk widget constructor does not support these options for the Tkinter Entry widget: .Entry

Table 41. Tkinter not available in ttkTkinter Entry option .Entry

background or bg background Use style configuration options; see Section 47, “Customizing and Creating ttk Themes and Styles”. bg does not support abbreviations.
borderwidth or bd Use style Configure this option.
disabledbackground Use style image as background option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
disabledforeground Use stylemap as foreground option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
foreground or fg Use style Configure this option.
highlightbackground To control the focus when the check button has no focus Display color, use stylemaps to control option highlightcolor; see Section 50.2, “TTK Stylemaps: Dynamic Appearance Changes”.
highlightcolor You can specify this by setting this option in the style Default focus highlight color. You can also control focus highlight color using style maps.
highlightthickness Configure this option using styles. This option may not be available for all themes.
insertbackground Not supported.
insertborderwidth Not supported.
insertofftime Not supported.
insertontime Not supported.
insertwidth Not supported.
readonlybackground Use style diagrams to control background option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
relief Configure this option using styles; see Section 47 “Customize and create ttk themes and styles”.
selectbackground Use style diagrams to control background option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
selectborderwidth Use style diagrams to control borderwidth code> option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
selectforeground Use style diagrams to control foreground option; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.

The methods .Entry on ttk include all methods described in Section 46, “Methods common to all ttk widgets”, and those described in Section 10, “Widgets” All methods on the Tkinter widget.

33. ttk.Frame

Like the Tkinter Frame widget, the ttk.Frame widget is a rectangular container for other widgets. To create a Frame widget as a child of the given widget parent:

w = ttk.Frame(parent, option = value, ...)

Options include:

Table 42. ttk.FrameOptions

borderwidth Use This option specifies the width of the border element; the default value is zero.
class_ You can provide the widget class name when creating this widget. This name can be used to customize the widget’s appearance; see Section 27, “Appearance Standardization”. Once a widget is created, the widget class name cannot be changed.
cursor Use this option to specify the appearance of the mouse cursor when it is over the widget; see Section 5.8, “Cursors”. The default value (empty string) specifies that the cursor is inherited from the parent widget.
height This option is to set the size of the frame height. If you want to force the frame to have a specific height, call .grid_propagate(0) on the widget; see Section 4.2, “Other Grid Management Methods”.
padding To create empty space inside the frame and outside the contained widget, set this option to the desired size. For example, padding='0.5i' will clear a half-inch wide area inside the frame and around the outside of the widget within the frame.
relief Specifies the relief style of the border; see Section 5.6, “Relief Style”. Unless you also increase borderwidth.
style Use this option to specify a custom widget style name; see Section 47, “Customizing and Creating ttk Themes and Styles”.
takefocus

Use this option to specify whether a widget is accessed during focus traversal; see Section 53, “Focus: Routing Keyboard Input”. Specify takefocus=True whether you want access to accept focus; specify takefocus=False whether the widget does not accept focus. The default value is an empty string; by default, the ttk.Frame widget does not receive focus.

width This option is to set the size of the frame width. If you want to force the frame to have a specific width, call .grid_propagate(0) on the widget; see Section 4.2, “Other Grid Management Methods”.

These options on the Tkinter Frame widget are notavailable as options on the ttk constructor. Frame:

Table 43. Tkinter Frame options not available in ttkTkinter

background or bg Configure this option using styles; see Section 47, “Customizing and Creating ttk Themes and Styles”.
highlightbackground To control the color of the focus highlight when the frame has no focus, use a style map to control the option highlightcolor; see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes”.
highlightcolor You can specify the default focus highlight color by setting this option in the style. You can also control focus highlight color using style maps.
highlightthickness Configure this option using styles. This option may not be available for all themes.
padx Not supported.
pady Not supported.

34. ttk.Label

The purpose of this widget is to display text, images, or both. Usually the content is static, but your program can change the text or images.

To create a ttk.Label widget as a child of the given widget parent item:

 w= ttk.Label( parent, option= value, ...)

Options include:

Table 44. ttk.LabelOptions

anchor If If the text and/or images are smaller than the specified width, you can use the anchor option to specify where they should be placed: tk.W, tk.CENTER, or tk.E means left alignment, center alignment or right alignment respectively. You can also specify this option using styles.
background Use this option to set the background color. You can also specify this option using styles.
borderwidth To add a border around the label, set this option to the width dimension. You can also specify this option using styles.
class_ You can provide the widget class name when creating this widget. This name can be used to customize the widget’s appearance; see Section 27, “Appearance Standardization”. Once the widget is created, the widget class name cannot be changed.
compound

If you provide both the text and image options, the compound option specifies how to display them.

'bottom' Displays an image below the text.
'image' Only displays the image, not the text.
'left' Displays the image to the left of the text.
'none' If there is, display the image, otherwise display the text . It’s the default value.
'right' Displays an image to the right of the text.
'text' Display text instead of an image.
'top' Displays an image above the text.
cursor Use this option to specify the appearance of the mouse cursor when it is over the widget; see Section 5.8, “Cursors”. The default value (empty string) specifies that the cursor is inherited from the parent widget.
font Use this option to specify the displayed font text style. You can also specify this option using styles.
foreground Use this option to specify the color of text to be displayed. You can also specify this option using styles.
image

This option specifies one or more images to be displayed in addition to or instead of text. The value must be the image specified in Section 5.9 “Images”. See the compound option above to see what happens when you provide both an image and text.

You can specify multiple images that will be displayed on a widget based on the widget’s state (see Section 50.2, “TTK Style Maps: Dynamic Appearance Changes” for a discussion of widget states). To do this, provide a tuple as the value of this option, where: (i0, s1 , i1, s2, i2, . ..)

  • i0 is the default image displayed on the widget.

  • For each pair of values after the first value, specify a state or combination of states and specify an image to display when the widget’s states match. sii1si

    Each state specifier can be a single state name, optionally starting with , or a sequence of such names. The specified widget must not be in this state. si'!'!

    For example, suppose you have three PhotoImage instances named , , and , and you include this option in the call to the constructor: im1im2im3Label

     self.w = ttk.Label(self, ...,
             image=(im1,
                    'selected', im2,
                    ('!disabled', 'alternate'), im3), ...)

    If the widget im2 is in this selected state, it will display the image. If not in state selected or disabled but in state alternate, image im3 is displayed. Otherwise it will display the image im1.

justify If the text you provide contains newline ( '\
'
) characters, this option specifies how each line is positioned horizontally: tk.LEFT code>Align left; tk.CENTER to the center; or tk.RIGHTright align each line. You can also specify this option using styles.
padding To add more space around all four sides of the text and/or image, set this option to the desired size. You can also specify this option using styles.
relief Set this option to Emboss style to create a 3D effect. You need to increase borderwidth for this effect to occur. You can also specify this option using styles.
style Use this option to specify a custom widget style name; see Section 47, “Customizing and Creating ttk Themes and Styles”.
takefocus

Use this option to specify whether the widget is accessed during focus traversal; see Section 53, “Focus: Routing Keyboard Input”. Specify takefocus=True whether you want access to accept focus; specify takefocus=False whether the widget does not accept focus.

The default value is an empty string; by default, the ttk.Label widget does not receive focus.

text The text string to be displayed in the widget.
textvariable An instance of StringVar (see Section 52, “Control Variables: Values Behind Widgets”); the text displayed on the widget will be its value. This option is ignored if both text and . textvariabletext
underline

You can request that a letter in a text string be underlined by setting this option to the position of that letter. For example, the options text='Quit' and underline=0 will underline Q.

Using this option does not change any functionality. If you want your application to react to the Q key or some variation (such as control-shift-Q), you need to set up the binding using the event system.

width

To specify a fixed width, set this option to a number of characters. To specify a minimum width, set this option to minus the number of characters. If this option is not specified, the label area will be large enough to accommodate the current text and/or image.

For text displayed in a proportional font, the actual width of the widget will be based on the average width of the characters in the font, rather than a specific number of characters.

This option can also be specified via styles.

wraplength If you set this option to a certain size, all text will be truncated to lines no longer than this size. This option can also be specified via styles.

The ttk constructor does not support the following options for the Tkinter version. Label.Label

Table 45. Tkinter not available in ttkTkinter Label option .Label

activebackground Use style diagrams to control background options; see Section 50.2, “TTK Style Map: Dynamic Appearance Changes”.
activeforeground Use style diagrams to control foregroundoptions.
bitmap Not supported.
disabledforeground Use stylemap as foreground option; see Section 50.2, “TTK Style Map: Dynamic Appearance Changes”.
height Not supported.
highlightbackground To control the color of the focus highlight when the label has no focus , use stylemaps to control the option highlightcolor; see Section 50.2, “TTK Stylemaps: Dynamic Appearance Changes”.
highlightcolor You can specify this by setting this option in the style Default focus highlight color. You can also control focus highlight color using style maps.
highlightthickness Configure this option using styles. This option may not be available for all themes.
padx Not supported.
pady Not supported.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137655 people are learning the system

syntaxbug.com © 2021 All Rights Reserved.