没有合适的资源?快使用搜索试试~ 我知道了~
首页Python-GUI-Tkinter教程
Python-GUI-Tkinter教程
需积分: 50 30 下载量 75 浏览量
更新于2023-03-16
评论 4
收藏 721KB PDF 举报
Python的GUI(Tkinter)教程,英文版,简单易学,有很详细的说明,和源代码,适合初学者!
资源详情
资源评论
资源推荐
Tkinter reference: a GUI
for Python
John W. Shipman
2005-04-10 23:01
Table of Contents
1. What is Tkinter? ...................................................................................................................... 2
2. A minimal application .............................................................................................................. 3
3. Layout management ................................................................................................................ 3
3.1. The .grid() method .................................................................................................... 4
3.2. Other grid management methods ................................................................................... 5
4. Standard attributes .................................................................................................................. 5
4.1. Dimensions ................................................................................................................... 5
4.2. Coordinate system ......................................................................................................... 6
4.3. Colors ........................................................................................................................... 6
4.4. Type fonts ..................................................................................................................... 6
4.5. Anchors ........................................................................................................................ 8
4.6. Relief styles ................................................................................................................... 8
4.7. Bitmaps ........................................................................................................................ 8
4.8. Cursors ......................................................................................................................... 9
4.9. Images ........................................................................................................................ 10
4.10. Geometry strings ....................................................................................................... 10
4.11. Window names .......................................................................................................... 11
5. The Button widget ................................................................................................................ 11
6. The Canvas widget ................................................................................................................ 13
6.1. Canvas concepts .......................................................................................................... 14
6.2. Methods on Canvas objects ......................................................................................... 15
6.3. The canvas arc object ................................................................................................... 19
6.4. The canvas bitmap object ............................................................................................. 20
6.5. The canvas image object ............................................................................................... 20
6.6. The canvas line object .................................................................................................. 20
6.7. The canvas oval object .................................................................................................. 21
6.8. The canvas polygon object ............................................................................................ 22
6.9. The canvas rectangle object .......................................................................................... 23
6.10. The canvas text object ................................................................................................. 23
6.11. The canvas window object .......................................................................................... 24
7. The Checkbutton widget ...................................................................................................... 24
8. The Entry widget .................................................................................................................. 27
8.1. Scrolling an Entry widget ........................................................................................... 30
9. The Frame widget .................................................................................................................. 31
10. The Label widget ................................................................................................................ 32
11. The Listbox widget ............................................................................................................ 33
11.1. Scrolling a Listbox widget ....................................................................................... 36
12. The Menu widget .................................................................................................................. 36
1Tkinter referenceNew Mexico Tech Computer Center
12.1. Menu item creation (coption) options ....................................................................... 38
13. The Menubutton widget ...................................................................................................... 39
14. The Radiobutton widget .................................................................................................... 41
15. The Scale widget ................................................................................................................ 44
16. The Scrollbar widget ........................................................................................................ 47
16.1. The scrollbar command callback .................................................................................. 49
16.2. Connecting scrollbars to other widgets ........................................................................ 49
17. The Text widget .................................................................................................................. 50
17.1. Indices in text widgets ................................................................................................ 52
17.2. Marks in text widgets ................................................................................................. 52
17.3. Images in text widgets ................................................................................................ 53
17.4. Windows in text widgets ............................................................................................ 53
17.5. Tags in text widgets ................................................................................................... 53
17.6. Setting tabs in a Text widget ...................................................................................... 54
17.7. Methods on Text widgets .......................................................................................... 54
18. Toplevel: Top-level window methods ................................................................................. 60
19. Universal widget methods .................................................................................................... 61
20. Standardizing appearance ..................................................................................................... 68
20.1. How to name a widget class ....................................................................................... 69
20.2. How to name a widget instance .................................................................................. 69
20.3. Resource specification lines ........................................................................................ 69
20.4. Rules for resource matching ........................................................................................ 70
21. Connecting your application logic to the widgets .................................................................... 71
22. Control variables: the values behind the widgets .................................................................... 71
23. Focus: routing keyboard input ............................................................................................... 73
24. Events .................................................................................................................................. 75
24.1. Levels of binding ....................................................................................................... 75
24.2. Event sequences ......................................................................................................... 76
24.3. Event types ................................................................................................................ 76
24.4. Event modifiers ......................................................................................................... 77
24.5. Key names ................................................................................................................. 78
24.6. Writing your handler ................................................................................................. 80
24.7. The extra arguments trick ........................................................................................... 81
24.8. Virtual events ............................................................................................................ 82
1. What is Tkinter?
Tkinter is a GUI (graphical user interface) widget set for Python. This document contains only the
commoner features.
This document applies to Python 1.5 and Tkinter 8.0.4 running in the X Window system under Linux.
Your version may vary.
Pertinent references:
•
An Introduction to Tkinter [1] by Fredrik Lundh
•
Python and Tkinter Programming by John Grayson (Manning, 2000, ISBN 1-884777-81-3).
•
Python 2.2 quick reference [2]: general information about the Python language.
[1] http://www.pythonware.com/library/tkinter/introduction/index.htm
[2] http://www.nmt.edu/tcc/help/pubs/python22/
New Mexico Tech Computer CenterTkinter reference2
• This publication is available in Web form [3] and also as a PDF document [4]. Please forward any
comments to tcc-doc@nmt.edu.
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
logic behind it.
2. A minimal application
Here is a trivial Tkinter program containing only a Quit button:
#!/usr/local/bin/python
1
from Tkinter import *
2
class Application(Frame):
3
def __init__(self, master=None):
Frame.__init__(self, master)
4
self.grid()
5
self.createWidgets()
def createWidgets(self):
self.quitButton = Button ( self, text="Quit",
command=self.quit )
6
self.quitButton.grid()
7
app = Application()
8
app.master.title("Sample application")
9
app.mainloop()
10
1
This line makes the script self-executing, assuming that your system has the Python interpreter at
path /usr/local/bin/python.
2
This line imports the entire Tkinter package into your program's namespace.
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.
8
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 application's main loop, waiting for mouse and keyboard events.
3. Layout management
Later we will discuss the widgets, the building blocks of your GUI 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.
[3] http://www.nmt.edu/tcc/help/pubs/tkinter/
[4] http://www.nmt.edu/tcc/help/pubs/tkinter/tkinter.pdf
3Tkinter referenceNew Mexico Tech Computer Center
•
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:
thing = Constructor(master, ...)
thing.grid(...)
where Constructor is one of the widget classes like Button, Frame, and so on, and master 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.
3.1. The .grid() method
To display a widget w on your application screen:
w.grid(option, ...)
This method registers a widget w with the grid geometry manager—if you don't do this, the widget will
exist internally, but it will not be visible on the screen.
Here are the options to the .grid() geometry management method:
The column number where you want the widget gridded, counting from
zero. The default value is zero.
column
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
columnspan
columnspan option to the 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.
Internal x padding. This dimension is added inside the widget inside its left
and right sides.
ipadx
Internal y padding. This dimension is added inside the widget inside its top
and bottom borders.
ipady
External x padding. This dimension is added to the left and right outside the
widget.
padx
External y padding. This dimension is added above and below the widget.
pady
The row number into which you want to insert the widget, counting from 0.
The default is the next higher-numbered unoccupied row.
row
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
rowspan
number of cells to grab. This option can be used in combination with the
columnspan option to grab a block of cells. 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 and column numbers
2–6.
New Mexico Tech Computer CenterTkinter reference4
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.
sticky
•
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=NE (top right), SE (bottom
right), SW (bottom left), or NW (top left).
•
You can position the widget centered against one side of the cell by using sticky=N (top center),
E (right center), S (bottom center), or W (left center).
•
Use sticky=N+S to stretch the widget vertically but leave it centered horizontally.
•
Use sticky=E+W to stretch it horizontally but leave it centered vertically.
•
Use sticky=N+E+S+W to stretch the widget both horizontally and vertically to fill the cell.
•
The other combinations will also work. For example, sticky=N+S+W will stretch the widget vertically
and place it against the west (left) wall.
3.2. Other grid management methods
These grid-related methods are defined on all widgets:
w.grid_forget()
This method makes widget w disappear from the screen. It still exists, it just isn't visible. You can
use .grid() it to make it appear again, but it won't remember its grid options.
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(0) where w is the widget whose size you want to
force.
w.grid_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.
4. Standard attributes
Before we look at the widgets, let's take a look at how some of their common attributes—such 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, and retrieve the current setting of any option by using the widget's .cget() method. See
Section 19, “Universal widget methods” (p. 61) for more on these methods.
4.1. 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.
5Tkinter referenceNew Mexico Tech Computer Center
剩余81页未读,继续阅读
mudongwu
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 2023年中国辣条食品行业创新及消费需求洞察报告.pptx
- 2023年半导体行业20强品牌.pptx
- 2023年全球电力行业评论.pptx
- 2023年全球网络安全现状-劳动力资源和网络运营的全球发展新态势.pptx
- 毕业设计-基于单片机的液体密度检测系统设计.doc
- 家用清扫机器人设计.doc
- 基于VB+数据库SQL的教师信息管理系统设计与实现 计算机专业设计范文模板参考资料.pdf
- 官塘驿林场林防火(资源监管)“空天地人”四位一体监测系统方案.doc
- 基于专利语义表征的技术预见方法及其应用.docx
- 浅谈电子商务的现状及发展趋势学习总结.doc
- 基于单片机的智能仓库温湿度控制系统 (2).pdf
- 基于SSM框架知识产权管理系统 (2).pdf
- 9年终工作总结新年计划PPT模板.pptx
- Hytera海能达CH04L01 说明书.pdf
- 数据中心运维操作标准及流程.pdf
- 报告模板 -成本分析与报告培训之三.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0