Tkinter




Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit,[1] and is Python's de facto standard GUI.[2] Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python.


The name Tkinter comes from Tk interface. Tkinter was written by Fredrik Lundh.[3]


Tkinter is free software released under a Python license.[4]




Contents






  • 1 Description


    • 1.1 Some definitions


    • 1.2 Window


    • 1.3 Top Level Window


    • 1.4 Widget


    • 1.5 Frame


    • 1.6 Child and parent




  • 2 A minimal application


    • 2.1 Process


    • 2.2 Simple application




  • 3 ttk extension


  • 4 References


  • 5 External links


    • 5.1 GUI systems based on Tkinter







Description


As with most other modern Tk bindings, Tkinter is implemented as a Python wrapper around a complete Tcl interpreter embedded in the Python interpreter. 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.


Python 2.7 and Python 3.1 incorporate the "themed Tk" ("ttk") functionality of Tk 8.5.[5][6] 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).


There are several popular GUI library alternatives available, such as wxPython, PyQt (PySide), Pygame, Pyglet, and PyGTK.



Some definitions



Window


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


A window that exists independently on the screen. It will be decorated with the standard frame and controls for the desktop manager. It can be moved around the desktop, and can usually be resized.



Widget


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



  • Core widgets: The containers:frame, toplevel, paned window. The buttons: button, radiobutton, checkbutton (checkbox), menubutton (combobox). The text widgets: label, labelframe, message, text. The entry widgets: scale, scroll, listbox, slider, spinbox, entry (singleline), text (multiline), and canvas (vector and pixel graphics).

  • There are the extension widgets:tk_optionMenu, tk_dialog, tk_messageBox, tk_getOpenFile, tk_getSaveFile, tk_chooseColor, tk_chooseDirectory.

  • The ttk widgets: The ttk widgets coexist with other widgets which they can replace.[7] There are ttk::button, ttk::checkbutton, ttk::combobox, ttk::entry, ttk::frame, ttk::label, ttk::labelframe, ttk::menubutton, ttk::notebook, ttk::panedwindow, ttk::progressbar, ttk::radiobutton, ttk::scale, ttk::scrollbar, ttk::separator, ttk::sizegrip, ttk::spinbox, ttk::treeview.


[8][9]



Frame


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


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.



A minimal application


Here is a minimal Python 3 Tkinter application with one widget:[10] (For Python 2, the only difference is the word "tkinter" in the import command will be capitalized to "Tkinter.")


1 #!/usr/bin/env python3
2 from tkinter import *
3 root = Tk() # Create the root (base) window
4 w = Label(root, text="Hello, world!") # Create a label with words
5 w.pack() # Put the label into the window
6 root.mainloop() # Start the event loop

[11]



Process


There are four stages to creating a widget



Create

create it within a frame

Configure

change the widgets attributes

Pack 

pack it into position so it become visible

Bind

bind it to a function or event. [9]


These are often compressed and the order can vary.



Simple application


Using the object orientated paradigm in Python, a simple program would be:


 1 #!/usr/bin/env python3
2 import tkinter as tk
3
4 class Application(tk.Frame):
5
6 def __init__(self, master=None):
7 super(Application, self).__init__(master)
8 self.grid()
9 self.createWidgets()
10
11 def createWidgets(self):
12 self.mondialLabel = tk.Label(self, text='Hello World')
13 self.mondialLabel.config(bg="#00ffff")
14 self.mondialLabel.grid()
15 self.quitButton = tk.Button(self, text='Quit', command=self.quit)
16 self.quitButton.grid()
17
18 app = Application()
19 app.master.title('Sample application')
20 app.mainloop()


  • line 1: Hashbang directive to the program launcher, allowing the selection of an appropriate interpreter executable, when self-executing.[12]

  • line 2: This line imports the tkinter module into your program's namespace, but renames it as tk.

  • line 4: The application class inherits from Tkinter's Frame class.

  • line 6: Calls the constructor for the parent class, Frame.

  • line 7: This is necessary to make the application actually appear on the screen.

  • line 11: Defining the widgets

  • line 12: Creates a label, named MondialLabel with the text "Hello World"

  • line 13: Sets the MondialLabel background colour to cyan

  • line 14: Places the label on the application so it is visible using the grid geometry manager method

  • line 15: Creates a button labeled “Quit”.

  • line 16: Places the button on the application. Grid, place and pack are all methods of making the widget visible

  • line 18: The main program starts here by instantiating the Application class.

  • line 19: This method call sets the title of the window to “Sample application”.

  • line 20: Starts the application's main loop, waiting for mouse and keyboard events.



ttk extension


These ttk extensions are now simply a part of the TK package, have been included since 8.5a6+.[7]


Widgets

ttk::button,
ttk::checkbutton,
ttk::combobox,
ttk::entry,
ttk::frame,
ttk::label,
ttk::labelframe,
ttk::menubutton,
ttk::notebook,
ttk::panedwindow,
ttk::progressbar,
ttk::radiobutton,
ttk::scale,
ttk::scrollbar,
ttk::separator,
ttk::sizegrip,
ttk::spinbox,
ttk::treeview.


Styles

Elements


ttk_image,
ttk_vsapi.


Themes

ttk::theme::alt,
ttk::theme::clam,
ttk::theme::classic,
ttk::theme::default,


various ttk themes

black ttk theme,
waldorf ttk theme,
ttk::setTheme.[7]



References





  1. ^ "Tkinter — Python interface to Tcl/Tk — Python v2.6.1 documentation". Retrieved 2009-03-12..mw-parser-output cite.citation{font-style:inherit}.mw-parser-output q{quotes:"""""""'""'"}.mw-parser-output code.cs1-code{color:inherit;background:inherit;border:inherit;padding:inherit}.mw-parser-output .cs1-lock-free a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/6/65/Lock-green.svg/9px-Lock-green.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-limited a,.mw-parser-output .cs1-lock-registration a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Lock-gray-alt-2.svg/9px-Lock-gray-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-lock-subscription a{background:url("//upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Lock-red-alt-2.svg/9px-Lock-red-alt-2.svg.png")no-repeat;background-position:right .1em center}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration{color:#555}.mw-parser-output .cs1-subscription span,.mw-parser-output .cs1-registration span{border-bottom:1px dotted;cursor:help}.mw-parser-output .cs1-hidden-error{display:none;font-size:100%}.mw-parser-output .cs1-visible-error{font-size:100%}.mw-parser-output .cs1-subscription,.mw-parser-output .cs1-registration,.mw-parser-output .cs1-format{font-size:95%}.mw-parser-output .cs1-kern-left,.mw-parser-output .cs1-kern-wl-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right,.mw-parser-output .cs1-kern-wl-right{padding-right:0.2em}


  2. ^ "Tkinter - Pythoninfo Wiki".


  3. ^ Shipman, John W. (2010-12-12), Tkinter reference: a GUI for Python, New Mexico Tech Computer Center, retrieved 2012-01-11


  4. ^ "Archived copy". Archived from the original on 2013-11-13. Retrieved 2013-11-13.CS1 maint: Archived copy as title (link)


  5. ^ "Python issue #2983, "Ttk support for Tkinter"".


  6. ^ "Python subversion revision 69051, which resolves issue #2983 by adding the ttk module".


  7. ^ abc "Ttk". wiki.tcl.tk. Retrieved 19 August 2018.


  8. ^ "Widgets in the initial Tk package". wiki.tcl.tk. Retrieved 19 August 2018.


  9. ^ ab Klein, Bernd. "GUI Programming with Python: Events and Binds". www.python-course.eu. Retrieved 18 August 2018.


  10. ^ "Tkinter 8.5 reference: a GUI for Python".


  11. ^ Fleck, Dan. "Tkinter – GUIs in Python" (PDF). CS112. George Mason University. Retrieved 18 August 2018.


  12. ^ "PEP 397 — Python launcher for Windows — Python.org". Retrieved 2017-06-07.




External links




  • TkInter, Python Wiki


  • Lundh, Fredrik (1999), An Introduction to Tkinter


  • TkDocs: includes language-neutral and Python-specific information and a tutorial


  • Ferg, Stephen, Thinking in Tkinter



GUI systems based on Tkinter



  • PAGE - Python Automatic GUI Generator


  • PySimpleGUI : Wrapper for tkinter, creates custom GUIs in a few lines of code.


  • Rapyd-Tk : A graphical development environment for writing applications in Tkinter.


  • tkRAD : Tkinter Rapid Application Development and XML widget builder for Tkinter.


  • Visual Tkinter : A Tkinter-based GUI builder for Python.









Comments

Popular posts from this blog

Information security

Volkswagen Group MQB platform

刘萌萌