Teks sumber — English
Terjemahan — Bahasa Indonesia
Ready-made subclasses cover the common types: :class:`StringVar`, :class:`IntVar`, :class:`DoubleVar` and :class:`BooleanVar`. Pass one as a widget's ``textvariable`` (or ``variable``) option, then read and update it with :meth:`~Variable.get` and :meth:`~Variable.set`; the widget tracks it with no further work on your part.
Tk option data types
Many widget options documented in the reference accept values of a small number of common types, described here. The standard options and their types are described in the :manpage:`options(3tk)` manual page.
The bind method from the widget command allows you to watch for certain events and to have a callback function trigger when that event type occurs. The form of the bind method is::
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.)
is optional, either ``''`` or ``'+'``. Passing an empty string denotes that this binding is to replace any other bindings that this event is associated with. Passing a ``'+'`` means that this function is to be added to the list of functions bound to this event type.
type
tipe
a text string that is pattern matched against the label of the menu entry, as scanned from the top of the menu to the bottom. Note that this index type is considered after all the others, which means that matches for menu items labelled ``last``, ``active``, or ``none`` may be interpreted as the above literals, instead.
Either type of image is created through either the ``file`` or the ``data`` option (other options are available as well).
The :meth:`!getvar` and :meth:`!setvar` methods give direct access to Tcl variables. In most code you will instead use a :class:`Variable` subclass such as :class:`StringVar` or :class:`IntVar`, which wraps a Tcl variable and converts its value to and from a Python type.
Return the available image types, such as ``'photo'`` and ``'bitmap'``.
If *id* is given, it must identify a callback that has not yet run or been cancelled, and the return value is a tuple ``(script, type)``, where *script* refers to the function to be called and *type* is either ``'idle'`` or ``'timer'``. A :exc:`TclError` is raised if *id* does not exist.
Return the contents of the current X selection. The keyword argument *selection* names the selection and defaults to ``PRIMARY``. The keyword argument *type* specifies the form in which the data is to be returned (the desired conversion target), an atom name such as ``'STRING'`` or ``'FILE_NAME'``; it defaults to ``STRING``, except on X11, where ``UTF8_STRING`` is tried first and ``STRING`` is used as a fallback. The *displayof* keyword argument names a widget that determines the display from which to retrieve the selection, and defaults to this widget.
Register *command* as a handler to supply the X selection owned by this widget when another application requests it. When the selection is retrieved, *command* is called with two arguments, the starting character offset and the maximum number of characters to return, and must return at most that many characters of the selection starting at that offset; for very long selections it is called repeatedly with increasing offsets. The keyword argument *selection* names the selection (default ``PRIMARY``) and the keyword argument *type* gives the form of the selection that the handler supplies (such as ``'STRING'`` or ``'FILE_NAME'``, default ``STRING``).
Append *string* to the Tk clipboard and claim ownership of the clipboard on this widget's display. Before appending, the clipboard should be emptied with :meth:`clipboard_clear`; all appends should be completed before returning to the event loop so that the clipboard is updated atomically. The keyword argument *type* specifies the form of the data, an atom name such as ``'STRING'`` or ``'FILE_NAME'`` (default ``STRING``), and the keyword argument *format* specifies the representation used to transmit it (default ``STRING``). The *displayof* keyword argument names a widget that determines the target display, and defaults to this widget. The contents can be retrieved with :meth:`clipboard_get` or :meth:`selection_get`.
Retrieve data from the clipboard on this widget's display. The keyword argument *type* specifies the form in which the data is to be returned, an atom name such as ``'STRING'`` or ``'FILE_NAME'``; it defaults to ``STRING``, except on X11, where ``UTF8_STRING`` is tried first and ``STRING`` is used as a fallback. The *displayof* keyword argument names a widget that determines the display, and defaults to the root window of the application. This is equivalent to ``selection_get(selection='CLIPBOARD')``.
*type*
The window type, or a list of types in order of preference, that the window manager should use to interpret the window, such as ``'dialog'`` or ``'splash'``.
Tk 8.6 added the *type* attribute, and Tk 9.0 added the *appearance*, *class*, *stylemask*, *tabbingid* and *tabbingmode* attributes.
Make *widget* a stand-alone top-level window, decorated by the window manager with a title bar and so on. Only :class:`Frame`, :class:`LabelFrame` and :class:`Toplevel` widgets may be used (the :mod:`tkinter.ttk` versions are **not** accepted); passing any other widget type raises an error. :meth:`wm_manage` is an alias of :meth:`!manage`.
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.
Create a new item of the corresponding type and return its integer id. Each method is called as ``create_TYPE(coord..., **options)``: the leading positional arguments give the coordinates that define the item (as separate numbers, as a single sequence of numbers, or as coordinate pairs), and the keyword arguments set item-specific options. Coordinates and screen distances may be given as numbers (interpreted as pixels) or as strings with a unit suffix (``'m'``, ``'c'``, ``'i'`` or ``'p'`` for millimetres, centimetres, inches or printer's points), but are always stored and returned in pixels.
The item types are: ``arc`` (an arc-shaped region that is a section of an oval, defined by two diagonally opposite corners ``x1, y1, x2, y2`` of the enclosing rectangle); ``bitmap`` (a two-color bitmap positioned at a point ``x, y``); ``image`` (a Tk image positioned at a point ``x, y``); ``line`` (a line or curve through the points ``x1, y1, ..., xn, yn``); ``oval`` (a circle or ellipse inscribed in the rectangle ``x1, y1, x2, y2``); ``polygon`` (a closed polygon through the points ``x1, y1, ..., xn, yn``); ``rectangle`` (a rectangle with corners ``x1, y1, x2, y2``); ``text`` (a string of text positioned at a point ``x, y``); and ``window`` (a child widget embedded in the canvas at a point ``x, y``, specified with the *window* option).
Most item types accept a common set of *standard item options*, plus a few options specific to each type. Option names are passed as keyword arguments, without the leading hyphen.
The color used to fill the item's interior, or to draw a *line* item or the characters of a *text* item. An empty string (the default for all types except *line* and *text*) leaves the item unfilled.
The following item types support additional options.
``oval`` and ``rectangle`` items have no type-specific options; they use only the standard item options.
Return the type of the item given by *tagOrId* (the first matching item if it matches several), such as ``'rectangle'`` or ``'text'``, or ``None`` if *tagOrId* does not match any item.
Add a new entry to the bottom of the menu. *itemType* is one of ``'command'``, ``'cascade'``, ``'checkbutton'``, ``'radiobutton'`` or ``'separator'`` and determines the type of the new entry; the remaining options configure it. The :meth:`!add_command`, :meth:`!add_cascade`, :meth:`!add_checkbutton`, :meth:`!add_radiobutton` and :meth:`!add_separator` convenience methods call this method with the corresponding *itemType*.
The entry is configured by the following options, although not every option applies to every entry type (a separator accepts none of them):
Same as :meth:`add`, except that the new entry is inserted just before the entry given by *index* instead of being appended to the end of the menu. *itemType* is one of ``'command'``, ``'cascade'``, ``'checkbutton'``, ``'radiobutton'`` or ``'separator'``. The :meth:`!insert_command`, :meth:`!insert_cascade`, :meth:`!insert_checkbutton`, :meth:`!insert_radiobutton` and :meth:`!insert_separator` convenience methods call this method with the corresponding *itemType*.
Query or modify the configuration options of the entry given by *index*. This mirrors :meth:`~Misc.configure`, except that it applies to an individual entry rather than to the menu as a whole. With no options, it returns a dictionary describing the current options of the entry; otherwise it sets the given options. The supported options are those accepted by :meth:`add` for the entry's type. :meth:`entryconfig` is an alias of :meth:`!entryconfigure`.
Return the type of the entry given by *index*: one of ``'command'``, ``'cascade'``, ``'checkbutton'``, ``'radiobutton'``, ``'separator'`` or ``'tearoff'`` (for the tear-off entry).
*master* is the widget whose Tcl interpreter owns the variable; if omitted, the default root window is used. *value* is the initial value; if omitted, a type-specific default is used. *name* is the name of the variable in the Tcl interpreter; if omitted, a unique name of the form ``'PY_VARnum'`` is generated. If *name* matches an existing variable and *value* is omitted, the existing value is retained.
In most cases you should use one of the typed subclasses below -- :class:`StringVar`, :class:`IntVar`, :class:`DoubleVar` or :class:`BooleanVar` -- rather than :class:`!Variable` directly.
Return the current value of the variable. For the base class the value is returned as a string; the typed subclasses convert it to the appropriate Python type.
Base class for Tk images. *imgtype* is the Tk image type, one of ``'photo'`` or ``'bitmap'``. An image is a named object that can be displayed by widgets through their *image* option; deleting all references to the :class:`!Image` object deletes the underlying Tk image. Usually you create a :class:`PhotoImage` or :class:`BitmapImage` rather than an :class:`!Image` directly.
Modify one or more configuration options of the image. The valid options depend on the image type; see :class:`PhotoImage` and :class:`BitmapImage`. :meth:`config` is an alias of :meth:`!configure`.
Return the type of the image, that is the value of *imgtype* with which it was created (for example ``'photo'`` or ``'bitmap'``).
A full-color image (the Tk ``photo`` image type), stored internally with a varying degree of transparency per pixel. It can read and write GIF, PPM/PGM and (in Tk 8.6 and later) PNG files, read SVG files (in Tk 9.0 and later), and be drawn in widgets. Inherits from :class:`Image`.
A two-color image (the Tk ``bitmap`` image type) created from an X11 bitmap. Each pixel displays a foreground color, a background color, or nothing (producing a transparent effect). Inherits from :class:`Image`.
A container for the attributes of an event passed to a callback bound with :meth:`Misc.bind`. An :class:`!Event` instance has the following attributes, each corresponding to a field of the underlying Tk event; depending on the event type, some attributes may be set to the string ``'??'`` to indicate that they are not meaningful. See :ref:`bindings-and-events`.
The character typed, as a string (for key events).
The :class:`EventType` of the event.
An :class:`enum.StrEnum` enumerating the Tk event types, used as the value of :attr:`Event.type`. Its members include, among others, ``KeyPress``, ``KeyRelease``, ``ButtonPress``, ``ButtonRelease``, ``Motion``, ``Enter``, ``Leave``, ``FocusIn``, ``FocusOut``, ``Configure``, ``Map``, ``Unmap``, ``Expose``, ``Destroy`` and ``MouseWheel``.
Return the available image types (such as ``'photo'`` and ``'bitmap'``) in the default root's interpreter.
Menu entry types, used as the *itemType* argument of :meth:`Menu.add` and :meth:`Menu.insert`.
Tk Option Data Types