This link has been bookmarked by 27 people . It was first bookmarked on 02 Feb 2007, by Rachel.
-
31 Jul 14
-
02 Apr 14
-
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.
-
Simply put, "packing" is a process of setting up a VISUAL relationship between a GUI component and its parent. If you don't pack a component, you will never see it.
-
early in the development process (Gerrit Muller has observed) often you don't yet know the best class structure to use -- early in the process, you simply don't have a clear enough understanding of the problem and the solution. Starting to use classes too early in the process can introduce a lot of unnecessary structure that merely clutters up the code, hinders understanding, and eventually requires more refactoring.
-
The "Button" widget (like all widgets) expects its first argument to be its parent.
-
-
30 Dec 13
Joe Hu"from Tkinter import * # ---------- code for class: curry (begin) --------------------- class curry: """from Scott David Daniels'recipe "curry -- associating parameters with a function" in the "Python Cookbook" http://aspn.activestate.com/ASPN/Python/Cookbook/ """ def __init__(self, fun, *args, **kwargs): self.fun = fun self.pending = args[:] self.kwargs = kwargs.copy() def __call__(self, *args, **kwargs): if kwargs and self.kwargs: kw = self.kwargs.copy() kw.update(kwargs) else: kw = kwargs or self.kwargs return self.fun(*(self.pending + args), **kw) # ---------- code for class: curry (end) --------------------- # ---------- code for function: event_lambda (begin) -------- def event_lambda(f, *args, **kwds ): """A helper function that wraps lambda in a prettier interface. Thanks to Chad Netzer for the code.""" return lambda event, f=f, args=args, kwds=kwds : f( *args, **kwds ) # ---------- code for function: event_lambda (end) ----------- class MyApp: def __init__(self, parent): self.myParent = parent self.myContainer1 = Frame(parent) self.myContainer1.pack() button_name = "OK" # command binding -- using curry self.button1 = Button(self.myContainer1, command = curry(self.buttonHandler, button_name, 1, "Good stuff!")) # event binding -- using the event_lambda helper function self.button1.bind("<Return>", event_lambda( self.buttonHandler, button_name, 1, "Good stuff!" ) ) self.button1.configure(text=button_name, background="green") self.button1.pack(side=LEFT) self.button1.focus_force() # Put keyboard focus on button1 button_name = "Cancel" # command binding -- using curry self.button2 = Button(self.myContainer1, command = curry(self.buttonHandler, button_name, 2, "Bad stuff!")) # event binding -- using the event_lambda helper function in two steps event_handler = event_lambda( self.buttonHandler, button_name, 2, "Bad stuff!" ) self.button2.bind("<Return>", event_handler ) self.button2.configure(text=button_name, background="red") self.button2.pack(side=LEFT) def buttonHandler(self, argument1, argument2, argument3): print " buttonHandler routine received arguments:", \ argument1.ljust(8), argument2, argument3 def buttonHandler_a(self, event, argument1, argument2, argument3): print "buttonHandler_a received event", event self.buttonHandler(argument1, argument2, argument3) print "\n"*100 # clear the screen print "Starting program tt079." root = Tk() myapp = MyApp(root) print "Ready to start executing the event loop." root.mainloop() print "Finished executing the event loop.""
-
29 Apr 12
-
22 Jun 11
-
01 Jun 11
-
09 May 11
-
14 Oct 10
-
07 Apr 10
-
11 Dec 09
-
11 Aug 09
John Nelsonfrom Tkinter import * class MyApp: def __init__(self, parent): self.myContainer1 = Frame(parent) self.myContainer1.pack() self.button1 = Button(self.myContainer1) self.button1["text"] = "Hello, World!" ### (1) self.button1["background"] = "green" ### (1)
-
29 Oct 08
-
14 Sep 08
-
writing event handler
-
In the previous examples, setting up the button has been a two-step process: first we create the button, then we set its properties
-
event handlers
-
called "handlers" because they "handle" (that is, respond to) such events.
-
Associating an event handler with a widget is called "binding
-
executes the "mainloop"
-
The loop continues to execute until a "destroy" event happens to the root window. A "destroy" event is one that closes a window.
-
distinguish between a container component and a widget
-
Tkinter provides a number of containers
-
most frequently used container is a "frame"
-
"packing" is a process of setting up a VISUAL relationship between a GUI component and its paren
-
place
-
kinter supports three geometry managers
-
A frame is basically a container
-
binding the event handler routines to widgets and events
-
the way that you create this binding
-
use the bind() method in a statement of the form
-
widget.bind(event_type_name, event_handler_name)
-
event binding
-
another way
-
command binding
-
When button1 is left-clicked with the mouse, the self.button1Click() method will be invoked to handle the event.
-
when an event occurs, it takes the form of an event objec
-
what do we want to happen when button1 is clicked
-
changes its color from green to yellow, and back again
-
make it shut down the window
-
button2
-
notice that because the text of one button is shorter than the text of the other, the two buttons are of different sizes
-
-
04 Jul 08
-
28 Mar 07
-
11 Feb 07
-
02 Feb 07
-
15 Jan 07
-
19 Jan 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.