Python's standard gui:tkinter components

Source: Internet
Author: User
Tags modifier pack

Label component: The label component is used to display text and images, and uses double-buffering usage: You can use the label component to specify what you want to display (text, bitmap, or picture):
 from Import *    = Tk ()  = Label (master, text="Hello fishc! " )  w.pack ()   // for automatic size  adjustment
You can also set the foreground and background colors of a label by foreground (FG) and background (BG) labels can display multiple lines of text, you can use the newline text character directly or with the Wraplength option, and when the text wraps, you can use The anchor and justify options make the text appear as you wish. You can use label to display photoimage and BitmapImage objects (be sure to keep a reference to a picture object in case the Python garbage collection mechanism recycles) the label ( master=none,**options) master--Parent Control **options--Component Options button control: The button component is used to implement a wide variety of buttons that are often used in toolbar, application windows, and the dialog box that represents accept or reject disables the button:
b = button (Master, text="help"
The label's size is just right for its content, and using the PADX and Pady options adds extra spacing between the button's contents and the Border button to display multiple lines of text, which you can do directly using the line break or using the Wraplength option. When the text wraps, you can use the anchor and justify and PADX options to make the text appear as if you want it to sag: Change the default value of the relief option raised to sunken b.config (Relief=sunken) button (master=none,**options) master--parent Control **options--Component Options     method: Flash ()--Refresh Button component, The method redraws the button component several times, invoke ()-invokes the function or method specified by the command option in the button, and returns the return value of the function-if the button's state is disabled (not available) or the command option is not specified, The method is invalid  frame the control frame component is a rectangular area on the screen. Frame is primarily used as the Framework Foundation for other components, or to provide spacing for other components The frame component is primarily designed to group other components in complex layouts, also to fill gaps, and as a base class for implementing advanced Components frame (master=none,**options) master--parent Control **options--Component Options   checkbutton component: Checkbutton (Multi-select button) The Checkbutton component that is used to determine whether a selection is selected is used to use Checkbutton as a two button, you need to create a tkinter variable to hold the button's state   default, and the variable option set to 1 for the selected state. Conversely, when set to 0 in a class, it is a good idea to store the value of the variable option as a property  checkbutton (master=none,**options) master--parent Control **options--Component Options      Method: Deselect ()--cancels the selected state of the Checkbutton component, which is set variable to Offvalueselect ()--Sets the Checkbutton component to the selected state. That is, set variable to Onvalueflash ()--Refreshes the Checkbutton component, which redraws the Checkbutton component several times   radiobutton component: Radiobutton (radio button component is used to implement a multiple-selection problem. The Radiobutton component is an issue that is used to implement multiple choices, and it is almost always used in groups where all members have common variables  Radiobutton  components that are very similar to  Checkbutton  components in order to implement their " Radio behavior to ensure that all the buttons in a group are  VARIABLE  options use the same variable and use the  value  option to specify what value each button represents RadioButton (master=none,**options) master--parent control **options--Component Options    labelframe components: The Labelframe component is a variant of the frame component, by default, Labelframe draws a border around its subcomponents and a heading for the component grouping, which requires first creating a  labelframe, and then add the subcomponents as usual. The labelframe  component automatically draws a border to enclose the subcomponents and displays a text caption above them labelframe (master=none,**options) master--parent control **options--Component Options   entry components: The entry component is typically used to get user input text using code to add text to the entry component, which can be used with the insert () method. If you want to replace the current text, you can first use the Delete () method, and then use the Insert () method to implement:  gets the text of the current input box can use the Get () method entry () (Master=none,**options) master--the parent control **options--Component Options     methods: Delete (first,last=none)-Delete all contents of the parameter first to the last range (contains first and last)--if the last argument is omitted, means delete the option specified by the first parameter--delete all contents of the input box using delete (0,end) get ()--Get the contents of the current input box ICursor (index)-- Move the cursor to the position specified by the index parameter-this also sets the value of the Insert index--Returns the ordinal of the option corresponding to the index parameter insert (INDEX,TEXT)-- Inserts the contents of the text parameter into the position specified by the index parameter-inserts the string specified by the text parameter into the specified position of the cursor using Insert (Insert,text) (End,text) Inserts the string specified by the text parameter at the end of the input box  entry component is to support the validation of input content, such as the requirement to enter a number, you enter the letter is illegal. This function is implemented by setting the Validate, ValidatEommand and Invalidcommand options. The function specified by the  invalidcommand option is called only if the return value of Validatecommand is false.  listbox Component: The ListBox (list box) component is used to display a selection list. When you create a ListBox component, he is empty, so add one or more lines of text first. Add text using the Insert () method, which has two parameters: the first parameter is the index number to insert, and the second argument is the inserted string. The index number is usually the ordinal of the item (0 is the ordinal of the first item in the list) the  listbox component provides four different selection modes based on the SelectMode option: Single, BROWSE (also radio, but with the mouse or arrow keys to change the options directly), Multiple (multiple selection) and extended (also multiple selection, but you need to hold down the shift or CTRL key or drag the mouse to achieve). By default, browse installs a vertical scroll bar on a component, which requires:
    1. Sets the Yscrollbarcommand option for the component to the set () method of the ScrollBar component;
    2. sets the command option for the scrollbar component to the Yview () method for the component
 list (master=none,**options) master--parent Control **options--Component Options  scrollbar component: The Scrollbar (scrollbar) component is used to scroll through the visible range of some components, Can be divided into vertical scrollbar and horizontal scrollbar according to direction in order to install the vertical scroll bar on a component, you need to do two things:1.  set the  yscrollbarcommand  option for the component to  Scrollbar  The  set ()   method of the component 2.  the  command  option for the set  Scrollbar  component is the  yview ()   method for the component.   When the visual range of a ListBox component changes, the ListBox component notifies the Srollbar component by calling the set () method. (The Yview () method of the ListBox component is called automatically when the user operates the scroll bar) Scrollbar (master=none,**options) master--parent Control **options--Component Options  scale Component: Scale component looks like a Scrollbar component with data. The scale component allows you to select a range of numbers by sliding the slider to create a range of scale components that need to have its from and to two options to control the split frequency (step) via the resolution option, set the tick by Tickinterval option   Scale (master=none,**options) master--parent Control **options--Component Options   text () component: the text () component is used to display and manipulate multiple lines of text. Because it is suitable for displaying plain text or formatted text (different fonts, embedded images, display links, even HTML with CSS formatting) is often used as a simple text editor and web browser   when creating a text component, there is no content, insert () Method and insert or end index number inserts the content       inserts an object into the text component, you can use the Windows_create () and Image_create () methods: Windows_ Create (index,**options)--Embed a Window object at the location specified by the index parameter--supports two ways in textEmbedded Window object in component: See the description of the Create option and window options below--optional create: Set a callback function to create the embedded window component, which has no parameters, And you must create a subassembly of text and return--optional window: Specifies a window component that has already been created, which must be a subassembly of the text component Image_create (INDEX,CNF={},**KW)-- Embeds an Image object at the location specified by the index parameter-the image object must be an tkinter photoimage or bitmapimage instance   Delete the contents of the text component can use Delete () The Text.delete method (1.0,end) text component also supports the "Redo" and "undo" actions, by setting the Undo option to true to turn on the "undo function" of the text component, and then using the Edit_undo () method to implement the undo operation, using the Edit_ The Redo () method implements a "restore" operation by default each time the complete operation is put into the stack (the operation is a complete operation before each focus switch in Tkinter, the user presses the ENTER key, the delete/insert operation, etc.)   Indexs usage: Indexs is used to point to the position of text in the text component, as in the Python sequence index      "Line.column" represents the row and column numbers of the index position as a string (in the middle with "."). Delimited) the "line.end" line number plus the format of the string ". End" is represented as the position of the last character of the line insert corresponds to the position of the cursor where current corresponds to the nearest position of the mouse coordinates (knowing to release the mouse to respond) End corresponds to the next position of the left and last character of the text buffer of the textual component expressions is used to modify the index of any format, and the expression to implement the modification in the form of a string  marks usage marks is usually embedded in the text of the textual component that is not visible to the object.   You can use the Tag_config () method to set the tags style if the same range of text with multiple tags, and set the same options, then the new create love your tag style will overwrite the older tag can also be used tag_raise ()And Tag_lower ()Methods to control the priority among tags also tags support event binding, using the method of Tag_bind () text (master=none,**options) master--parent Control **options--Component Option method: Dump ( INDEX1,INDEX2=NONE,COMMAND=NONE,**KW)--Returns the content between Index1 and Index2-The returned value is a list of 3 ancestors (keywords, values, indexes), the order of key parameters is: All,image, mark,tag,text,window--Default keyword is ' All ', indicating that all keywords are selected-if you need to filter individual keywords, you can use Dump (index1,index2,image=true,text=true) This form invocation--If the command function is specified, is called once for each ternary group in the list as a parameter the canvas component: Canvas is a generic component that is typically used to display and edit graphics. Can be used to draw line segments, circles, polygons, and even draw other component usages: drawing objects on a canvas component, you can use CREATE_XXX (XXX) to indicate object types, such as line lines, rectangles rectangle, text texts, etc. Objects added to the canvas remain. If you want to change you can use the coords (), Itemconfig (), and Move () methods to move objects on the canvas or delete directly with the Delete () method to display text on the canvas, using the Create_text () Method: Use the Create_oval () method to draw an ellipse (or circle), and the parameter is to draw a bounding rectangle (Tkinter automatically draws an ellipse within the rectangle), and you can use the Create_polygon () method to support the object in the canvas component
    • Arc (arc, chord, or fan)
    • Bitmap (built-in bitmap file or XBM formatted file)
    • Image (instance object of Bitmaoimage or Photoimage)
    • Line (lines)
    • Oval (Oval)
    • Polygon (Polygon)
    • Rectangle (Rectangle)
Where strings, fans, ovals, circles, and rectangles are enclosed shapes that have contour lines and fill colors, But all can be set to transparent canvas objects are displayed in the order by default the new Canvas object overrides the overlapping part of the old Canvas object to specify the canvas object The canvas component provides a central way for you to specify the canvas object
    • Item handles
    • Tags
    • All
    • Current
Item handles is actually a label that is used to specify a canvas object for the shape number tags that are loaded on the canvas object. A canvas object associated with multiple tags (canvas pre-defined two Tags:all and current) all means that all canvas objects in the canvas component current represent the canvas object under the mouse   method: Create_arc (bbox,* *options)--Create a fan, bow, or arc based on bbox (X1,Y1,X2,Y2)--The newly created canvas object is at the top of the display list-the table below the id--of the canvas object that was created successfully lists the specific meanings of each options option    menu components: The menu component is used to implement top-level menus, drop-down menus, and pop-up menu usages: Create a top-level menu, you need to create a menu instance first, and then use the Add () method to add commands and other submenus: Create a drop-down menu method as well, But the difference is that the last need to add to the main menu (instead of the window)  menu (master,**options) master--parent Control **options--Component Option Method: Add (type,**options)-- The type parameter specifies the type of menu to add, which can be: "Command", "Cascade", "Checkbutton", "RadioButton" or "separator"--you can also set the properties of the menu via the Options option. The following table lists the options you can use and what they mean Add_cascade (**options)--Add a parent menu Add_command (**options)--Add a plain Command menu item Add_checkbutton (* * Options)--Add a multi-select menu item Add_separator (**options)--Add a split line  menubutton component: The Menubutton component is a button associated with the menu component. It can be placed anywhere in the window and, when pressed, pops up the drop-down menu usage: Create a Menubutton component and create a menu component associated with it  optionmenu (select menu) In fact, the drop-down menu of the revision to get the user's choice of content, Using the Get () method of the Tkinter variable to add multiple options to the selection menu, the option is now written into a meta-ancestor, and then the meta-ancestral into the asterisk as a formal parameter is "packaged", instead, as an argument is the role of "unpacking"   Specify that a function can accept any number of arguments by adding an asterisk (*) or two asterisk (* *) before the parameter
    • When the parameter shape is (*args), any actual attendees passed to the parameter are packaged as a single ancestor (tuple) by location
    • When the parameter shape is (**args), any key that is passed to the function = value is wrapped in a dictionary (dict)
When an asterisk (*) is used as an argument, the function is called by unpacking the parameter: packages are packaged, unpacking events by adding an asterisk (*) or two asterisks (* *) to the arguments before the argument is bound to the event and Bindings:tkinter provides a powerful mechanism to allow you to handle time freely, for each component can bind a function or method to a specific event through the bind () method Widget.bind (event, Handler)When the event that is triggered satisfies the event that the component is bound to, Tkinter takes the time object (event) to call the handler () method to accept the keyboard time (Key) only when the component has the focus, and to get the focus with Focus_set () <motion > Events capture a sequence of motion trace events on a component of the mouse: Tkinter uses a mechanism called an event sequence to allow user-defined events, and the user needs to use the bind () method to bind a specific time series to a custom method. A time series is represented as a string that can represent one or more associated event syntax:<modifier-typr-detail>
    • The sequence of events is enclosed in angle brackets (<...>)
    • The content of the type section is most important and is typically used to describe common event types, such as mouse clicks or keyboard keystrokes.
    • Modifier part of the content is optional, it is usually used to describe the combination of keys, such as CTRL + C,shift + left mouse button click
    • The contents of the detail section are optional and are typically used to describe specific keys, such as Button-1 for the left mouse button
Type:buttonrelease than Button good, afraid of user delay mouse keypress can be abbreviated to Keymodifier:event object when Tkinter to callback your defined function, will take the event object (as a parameter) to call The message Component message component is a label component variant that is used to display a multi-jersey text message. The message component can wrap and adjust the size of the text to fit a given dimension Spinbox component: The Spinbox component is a variant of the entry component, The difference between selecting a and entry component from some fixed value is to use the Spinbox component to specify the content that is allowed to be entered by the User Panedwindow component Panedwindow component is a control management component. Repeated with frame components provides a framework for components you can use the Panedwindow component when you need to provide a multi-space framework that can be adjusted by the user. The Panedwindow component generates a separate pane for each subcomponent, and the user can freely adjust the pane's size pane to set the split line using the Sashrelief toplevel component: The TopLevel component is typically used to display additional windows, dialogs, and other pop-up windows Pack, grid, and place are used to manage the layout of all components that are under the same parent component, where:
    • Pack is the component in order of addition
    • Grid arranges components in row/column form
    • Place allows programmers to specify the size and location of the component
Compared to the grid manager, pack is more suitable for the arrangement of a small number of components, but it is easier to use (. Pack ()). If you need to create a relatively complex layout structure, the recommendation is to use multiple frame structures, or use the grid manager to implement

Python's standard gui:tkinter components

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.