Tkinter 8.5reference:aGUIfor Python - GitHub Pages

1y ago
6 Views
2 Downloads
2.08 MB
168 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Kamden Hassan
Transcription

Tkinter 8.5 reference: a GUI forPythonJohn W. Shipman2013-12-31 17:59AbstractDescribes the Tkinter widget set for constructing graphical user interfaces (GUIs) in the Pythonprogramming language. Includes coverage of the ttk themed widgets.12This publication is available in Web form and also as a PDF document . Please forward anycomments to tcc-doc@nmt.edu.Table of Contents1. A cross-platform graphical user interface builder for Python . 32. A minimal application . 43. Definitions . 54. Layout management . 54.1. The .grid() method . 64.2. Other grid management methods . 74.3. Configuring column and row sizes . 74.4. Making the root window resizeable . 85. Standard attributes . 95.1. Dimensions . 95.2. The coordinate system . 105.3. Colors . 105.4. Type fonts . 105.5. Anchors . 125.6. Relief styles . 125.7. Bitmaps . 125.8. Cursors . 135.9. Images . 145.10. Geometry strings . 155.11. Window names . 165.12. Cap and join styles . 165.13. Dash patterns . 175.14. Matching stipple patterns . 176. Exception handling . 187. The Button widget . 188. The Canvas widget . 208.1. Canvas coordinates . 228.2. The Canvas display list . 228.3. Canvas object IDs . //www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdfNew Mexico Tech Computer CenterTkinter 8.5 reference1

8.4. Canvas tags . 228.5. Canvas tagOrId arguments . 228.6. Methods on Canvas widgets . 228.7. Canvas arc objects . 288.8. Canvas bitmap objects . 298.9. Canvas image objects . 308.10. Canvas line objects . 308.11. Canvas oval objects . 328.12. Canvas polygon objects . 338.13. Canvas rectangle objects . 358.14. Canvas text objects . 378.15. Canvas window objects . 389. The Checkbutton widget . 3810. The Entry widget . 4110.1. Scrolling an Entry widget . 4510.2. Adding validation to an Entry widget . 4511. The Frame widget . 4712. The Label widget . 4813. The LabelFrame widget . 5014. The Listbox widget . 5214.1. Scrolling a Listbox widget . 5615. The Menu widget . 5615.1. Menu item creation (coption) options . 5915.2. Top-level menus . 6016. The Menubutton widget . 6117. The Message widget . 6318. The OptionMenu widget . 6419. The PanedWindow widget . 6519.1. PanedWindow child configuration options . 6720. The Radiobutton widget . 6821. The Scale widget . 7122. The Scrollbar widget . 7422.1. The Scrollbar command callback . 7722.2. Connecting a Scrollbar to another widget . 7723. The Spinbox widget . 7824. The Text widget . 8224.1. Text widget indices . 8424.2. Text widget marks . 8624.3. Text widget images . 8624.4. Text widget windows . 8724.5. Text widget tags . 8724.6. Setting tabs in a Text widget . 8724.7. The Text widget undo/redo stack . 8824.8. Methods on Text widgets . 8825. Toplevel: Top-level window methods . 9526. Universal widget methods . 9727. Standardizing appearance . 10527.1. How to name a widget class . 10627.2. How to name a widget instance . 10727.3. Resource specification lines . 10727.4. Rules for resource matching . 10828. ttk: Themed widgets . 1082Tkinter 8.5 referenceNew Mexico Tech Computer Center

28.1. Importing ttk . 10928.2. The ttk widget set . 11029. ttk.Button . 11030. ttk.Checkbutton . 11231. ttk.Combobox . 11532. ttk.Entry . 11633. ttk.Frame . 11834. ttk.Label . 11935. ttk.LabelFrame . 12236. ttk.Menubutton . 12437. ttk.Notebook . 12637.1. Virtual events for the ttk.Notebook widget . 12838. ttk.PanedWindow . 12939. ttk.Progressbar . 13040. ttk.Radiobutton . 13141. ttk.Scale . 13342. ttk.Scrollbar . 13543. ttk.Separator . 13744. ttk.Sizegrip . 13745. ttk.Treeview . 13745.1. Virtual events for the ttk.Treeview widget . 14546. Methods common to all ttk widgets . 14546.1. Specifying widget states in ttk . 14647. Customizing and creating ttk themes and styles . 14648. Finding and using ttk themes . 14749. Using and customizing ttk styles . 14750. The ttk element layer . 14950.1. ttk layouts: Structuring a style . 14950.2. ttk style maps: dynamic appearance changes . 15151. Connecting your application logic to the widgets . 15352. Control variables: the values behind the widgets . 15353. Focus: routing keyboard input . 15553.1. Focus in ttk widgets . 15654. Events . 15754.1. Levels of binding . 15754.2. Event sequences . 15854.3. Event types . 15854.4. Event modifiers . 16054.5. Key names . 16054.6. Writing your handler: The Event class . 16254.7. The extra arguments trick . 16454.8. Virtual events . 16555. Pop-up dialogs . 16555.1. The tkMessageBox dialogs module . 16555.2. The tkFileDialog module . 16755.3. The tkColorChooser module . 1681. A cross-platform graphical user interface builder for PythonTkinter is a GUI (graphical user interface) widget set for Python. This document was written for Python2.7 and Tkinter 8.5 running in the X Window system under Linux. Your version may vary.New Mexico Tech Computer CenterTkinter 8.5 reference3

Pertinent references: Fredrik Lundh, who wrote Tkinter, has two versions of his An Introduction to Tkinter: a more complete341999 version and a 2005 version that presents a few newer features.5 Python 2.7 quick reference : general information about the Python language. For an example of a sizeable working application (around 1000 lines of code), see huey: A color and6font selection tool . The design of this application demonstrates how to build your own compoundwidgets.We'll start by looking at the visible part of Tkinter: creating the widgets and arranging them on thescreen. Later we will talk about how to connect the face—the “front panel”—of the application to thelogic behind it.2. A minimal applicationHere is a trivial Tkinter program containing only a Quit button:#!/usr/bin/env pythonimport Tkinter as tk12class Application(tk.Frame):def init (self, master None):tk.Frame. init (self, master)self.grid()self.createWidgets()345def createWidgets(self):self.quitButton tk.Button(self, text 'Quit',6command self.quit)7self.quitButton.grid()app Application()app.master.title('Sample application')app.mainloop()123456789108910This line makes the script self-executing, assuming that your system has Python correctly installed.This line imports the Tkinter module into your program's namespace, but renames it as tk.Your application class must inherit from Tkinter's Frame class.Calls the constructor for the parent class, Frame.Necessary to make the application actually appear on the screen.Creates a button labeled “Quit”.Places the button on the application.The main program starts here by instantiating the Application class.This method call sets the title of the window to “Sample application”.Starts the application's main loop, waiting for mouse and keyboard du/tcc/help/lang/python/examples/huey/454Tkinter 8.5 referenceNew Mexico Tech Computer Center

3. DefinitionsBefore we proceed, let's define some of the common terms.windowThis term has different meanings in different contexts, but in general it refers to a rectangular areasomewhere on your display screen.top-level windowA window that exists independently on your screen. It will be decorated with the standard frameand controls for your system's desktop manager. You can move it around on your desktop. Youcan generally resize it, although your application can prevent thiswidgetThe generic term for any of the building blocks that make up an application in a graphical user interface. Examples of widgets: buttons, radiobuttons, text fields, frames, and text labels.frameIn Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame is arectangular area that can contain other widgets.child, parentWhen any widget is created, a parent-child relationship is created. For example, if you place a textlabel inside a frame, the frame is the parent of the label.4. Layout managementLater we will discuss the widgets, the building blocks of your GUI application. How do widgets getarranged in a window?Although there are three different “geometry managers” in Tkinter, the author strongly prefers the.grid() geometry manager for pretty much everything. This manager treats every window or frameas a table—a gridwork of rows and columns. A cell is the area at the intersection of one row and one column. The width of each column is the width of the widest cell in that column. The height of each row is the height of the largest cell in that row. For widgets that do not fill the entire cell, you can specify what happens to the extra space. You caneither leave the extra space outside the widget, or stretch the widget to fit it, in either the horizontalor vertical dimension. You can combine multiple cells into one larger area, a process called spanning.When you create a widget, it does not appear until you register it with a geometry manager. Hence,construction and placing of a widget is a two-step process that goes something like this:self.thing tk.Constructor(parent, .)self.thing.grid(.)where Constructor is one of the widget classes like Button, Frame, and so on, and parent is theparent widget in which this child widget is being constructed. All widgets have a .grid() methodthat you can use to tell the geometry manager where to put it.New Mexico Tech Computer CenterTkinter 8.5 reference5

4.1. The .grid() methodTo display a widget w on your application screen:w.grid(option value, .)This method registers a widget w with the grid geometry manager—if you don't do this, the widget willexist internally, but it will not be visible on the screen. For the options, see Table 1, “Arguments of the.grid() geometry manager” (p. 6).Table 1. Arguments of the .grid() geometry managercolumnThe column number where you want the widget gridded, counting from zero. The defaultvalue is zero.columnspan Normally a widget occupies only one cell in the grid. However, you can grab multiplecells of a row and merge them into one larger cell by setting the columnspan option tothe number of cells. For example, w.grid(row 0, column 2, columnspan 3)would place widget w in a cell that spans columns 2, 3, and 4 of row 0.inTo register w as a child of some widget w2, use in w2. The new parent w2 must be adescendant of the parent widget used when w was created.ipadxInternal x padding. This dimension is added inside the widget inside its left and rightsides.ipadyInternal y padding. This dimension is added inside the widget inside its top and bottomborders.padxExternal x padding. This dimension is added to the left and right outside the widget.padyExternal y padding. This dimension is added above and below the widget.rowThe row number into which you want to insert the widget, counting from 0. The defaultis the next higher-numbered unoccupied row.rowspanNormally a widget occupies only one cell in the grid. You can grab multiple adjacentcells of a column, however, by setting the rowspan option to the number of cells to grab.This option can be used in combination with the columnspan option to grab a block ofcells. For example, w.grid(row 3, column 2, rowspan 4, columnspan 5)would place widget w in an area formed by merging 20 cells, with row numbers 3–6 andcolumn numbers 2–6.stickyThis option determines how to distribute any extra space within the cell that is not takenup by the widget at its natural size. See below. If you do not provide a sticky attribute, the default behavior is to center the widget in the cell. You can position the widget in a corner of the cell by using sticky tk.NE (top right), tk.SE (bottomright), tk.SW (bottom left), or tk.NW (top left). You can position the widget centered against one side of the cell by using sticky tk.N (top center),tk.E (right center), tk.S (bottom center), or tk.W (left center). Use sticky tk.N tk.S to stretch the widget vertically but leave it centered horizontally. Use sticky tk.E tk.W to stretch it horizontally but leave it centered vertically. Use sticky tk.N tk.E tk.S tk.W to stretch the widget both horizontally and vertically to fillthe cell.6Tkinter 8.5 referenceNew Mexico Tech Computer Center

The other combinations will also work. For example, sticky tk.N tk.S tk.W will stretch thewidget vertically and place it against the west (left) wall.4.2. Other grid management methodsThese grid-related methods are defined on all widgets:w.grid bbox(column None, row None, col2 None, row2 None)Returns a 4-tuple describing the bounding box of some or all of the grid system in widget w. Thefirst two numbers returned are the x and y coordinates of the upper left corner of the area, and thesecond two numbers are the width and height.If you pass in column and row arguments, the returned bounding box describes the area of the cellat that column and row. If you also pass in col2 and row2 arguments, the returned bounding boxdescribes the area of the grid from columns column to col2 inclusive, and from rows row to row2inclusive.For example, w.grid bbox(0, 0, 1, 1) returns the bounding box of four cells, not one.w.grid forget()This method makes widget w disappear from the screen. It still exists, it just isn't visible. You canuse .grid() it to make it appear again, but it won't remember its grid options.w.grid info()Returns a dictionary whose keys are w's option names, with the corresponding values of those options.w.grid location(x, y)Given a coordinates (x, y) relative to the containing widget, this method returns a tuple (col,row) describing what cell of w's grid system contains that screen coordinate.w.grid propagate()Normally, all widgets propagate their dimensions, meaning that they adjust to fit the contents.However, sometimes you want to force a widget to be a certain size, regardless of the size of itscontents. To do this, call w.grid propagate(0) where w is the widget whose size you want toforce.w.grid remove()This method is like .grid forget(), but its grid options are remembered, so if you .grid() itagain, it will use the same grid configuration options.w.grid size()Returns a 2-tuple containing the number of columns and the number of rows, respectively, in w'sgrid system.w.grid slaves(row None, column None)Returns a list of the widgets managed by widget w. If no arguments are provided, you will get alist of all the managed widgets. Use the row argument to select only the widgets in one row, orthe column argument to select only the widgets in one column.4.3. Configuring column and row sizesUnless you take certain measures, the width of a grid column inside a given widget will be equal to thewidth of its widest cell, and the height of a grid row will be the height of its tallest cell. The stickyattribute on a widget controls only where it will be placed if it doesn't completely fill the cell.If you want to override this automatic sizing of columns and rows, use these methods on the parentwidget w that contains the grid layout:New Mexico Tech Computer CenterTkinter 8.5 reference7

w.columnconfigure(N, option value, .)In the grid layout inside widget w, configure column N so that the given option has the givenvalue. For options, see the table below.w.rowconfigure(N, option value, .)In the grid layout inside widget w, configure row N so that the given option has the given value.For options, see the table below.Here are the options used for configuring column and row sizes.Table 2. Column and row configuration options for the .grid() geometry managerminsizeThe column or row's minimum size in pixels. If there is nothing in the given column orrow, it will not appear, even if you use this option.padA number of pixels that will be added to the given column or row, over and above thelargest cell in the column or row.weightTo make a column or row stretchable, use this option and supply a value that gives therelative weight of this column or row when distributing the extra space. For example, ifa widget w contains a grid layout, these lines will distribute three-fourths of the extraspace to the first column and one-fourth to the second column:w.columnconfigure(0, weight 3)w.columnconfigure(1, weight 1)If this option is not used, the column or row will not stretch.4.4. Making the root window resizeableDo you want to let the user resize your entire application window, and distribute the extra space amongits internal widgets? This requires some operations that are not obvious.It's necessary to use the techniques for row and column size management, described in Section 4.3,“Configuring column and row sizes” (p. 7), to make your Application widget's grid stretchable.However, that alone is not sufficient.Consider the trivial application discussed in Section 2, “A minimal application” (p. 4), which containsonly a Quit button. If you run this application, and resize the window, the button stays the same size,centered within the window.Here is a replacement version of the . createWidgets() method in the minimal application. Inthis version, the Quit button always fills all the available space.def createWidgets(self):1top self.winfo toplevel()2top.rowconfigure(0, weight 1)3top.columnconfigure(0, weight 1)4self.rowconfigure(0, weight 1)5self.columnconfigure(0, weight 1)self.quit Button(self, text 'Quit', command self.quit)6self.quit.grid(row 0, column 0,sticky tk.N tk.S tk.E tk.W)18The “top level window” is the outermost window on the screen. However, this window is not yourApplication window—it is the parent of the Application instance. To get the top-level window,Tkinter 8.5 referenceNew Mexico Tech Compu

programming language. Includes coverage of the ttk themed widgets. This publication is available in Web form 1 . A cross-platform graphical user interface builder for Python. Tkinter. is a GUI (graphical user interface) widget set for Python. This document was written for Python 2.7 and.

Related Documents:

Tkinter is largely unchanged between python 2 and python 3, with the major difference being that the tkinter package and modules were renamed. Importing in python 2.x In python 2.x, the tkinter package is named Tkinter, and related packages have their own names. For example, the following shows a typical set of import statements for python 2.x:

Python 1 and 2; renamed to tkinter in Python 3). If Tkinter is available, then no errors occur, as demonstrated in the following: import tkinter If your Python interpreter was not compiled with Tkinter enabled, the module import fails. You might need to recompile your Python interpreter to gain access to Tkinter. This usually

In addition to the Tk interface module, Tkinter includes a number of Python modules. The two most important modules are the Tkinter module itself, and a module called Tkconstants. The former automatically imports the latter, so to use Tkinter, all you need to do is to import one module: import Tkinter Or,

programming in Python, using Tkinter . There are several GUI interfaces available in Python: Tkinter is the Python interface to the Tk GUI toolkit. wxPython is an open-source Python interface for wxWindows. JPython is a Python port for Java which gives Python scripts access

Describes the Tkinterwidget set for constructing graphical user interfaces (GUIs) in the Python programming language. Includes coverage of the ttk themed widgets. This publication is available in Web form 1 and also as a PDF document 2. Please forward any comments to tcc-doc@nmt.edu. Table of Contents 1.

"Tkinter is Python's de facto standard GUI (Graphical User Interface) package. It is a thin object oriented layer on top of Tcl/Tk." Tkinter examples you may have seen. Tkinter examples you may have seen. With ttk, you get a nicer look. Themed widgets can match the platform. .

Setup Begin with this import statement: from tkinter import * Note: In earlier versions of Python, this module was called Tkinter, not tkinter Then create an object of type Tk: top Tk() This is the top-level window of your GUI program You can use any name for it; in these slide

Introduction to Groups, Rings and Fields HT and TT 2011 H. A. Priestley 0. Familiar algebraic systems: review and a look ahead. GRF is an ALGEBRA course, and specifically a course about algebraic structures. This introduc-tory section revisits ideas met in the early part of Analysis I and in Linear Algebra I, to set the scene and provide motivation. 0.1 Familiar number systems Consider the .