Teks sumber — English
Terjemahan — Bahasa Indonesia
When your Python application uses a class in Tkinter, for example, to create a widget, the :mod:`!tkinter` module first assembles a Tcl/Tk command string. It passes that Tcl command string to an internal :mod:`_tkinter` binary module, which then calls the Tcl interpreter to evaluate it. The Tcl interpreter will then call into the Tk and/or Ttk packages, which will in turn make calls to Xlib, Cocoa, or GDI.
While all operations in Tkinter are implemented as method calls on widget objects, you've seen that many Tcl/Tk operations appear as commands that take a widget pathname as its first parameter, followed by optional parameters, for example
In the official Tcl/Tk reference documentation, you'll find most operations that look like method calls on the man page for a specific widget (for example, you'll find the :meth:`~tkinter.ttk.Button.invoke` method on the `ttk::button <https://www.tcl-lang.org/man/tcl9.0/TkCmd/ttk_button.html>`_ man page), while functions that take a widget as a parameter often have their own man page (for example, `grid <https://www.tcl-lang.org/man/tcl9.0/TkCmd/grid.html>`_).
You'll also find that many Tkinter methods have compound names, for example, :meth:`~Misc.winfo_x`, :meth:`~Misc.winfo_height`, :meth:`~Misc.winfo_viewable`. You'd find documentation for all of these in the `winfo <https://www.tcl-lang.org/man/tcl9.0/TkCmd/winfo.html>`_ man page.
The options supported by a given widget are listed in that widget's man page, or can be queried at runtime by calling the :meth:`~Misc.config` method without arguments, or by calling the :meth:`~Misc.keys` method on that widget. The return value of these calls is a dictionary whose key is the name of the option as a string (for example, ``'relief'``) and whose values are 5-tuples.
Keep a reference to the variable for as long as the widget uses it -- for example by storing it as an attribute. A :class:`Variable` that is garbage collected removes its underlying Tcl variable, breaking the connection to the widget (see :class:`Variable`).
For example::
Sebagai contoh::
The *window manager* is the part of the desktop responsible for the title bar, border and controls drawn around each top-level window, and for such things as its title, position, size and icon. Tk gives access to these through the :class:`Wm` mixin, which is inherited by the :class:`Tk` root window and by every :class:`Toplevel`. You therefore call the window-manager methods directly on a top-level window. Each has a short name and an equivalent ``wm_``-prefixed name, for example :meth:`~Wm.title` and :meth:`~Wm.wm_title`.
This is any callable object. Most callbacks take no arguments. For example::
Colors can be given as symbolic names, such as ``"red"`` or ``"light blue"``, or as strings representing RGB values in 4 bit: ``"#RGB"``, 8 bit: ``"#RRGGBB"``, 12 bit: ``"#RRRGGGBBB"``, or 16 bit: ``"#RRRRGGGGBBBB"`` ranges, where R,G,B here represent any legal hex digit. Tk provides a portable set of symbolic names available on all platforms; they are matched ignoring case and spaces, so ``"light blue"``, ``"Light Blue"`` and ``"lightblue"`` are the same color. The full list of names, including the platform-specific names of the current system colors (for example ``"systemButtonFace"`` on Windows or ``"systemWindowBody"`` on macOS), is given in the :manpage:`colors(3tk)` manual page. :meth:`~tkinter.Misc.winfo_rgb` returns the red, green and blue components of a color.
The name of the mouse cursor to display while the pointer is over the widget. Tk provides a portable set of cursor names available on all platforms (for example ``"arrow"``, ``"watch"``, ``"cross"``, or ``"hand2"``); the standard X cursor names from :file:`cursorfont.h` may also be used, without the ``XC_`` prefix (so ``XC_hand2`` becomes ``"hand2"``). The full list of names, including the platform-specific ones, is given in the :manpage:`cursors(3tk)` manual page. You can also specify a bitmap and mask file of your own. On Windows a cursor file (:file:`.cur` or :file:`.ani`) may be used directly, giving its path preceded with an ``@``, as in ``"@C:/cursors/bart.ani"``.
Screen distances can be specified in either pixels or absolute distances. Pixels are given as numbers and absolute distances as strings, with the trailing character denoting units: ``c`` for centimetres, ``i`` for inches, ``m`` for millimetres, ``p`` for printer's points. For example, 3.5 inches is expressed as ``"3.5i"``.
Used for the *scrollregion* option of :class:`Canvas`. This is a string with four space-delimited elements, each of which is a legal distance (see above), or a sequence of four such elements. For example: ``"2 3 4 5"``, ``(2, 3, 4, 5)``, ``"3i 2i 4.5i 2i"`` and ``"3c 2c 4c 10.43c"`` are all legal regions.
is a string that denotes the target kind of event. Physical events use the ``<modifier-modifier-type-detail>`` form (for example ``"<Enter>"`` or ``"<Control-Button-1>"``); application-defined virtual events use double angle brackets, as in ``"<<Paste>>"``. (See the :manpage:`bind(3tk)` man page for details.)
The ``add`` parameter above only affects the bindings you make yourself. Every widget also inherits *class bindings* that implement its standard behavior -- for example a :class:`Text` widget binds :kbd:`Control-t` to transpose two characters. These are described in the bindings section of the widget's Tk man page (such as :manpage:`text(3tk)` or :manpage:`entry(3tk)`).
The image object can then be used wherever an ``image`` option is supported by some widget (for example, labels, buttons, menus). In these cases, Tk will not keep a reference to the image. When the last Python reference to the image object is deleted, the image data is deleted as well, and Tk will display an empty box wherever the image was used.
The exception raised when a call into the Tcl interpreter fails, for example when a widget is given an unknown option or an invalid value.
Generate the event *sequence* on this widget and arrange for it to be processed just as if it had come from the window system. *sequence* must be a single event pattern, such as ``'<Button-1>'`` or ``'<<Paste>>'``, not a concatenation of several. Keyword arguments specify additional fields of the event, for example *x* and *y* for the pointer position, or *when* to control when the event is processed; refer to the Tk ``event`` manual page for the full list.
Enter the event loop until all pending idle callbacks have been called. This updates the display of windows, for example after geometry changes, but does not process events caused by the user.
Wait until the visibility state of *window* changes, for example when it first appears on the screen, continuing to process events in the meantime. If *window* is omitted, this widget is used. This is typically used to wait for a newly created window to become visible before acting on it.
Query or set whether Tk's look and feel should strictly adhere to Motif. A true *boolean* value enables strict Motif compliance (for example, no color change when the mouse passes over a slider). Return the resulting setting.
The option database is referenced through the widget name or class. For example, if a :class:`Frame` widget named ``frame`` is to be made busy, the busy cursor can be specified for it by either of the calls::
Return a floating-point value giving the number of pixels in the widget corresponding to the screen distance *number* (for example, ``"2.0c"`` or ``"1i"``). The result may be fractional; for a rounded integer value use :meth:`winfo_pixels`.
Return the number of pixels in the widget corresponding to the screen distance *number* (for example, ``"2.0c"`` or ``"1i"``). The result is rounded to the nearest integer; for a fractional result use :meth:`winfo_fpixels`.
Return the Tcl/Tk patch level as a named tuple with the same five fields as :data:`sys.version_info`: *major*, *minor*, *micro*, *releaselevel* and *serial*. *releaselevel* is ``'alpha'``, ``'beta'`` or ``'final'``. Converting it to a string gives the version in the usual Tcl/Tk notation, for example ``'9.0.3'`` for a final release or ``'9.1b2'`` for a pre-release.
The :class:`!Wm` mixin provides access to the window manager, allowing an application to control such things as the title, geometry and icon of a top-level window, the way it is resized, and how it responds to window manager protocols. It is mixed into :class:`Tk` and :class:`Toplevel`, so its methods are available on every top-level window. Each method has two equivalent spellings: a short name and a ``wm_``-prefixed name (for example, :meth:`title` and :meth:`wm_title`). See also :ref:`tkinter-window-manager`.
Set or query the leader of a group of related windows. *pathName* gives the path name of the group leader; the window manager may, for example, unmap all windows in the group when the leader is iconified. An empty string removes the window from any group. With no argument, return the path name of the current group leader, or an empty string. :meth:`wm_group` is an alias of :meth:`!group`.
Set or query the bitmap used by the window manager for the window's icon. *bitmap* names a bitmap in one of the standard forms accepted by Tk; an empty string cancels the current icon bitmap. With no argument, return the name of the current icon bitmap, or an empty string. On Windows the *default* argument names an icon (for example an ``.ico`` file) applied to all top-level windows that have no icon of their own. :meth:`wm_iconbitmap` is an alias of :meth:`!iconbitmap`.
Set the titlebar icon for the window from one or more :class:`PhotoImage` objects given in *images*. Several images of different sizes (for example 16x16 and 32x32) may be supplied so that the window manager can choose an appropriate one. The image data is taken as a snapshot at the time of the call; later changes to the images are not reflected. If *default* is true, the icon is also applied to all top-level windows created in the future. On macOS only the first image is used. :meth:`wm_iconphoto` is an alias of :meth:`!iconphoto`.
Withdraw the window from the screen, unmapping it and causing the window manager to forget about it. If the window has never been mapped, it is instead mapped in the withdrawn state. It is sometimes necessary to withdraw a window and then re-map it (for example with :meth:`deiconify`) to make some window managers notice changes to window attributes. :meth:`wm_withdraw` is an alias of :meth:`!withdraw`.
Finish loading and initializing the Tk subsystem. This is needed only when the interpreter was created without Tk (for example through :func:`Tcl`); it is called automatically when *useTk* is true.
Report a callback exception. This is called when an exception propagates out of a Tkinter callback; *exc*, *val* and *tb* are the exception type, value and traceback as returned by :func:`sys.exc_info`. The default implementation prints a traceback to :data:`sys.stderr`. It can be overridden to customize error handling, for example to display the traceback in a dialog.
Most of the methods take one or more *index* arguments that identify a position within the text. As described in the Tk ``text`` manual page, an index is a string consisting of a base, optionally followed by one or more modifiers. The base may be ``'line.char'`` (line *line*, character *char*, where lines are counted from 1 and characters within a line from 0; ``'line.end'`` refers to the newline ending the line), ``'end'`` (the position just after the last newline), the name of a mark, ``'tag.first'`` or ``'tag.last'`` (the first character tagged with *tag*, or the position just after the last such character), the name of an embedded image or window, or ``@x,y`` (the character covering pixel coordinates *x*, *y* in the widget). A modifier such as ``'+5 chars'``, ``'-3 lines'``, ``'linestart'``, ``'lineend'``, ``'wordstart'`` or ``'wordend'`` adjusts the index relative to its base; several modifiers may be combined and are applied from left to right, for example ``'insert wordstart - 1 c'``.
When a :class:`!Variable` is garbage collected, its Tcl variable is unset. Keep a reference to it for as long as a widget is linked to it, for example by storing it as an attribute rather than in a local variable. Otherwise Tk recreates the Tcl variable to keep the widget working, but it is never unset again, leaking one Tcl variable per dropped wrapper.
Return the type of the image, that is the value of *imgtype* with which it was created (for example ``'photo'`` or ``'bitmap'``).
Set pixels of the image to the colors given in *data*, which must be a string or a nested sequence of horizontal rows of pixel colors (for example ``"{red green} {blue yellow}"``).
The special tag ``'all'``, which matches every item of a :class:`Canvas` or every character of a :class:`Text` (for example ``canvas.delete(ALL)``).