tkinter(python gui编程)

所需积分/C币:50 2018-07-11 17:18:32 2.12MB PDF
2
收藏 收藏
举报

python自带的tkinter的pdf版本文档,详细介绍了tkinter的API。
28.1. Importing ttk…… 09 28.2. The ttk widget set ……110 29. ttk Button 110 30 ttk, Checkbutton ·.··· 112 31. ttk combobox 垂番番 115 32.ttk Entry ·...+·.;··+; 116 33. ttk. Frame 垂 118 34. ttk Label 119 35 ttk. Labelframe 122 36 ttk. Menubutton 124 37.ttk. Notebook∴ 126 371. Virtual events for the ttk. Notebook widget…………,…,……….…….128 38. ttk. Panedwindow 129 39.tk. Progressbar………,…, 潘垂看4垂音·卡垂番 ..130 40 ttk, Radiobutton ..131 41. ttk scale 133 42. ttk Scrollbar .·.··.····,·· 垂 135 43.t!k, Separator……, 137 ttk. Sizegrip 137 45. ttk. Treeview 137 45.1. Virtual events for the ttk. treeview widget 145 46. Methods common to all ttk widgets………… ∴145 46.1. Specifying widget states in ttk……… 音垂,音· 146 47. Customizing and creating ttk themes and styles .146 48. Finding and using ttk themes…………………………………….17 49. Using and customizing ttk styles…….…….14 50. The ttk element layer... 149 50.1. ttk layouts: Structuring a styl 149 50.2. ttk style maps: dynamic appearance changes .151 51. Connecting your appl plication logic to the widgets………… 153 52 Control variables: the values behind the widgets 153 53. Focus: routing keyboard input 155 53.1. Focus in ttk widgets………156 54 Events 157 54.1. Levels of bindi 157 54.2. Event 158 54.3. Event types… ∴158 54.4. Event modifiers 160 54.5. Key names 160 54.6. Writing your handler: The event class………… 162 54.7. The extra arguments trick 164 54. 8. Virtual events 165 55. Pop-up dialogs……………… 165 551. The tkMessageBox dialogs module………… ∴,165 55.2. The tkFileDialog module .167 55.3. The tkColorchooser module 168 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 Tkinter 8.5 running in the X Window system under Linux. Your version may vary New mexico Tech Computer Center Tkinter 8.5 reference Pertinent references Fredrik Lundh who wrote Tkinter has two versions of his an Introduction to Tkinter: a more complete 1999 version and a 2005 version that presents a few newer features Pithon 2. 7 quick reference: general information about the python language For an example of a sizeable working application(around 1000 lincs of code), see huey: A color and font selection tool. The design of this a pplication demonstrates how to build your own compound widgets We'll start by looking at the visible part of Tkinter: creating the widgets and arranging them on the screen. Later we will talk about how to connect the face-the "front panel"-of the application to the gic d it 2. A minimal application Here is a trivial Tkinter program containing only a Quit button #!/usr/bin/env python import tkinter as tk class application(tk frame def init (self, master=None): tk. Frame. init (self master) 345 self grid() self. createWidgets() def createWidgets(self) self. quitButton tk Button(self, text='Quit' command=self. quit) self. quitButton grid() app Application( app. master title(' Sample application') app. maintop() 1 This line makes the script self-executing assuming that your system has Python correctly installed 2 This line imports the Tkinter module into your programs namespace, but renames it as tk. 3 Your application class must inherit from Tkinter's Frame class 4 Calls the constructor for the parent class, Frame 5 Necessary to make the application actually appear on the screen 6 Creates a button labeled Quit 7 Places the button on the application The main program starts here by instantiating the Application class 9 This method call sets the title of the window to "Sample application 10 Starts the applications main loop, waiting for mouse and keyboard events http://www.pythonware.com/library/tkinter/introduction/ http://effbot.org/tkinterbook/ http://www.nmt.edu/tcc/help/pubs/python/ http://www.nmt.edu/tcc/help/lang/python/examples/huey/ Tkinter 8.5 reference New Mexico Tech Computer Center 3. Definitions Before we proceed, let's define some of the common terms window This term has different meanings in different contexts but in general it refers to a rectangular area somewhere on your display screen top-level window a window that exists independently on your screen It will be decorated with the standard frame and controls for your system's desktop manager. You can move it around on your desktop. You can generally resize it, although your application can prevent this widget The generic term for any of the building blocks that make up an application in a graphical user in- terface. Examples of widgets: buttons radiobuttons text fields frames, and text labels fr In Tkinter, the Frame widget is the basic unit of organization for complex layouts. A frame rectangular area that can contain other widgets child, 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 4. Layout management Later we will discuss the widgets, the building blocks of your GUl application. How do widgets get arranged 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 frame as 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 can either leave the extra space outside the widget, or stretch the widget to fit it, in either the horizontal or 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 Cons tructor is one of the widget classes like Button, Frame, and so on, and paren t is the parent widget in which this child widget is being constructed. All widgets have a.grid() method that you can use to tell the geometry manager where to put it New mexico Tech Computer Center Tkinter 8.5 reference 4.1. The grid()method To display a widget w on your application screen wgrid(option=value, ..) This method registers a widget w with the grid geometry manager--if you dont do this, the widget will exist 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 o) geometry manager column The column number where you want the widget gridded, counting from zero. The default value is zero columnspan Normally a widget occupies only one cell in the grid. However, you can grab multiple cells of a row and merge them into one larger cell by setting the co lumnspan option to the number of cells. For example, wgrid(row=0, column=2, columnspan=3) would place widget w in a cell that spans columns 2, 3, and 4 of row O ln To register w as a child of some widget W,, use in =w, The new parent w, must be a descendant of the parent widget used when w was created Ipad Internal x padding. This dimension is added inside the widget inside its left and right ipad Internal y padding. This dimension is added inside the widget inside its top and bottom borders padx External x padding. This dimension is added to the left and right outside the widget pady External y padding. This dimension is added above and below the widget row The row number into which you want to insert the widget, counting from 0. The default is the next higher-numbered unoccupied row. rows pan Normally a widget occupies only one cell in the grid. You can grab multiple adjacent cells 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 of cells. For example, wgrid(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 and column numbers 2-6 sticky This option determines how to distribute any extra space within the cell that is not taken up 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(bottom right), 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 wid get both horizontally and vertically to fill the cell Tkinter 8.5 reference New Mexico Tech Computer Center The other combinations will also work. For example, sticky=tk. N+tk. S+tk.W will stretch the widget vertically and place it against the west (left)wall 4.2. other grid management methods These grid-related methods are defined on all widgets wgrid bbox( column=None row=None, col2=None row=None) Returns a 4-tuple describing the bounding box of some or all of the grid system in widget w. The first two numbers returned are the x and y coordinates of the upper left corner of the area, and the second two numbers are the width and height If you pass in co lumn and row arguments, the returned bounding box describes the area of the cell at that column and row. if you also pass in col2 and row arguments the returned bounding box describes the arca of the grid from columns column to col2 inclusive, and from rows row to row2 inclusive For example, wgrid bbox(o,0, 1,1)returns the bounding box of four cells, not one wgrid forget() This method makes widget w disappear from the screen. It still exists, it just isnt visible. You can use. grid() it to make it appear again, but it won't remember its grid options .grid info( Returns a dictionary whose keys are w's option names, with the corresponding values of those options wgrid 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 its contents. To do this, call w grid propagate(o)where w is the widget whose size you want to orce wgrid remove() This method is like grid forget(), but its grid options are remembered, so if you. grid()it again, it will use the same grid configuration options wgrid size() Returns a 2-tuple containing the number of columns and the number of rows, respectively, in w's grid system wgrid slaves( row=None, column=None) Returns a list of the widgets managed by widget w. If no arguments are provided, you will get a list of all the managed widgets. Use the row= argument to select only the widgets in one row, or the column= argument to select only the widgets in one column 4.3. Configuring column and row sizes Unless you take certain measures, the width of a grid column inside a given widget will be equal to the width of its widest cell, and the height of a grid row will be the height of its tallest cell. The sticky attribute 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 parent widget W that contains the grid layout New mexico Tech Computer Center Tkinter 8.5 reference w.columnconfigure(N, option=value, .. In the grid layout inside widget w, configure column N so that the given option has the given value 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 manager minsize The column or row's minimum size in pixels. If there is nothing in the given column or row,it will not appear, even if you use this option pad A number of pixels that will be added to the given column or row, over and above the largest cell in the column or row weight To make a column or row stretchable, use this option and supply a value that gives the relative weight of this column or row when distributing the extra space. For example, if a widget w contains a grid layout, these lines will distribute three-fourths of the extra space to the first column and one-fourth to the second column w.columnconfigure(0, weight=3) W.columnconfigure(l, weight=l) If this option is not used, the column or row will not stretch 4. 4. Making the root window resizeable Do you want to let the user resize your entire application window and distribute the extra space among its 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 contains only 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. In this version the Quit button always fills all the available space def createwidgets(self) top=self winfo toplevel( top. rowconfigure(0, weight=l) top co lumnconfigure(0, weight=l) self. rowconfigure(0, weight=l) 4 self. columnconfigure(o, weight=l self. quit= Button (self, text=Quit, command=self. quit self. quit. grid(row=0, column=0 6 sticky=tk. N+tk. S+tk. E+tk. w) 1 The"top level window"is the outermost window on the screen. However this window is not your Application window-it is the parent of the Application instance. To get the top-level window, Tkinter 8.5 reference New mexico Tech Computer Center call the winfo toplevel() method on any widget in your application; see Section 26, "Universal widget methods"(p. 97) 2 This line makes row 0 of the top level window's grid stretchable 3 This line makes column 0 of the top level window's grid stretchable 4 Makes row O of the Application widget's grid stretchable 5 Makes column O of the Application widget's grid stretchable The argument sticky=tk. N+tk. S+tk. E+tk. W makes the button expand to fill its cell of the There is one more change that must be made. In the constructor change the second line as shown def init(self, master=None) tk. Frame. init (self, master) self grid(sticky=tk. N+tk. S+tk.E+tk. W) self. createwidgets() The argumentsticky=tk. N+tk. S+tk. E+tk. aw to self grid() is necessary so that the Applic ation widget will expand to fill its cell of the top-level window's grid 5. Standard attributes Before we look at the widgets, let's take a look at how some of their common attributessuch as sizes colors and fonts--are specified Each widget has a set of options that affect its appearance and behavior-attributes such as fonts, colors, sizes, text labels, and such You can specify options when calling the widget's constructor using keyword arguments such as text=PANIC! or height=20 After you have created a widget, you can later change any option by using the widget's config() method. You can retrieve the current setting of any option by using the widget's. cget() method See Section 26, Universal widget methods"(p. 97) for more on these methods 51. Dimensions Various lengths, widths, and other dimensions of widgets can be described in many different units If you set a dimension to an integer, it is assumed to be in pixels You can specify units by setting a dimension to a string containing a number followed by Table 3 Dimensional units c Centimeters iNches m Millimeters p Printer's points(about 1/72) New mexico Tech Computer Center Tkinter 8.5 reference 5.2. The coordinate system As in most contemporary display systems, the origin of each coordinate system is at its upper left corner, with the x coordinate increasing toward the right, and the y coordinate increasing toward the bottom X The base unit is the pixel, with the top left pixel having coordinates(0,0 ) Coordinates that you specify as integers are always expressed in pixels, but any coordinate may be specified as a dimensioned quantity; see Section 5.1, Dimensions"(p. 9) 5.3. Colors There are two general ways to specify colors in Tkinter You can use a string specifying the proportion of red green, and blue in hexadecimal digits trgb Four bits per color #rrggbb Eight bits per color #rrrgggbbb Twelve bits per color For example, #fff is white, #000000 is black, #ooofffooo is pure green, and #ooffff is pure cyan(green plus blue) You can also use any locally defined standard color name. The colors 'white,'black,'red' green, ' blue,'cyan', 'yelLow, and 'magenta'will al ways be available. Other names may work, depending on your local installation 5.4. Type fonts Depending on your platform, there may be up to three ways to specify type style As a tuple whose first element is the font family, followed by a size(in points if positive, in pixels if negative), optionally followed by a string containing one or more of the style modifiers bold, italic underline, and overstrike Examples: ('Helvetica 6)for a 16-point Helvetica regular; ( Times,24,bold italic)for a 24-point Times bold italic. For a 20-pixel Times bold font, use(Times,-20 bold) You can create a"font object"by importing the tkFont module and using its Font class constructor import tkFont font =tkFont Font(option,..) where the options include family The font family name as a string Tkinter 8.5 reference New Mexico Tech Computer Center

...展开详情
试读 127P tkinter(python gui编程)
立即下载 低至0.43元/次 身份认证VIP会员低至7折
一个资源只可评论一次,评论内容不能少于5个字
上传资源赚积分or赚钱
    最新推荐
    tkinter(python gui编程) 50积分/C币 立即下载
    1/127
    tkinter(python gui编程)第1页
    tkinter(python gui编程)第2页
    tkinter(python gui编程)第3页
    tkinter(python gui编程)第4页
    tkinter(python gui编程)第5页
    tkinter(python gui编程)第6页
    tkinter(python gui编程)第7页
    tkinter(python gui编程)第8页
    tkinter(python gui编程)第9页
    tkinter(python gui编程)第10页
    tkinter(python gui编程)第11页
    tkinter(python gui编程)第12页
    tkinter(python gui编程)第13页
    tkinter(python gui编程)第14页
    tkinter(python gui编程)第15页
    tkinter(python gui编程)第16页
    tkinter(python gui编程)第17页
    tkinter(python gui编程)第18页
    tkinter(python gui编程)第19页
    tkinter(python gui编程)第20页

    试读结束, 可继续阅读

    50积分/C币 立即下载 >