没有合适的资源?快使用搜索试试~ 我知道了~
首页thinking in tkinter
资源详情
资源评论
资源推荐
Thinking in Tkinter
by Stephen Ferg
revised: 2005-07-17
This file contains the source code for all of the files in the Thinking in Tkinter series.
If you print this file with a small font, you should be able to print it in portrait mode without truncating
the ends of any of the lines of code. If you print it, and find that you ARE truncating lines of code,
consider printing it in a smaller font, or printing it in landscape mode.
Printed size is approximately 40 to 60 pages, depending on your print settings. You can see what the
printed result will look like, and how many pages it will require, by looking at the results of Print
Previewbefore you actually print the file.
tt000.py
Subject: "Thinking in Tkinter"
Author : Stephen Ferg
About " Thinking In Tkinter "
I've been trying to teach myself Tkinter out of various books, and I'm finding it more difficult than I
think it should be.
The problem is that the authors of the books want to rush into telling me about all of the widgets in the
Tkinter toolbox, but never really pause to explain basic concepts. They don't explain how to "think in
Tkinter".
Here are a few short programs that begin to explain how to think in Tkinter. In them, I don't attempt to
catalog all of the types of widgets, attributes, and methods that are available in Tkinter. and I certainly
don't try to provide a comprehensive introduction to Tkinter. I just try to get you started down the road
of understanding some basic Tkinter concepts.
Note that the discussion is devoted exclusively to the Tkinter pack (or "packer") geometry manager.
There is no discussion of the grid or place geometry managers.
The Four Basic Gui-programming Tasks
When you develop a user interface (UI) there is a standard set of tasks that you must accomplish.
1) You must specify how you want the UI to *look*. That is, you must write code that determines what
the user will see on the computer screen.
2) You must decide what you want the UI to *do*. That is, you must write routines that accomplish the
tasks of the program.
3) You must associate the "looking" with the "doing". That is, you must write code that associates the
things that the user sees on the screen with the routines that you have written to perform the program's
tasks.
4) finally, you must write code that sits and waits for input from the user.
Some Gui-programming Jargon
GUI (graphical user interface) programming has some special jargon associated with these basic tasks.
1) We specify how we want a GUI to look by describing the "widgets" that we want it to display, and
their spatial relationships (i.e. whether one widget is above or below, or to the right or left, of other
widgets). The word "widget" is a nonsense word that has become the common term for "graphical user
interface component". Widgets include things such as windows, buttons, menus and menu items, icons,
drop-down lists, scroll bars, and so on.
2) The routines that actually do the work of the GUI are called "callback handlers" or "event handlers".
"Events" are input events such as mouse clicks or presses of a key on the keyboard. These routines are
called "handlers" because they "handle" (that is, respond to) such events.
3) Associating an event handler with a widget is called "binding". Roughly, the process of binding
involves associating three different things:
(a) a type of event (e.g. a click of the left mouse button,
or a press of the ENTER key on the keyboard),
(b) a widget (e.g. a button), and
(c) an event-handler routine.
For example, we might bind (a) a single-click of the left mouse button on (b) the "CLOSE"
button/widget on the screen to (c) the "closeProgram" routine, which closes the window and shuts
down the program.
4) The code that sits and waits for input is called the "event loop".
About The Event Loop
If you believe the movies, every small town has a little old lady who spends all of her time at her front
window, just WATCHING. She sees everything that goes on in the neighborhood. A lot of what she
sees is uninteresting of course -- just people going to and fro in the street. But some of it is interesting
-- like a big fight between the newly-wed couple in the house across the street. When interesting events
happen, the watchdog lady immediately is on the phone with the news to the police or to her neighbors.
The event loop is a lot like this watchdog lady. The event loop spends all of its time watching events go
by, and it sees all of them. Most of the events are uninteresting, and when it sees them, it does nothing.
But when it sees something interesting -- an event that it knows is interesting, because an event handler
has been bound to the event -- then it immediately calls up the event handler and lets it know that the
event has happened.
Program Behavior
This program eases you into user-interace programming by showing how these basic concepts are
implemented in a very simple program. This program doesn't use Tkinter or any form of GUI
programming. It just puts up a menu on the console, and gets simple keyboard input. Even so, as you
can see, it does the four basic tasks of user-interface programming.
[revised: 2003-02-23]
Program Source Code
#----- task 2: define the event handler routines ---------------------
def handle_A():
print "Wrong! Try again!"
def handle_B():
print "Absolutely right! Trillium is a kind of flower!"
def handle_C():
print "Wrong! Try again!"
# ------------ task 1: define the appearance of the screen ------------
print "\n"*100 # clear the screen
print " VERY CHALLENGING GUESSING GAME"
print "========================================================"
print "Press the letter of your answer, then the ENTER key."
print
print " A. Animal"
print " B. Vegetable"
print " C. Mineral"
print
print " X. Exit from this program"
print
print "========================================================"
print "What kind of thing is 'Trillium'?"
print
# ---- task 4: the event loop. We loop forever, observing events. ---
while 1:
# We observe the next event
answer = raw_input().upper()
# -------------------------------------------------------
# Task 3: Associate interesting keyboard events with their
# event handlers. A simple form of binding.
# -------------------------------------------------------
if answer == "A": handle_A()
if answer == "B": handle_B()
if answer == "C": handle_C()
if answer == "X":
# clear the screen and exit the event loop
print "\n"*100
break
# Note that any other events are uninteresting, and are ignored
tt010.py
The Simplest Possible Tkinter Program -- Three Statements!
Of the four basic GUI tasks that we discussed in the last program, this program does only one -- it runs
the event loop.
(1) The first statement imports Tkinter, so that it is available for use. Note that the form of the import
("from Tkinter import *") means that we will not have to qualify anything that we get from Tkinter
with a "Tkinter." prefix.
(2) The second statement creates a "toplevel" window. Technically, what the the second statement is
doing, is creating an instance of the class "Tkinter.Tk".
This toplevel window is the highest-level GUI component in any Tkinter application. By convention,
the toplevel window is usually named "root".
(3) The third statement executes the "mainloop" (that is, the event loop) method of the "root" object. As
the mainloop runs, it waits for events to happen in root. If an event occurs, then it is handled and the
loop continues running, waiting for the next evernt. The loop continues to execute until a "destroy"
event happens to the root window. A "destroy" event is one that closes a window. When the root is
destroyed, the window is closed and the event loop is exited.
Program Behavior
When you run this program, you will see that (thanks to Tk) the toplevel window automatically comes
furnished with widgets to minimize, maximize, and close the window. Try them -- you'll see that they
really do work.
Clicking on the "close" widget (the "x" in a box, at the right of the title bar) generates a "destroy" event.
The destroy event terminates the main event loop. And since there are no statements after
"root.mainloop()", the program has nothing more to do, and ends.
[revised: 2003-02-23]
Program Source Code
from Tkinter import * ### (1)
root = Tk() ### (2)
root.mainloop() ### (3)
tt020.py
Now we tackle another of the four main GUI tasks -- specifying how the GUI should look.
In this program, we introduce three major concepts of Tkinter programming:
* creating a GUI object and associating it with its parent
* packing
* containers vs. widgets
From now on, I'm going to distinguish between a container component and a widget. As I will be using
the terms, a "widget" is a GUI component that (usually) is visible and does things. A "container" in
contrast is simply a container -- a basket, as it were -- into which we can put widgets.
Tkinter provides a number of containers. "Canvas" is a container for drawing applications. The most
frequently used container is a "frame".
Frames are provided by Tkinter in a class called "Frame". An expression like:
Frame(myParent)
creates an instance of the Frame class (that is, it creates a frame), and associates the frame instance
with its parent, myParent. Or another way of looking at it is: such an expression adds a child frame to
the myParent component.
So in this program, statement (1):
剩余54页未读,继续阅读
Pythinker
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0