Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Tkinter

From Wikipedia, the free encyclopedia
Python binding to the Tk GUI toolkit
Tkinter
The IDLE Python editor
TheIDLE Python editor
LicensePython license
Websitewiki.python.org/moin/TkInter Edit this on Wikidata

Tkinter is abinding to theTkGUI toolkit forPython. It is the standard Python interface to the Tk GUI toolkit,[1] and is Python'sde facto standard GUI.[2] Tkinter is included with standardLinux,Microsoft Windows andmacOS installs of Python.

The nameTkinter comes fromTk interface. Tkinter was written bySteen Lumholt andGuido van Rossum,[3] then later revised by Fredrik Lundh.[4]

Tkinter isfree software released under aPython license.[5]

Description

[edit]

As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a completeTclinterpreter embedded in the Pythoninterpreter. Tkinter calls are translated into Tcl commands, which are fed to this embedded interpreter, thus making it possible to mix Python and Tcl in a single application.

There are several popular GUI library alternatives available, such asKivy,Pygame,Pyglet,PyGObject,PyQt,PySide, andwxPython.

Definitions

[edit]

Window

[edit]

This term has different meanings in different contexts, but in general it refers to a rectangular area somewhere on the user's display screen.

Top-level window

[edit]

A window which acts as a child of the primary window. It will be decorated with the standard frame and controls for thedesktop manager. It can be moved around the desktop and can usually be resized.

Widget

[edit]

The generic term for any of the building blocks that make up an application in a graphical user interface.

  • Core widgets:
    • Containers:
      • frame
      • labelframe
      • toplevel
      • paned window.
    • Buttons:
      • button
      • radiobutton
      • checkbutton (checkbox)
      • menubutton.
    • Text widgets:
      • label,
      • message
      • text
    • Entry widgets:
      • scale
      • scrollbar
      • listbox
      • slider
      • spinbox
      • entry (singleline)
      • optionmenu
      • text (multiline)
    • Canvas (vector and pixel graphics)

Tkinter provides three modules that allow pop-up dialogs to be displayed: tk.messagebox (confirmation, information, warning and error dialogs), tk.filedialog (single file, multiple file and directory selection dialogs) and tk.colorchooser (colour picker).

Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5.[6][7] This allows Tk widgets to be easily themed to look like the native desktop environment in which the application is running, thereby addressing a long-standing criticism of Tk (and hence of Tkinter). Some widgets are exclusive to ttk, such as the combobox, progressbar, treeview, notebook, separator and sizegrip.[8]

Frame

[edit]

In Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame is a rectangular area that can contain other widgets.

Child and parent

[edit]

When any widget is created, a parent–child relationship is created. For example, if you place a text label inside a frame, the frame is the parent of the label.

Minimal application

[edit]

Below is a minimal Python 3 Tkinter application with one widget:[9]

#!/usr/bin/env python3fromtkinterimport*root=Tk()# Create the root (base) windoww=Label(root,text="Hello, world!")# Create a label with wordsw.pack()# Put the label into the windowroot.mainloop()# Start the event loop

For Python 2, the only difference is the word "tkinter" in the import command will be capitalized to "Tkinter".[10]

Process

[edit]

There are four stages to creating a widget[11]

Create
Create it within a frame
Configure
Change the widget's attributes.
Pack
Pack it into position so it becomes visible. Developers also have the option to use .grid() (row=int, column=int to define rows and columns to position the widget, defaults to 0) and .place() (relx=int or decimal, rely=int or decimal, define coordinates in the frame, or window).
Bind
Bind it to a function or event.

These are often compressed, and the order can vary.

Simple application

[edit]

Using theobject-oriented paradigm in Python, a simple program would be (requires Tcl version 8.6, which is not used by Python on MacOS by default):

#!/usr/bin/env python3importtkinterastkclassApplication(tk.Frame):def__init__(self,root=None):tk.Frame.__init__(self,root)self.grid()self.createWidgets()defcreateWidgets(self):self.medialLabel=tk.Label(self,text="Hello World")self.medialLabel.config(bg="#00ffff")self.medialLabel.grid()self.quitButton=tk.Button(self,text="Quit",command=self.quit)self.quitButton.grid()app=Application()app.root=tk.Tk()app.root.title("Sample application")app.mainloop()
  • line 1:Hashbangdirective to the program launcher, allowing the selection of an appropriate interpreter executable, when self-executing.[12]
  • line 2: Imports the tkinter module into your program's namespace, but renames it as tk.
  • line 5: The application class inherits from Tkinter's Frame class.
  • line 7: Defines the function that sets up the Frame.
  • line 8: Calls the constructor for the parent class, Frame.
  • line 12: Defining the widgets.
  • line 13: Creates a label, named MedialLabel with the text "Hello World".
  • line 14: Sets the MedialLabel background colour to cyan.
  • line 15: Places the label on the application so it is visible using the grid geometry manager method.
  • line 16: Creates a button labeled “Quit”.
  • line 17: Places the button on the application. Grid, place and pack are all methods of making the widget visible.
  • line 20: The main program starts here by instantiating the Application class.
  • line 21: Creates main window app.root as a Tk object.
  • line 22: This method call sets the title of the window to “Sample application”.
  • line 23: Starts the application's main loop, waiting for mouse and keyboard events.

See also

[edit]
  • IDLE — integrated development environment in Python, written exclusively using Tkinter

References

[edit]
  1. ^"Tkinter — Python interface to Tcl/Tk — Python v2.6.1 documentation". Retrieved2009-03-12.
  2. ^"Tkinter - Pythoninfo Wiki".
  3. ^tkinter—Python interface to Tcl/Tk—Python 3.9.10 Documentation
  4. ^Shipman, John W. (2010-12-12),Tkinter reference: a GUI for Python, New Mexico Tech Computer Center, retrieved2012-01-11
  5. ^"Tkinter - Tkinter Wiki". Archived fromthe original on 2013-11-13. Retrieved2013-11-13.
  6. ^"Python issue #2983, "Ttk support for Tkinter"".
  7. ^"Python subversion revision 69051, which resolves issue #2983 by adding the ttk module".
  8. ^"Tkinter ttk widgets - Python Tutorial".CodersLegacy. Retrieved2022-01-13.
  9. ^"Tkinter 8.5 reference: a GUI for Python".
  10. ^Fleck, Dan."Tkinter – GUIs in Python"(PDF).CS112. George Mason University. Retrieved18 August 2018.
  11. ^Klein, Bernd."GUI Programming with Python: Events and Binds".www.python-course.eu. Retrieved18 August 2018.
  12. ^"PEP 397 — Python launcher for Windows — Python.org". Retrieved2017-06-07.

External links

[edit]
Low-level platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
OnWindows
OnUnix
OnBeOS,Haiku
OnAndroid
CLI
Low Level Cross-platform
CLI
C
Java
High-level, platform-specific
OnAmigaOS
OnClassic Mac OS,macOS
Object Pascal
Objective-C,Swift
C++
CLI
OnWindows
CLI
C++
Object Pascal
OnUnix andX11
High-level, cross-platform
C
C++
Objective-C
CLI
Adobe Flash
Go
Haskell
Java
JavaScript
Common Lisp
Lua
Pascal
Object Pascal
Perl
PHP
Python
Ruby
Tcl
XML
shell
Dart
Retrieved from "https://en.wikipedia.org/w/index.php?title=Tkinter&oldid=1333190877"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp