TKinter CS360 Dick Steflik Hello World from Tkinter

  • Slides: 9
Download presentation
TKinter CS-360 Dick Steflik

TKinter CS-360 Dick Steflik

Hello World from Tkinter import Label widget = Label(None, text=‘Hello World’) widget. pack() widget.

Hello World from Tkinter import Label widget = Label(None, text=‘Hello World’) widget. pack() widget. mainloop() # get a widget # make a Label # arrange it in its parent # start the event loop 1. Load a widget class from Tkinter 2. Make an instance of it (repeat 1 and 2 as needed) 3. Arrange the widget in its parent widget 4. Enter the event loop

Geometry Managers Geometry managers allow us to organize widgets inside of a container Pack

Geometry Managers Geometry managers allow us to organize widgets inside of a container Pack geometry manager http: //effbot. org/tkinterbook/pack. htm Grid geometry manager http: //effbot. org/tkinterbook/grid. htm Place geometry manager http: //effbot. org/tkinterbook/place. htm

Tkinter Events and Binding <Button-1> <Button-2> <Button-3> <B 1 -Motion> <Button. Release-1> <Double-Button-1> <Enter>

Tkinter Events and Binding <Button-1> <Button-2> <Button-3> <B 1 -Motion> <Button. Release-1> <Double-Button-1> <Enter> <Leave> <Focus. In> <Focus. Out> <Return> <Key> <Shift-Up> <Configure> - left mouse button - middle mouse button (on 3 button mouse) - rightmost mouse button - mouse moved with left button depressed - left button released - double click on button 1 - mouse pointer entered widget - mouse pointer left the widget - Keyboard focus moved to another widget - Enter key depressed - A key was depressed - Up arrow while holding Shift key - widget changed size or location http: //effbot. org/tkinterbook/tkinter-events-and-bindings. htm

Event Handling • Event sources (widgets) can specify their handlers – command handlers –

Event Handling • Event sources (widgets) can specify their handlers – command handlers – callbacks

Command Handlers use the ‘command=‘ keyword followed by the command you want executed ex

Command Handlers use the ‘command=‘ keyword followed by the command you want executed ex from Tkinter import * root = Tk() Button (root, text=‘Press Me’ , command=root. quit). pack(side=LEFT) root. mainloop()

Callbacks A callback is the name of the function that is to be run

Callbacks A callback is the name of the function that is to be run in response of an event Callbacks can be defined as a free standing function in our program or as a class member. ex. from Tkinter import * def quit(): print ‘Hello, getting out of here’ import sys; sys. exit() widget = Button(None, text=‘Press me to quit’ , command=quit) widget. pack() widget. mainloop()

Bound Method Callbacks Lets make a Hello Class and use it: from Tkinter import

Bound Method Callbacks Lets make a Hello Class and use it: from Tkinter import * class Hello. Class; # create the window in the class constructor def __init__(self): widget = Button(None, text=‘Press Me to quit’, command=self. quit) widget. pack() def quit(self): print ‘leaving now’ import sys ; sys. exit() Hello. Class() mainloop() # create a Hello. Class object

Binding Events from Tkinter import * def hello(event): print ‘Double click to exit’ def

Binding Events from Tkinter import * def hello(event): print ‘Double click to exit’ def quit(event); print ‘caught a double click, leaving’ import sys ; sys. exit() widget = Button(None, text=‘Hello Event World’) widget. pack() widget. bind(‘<Button-1>’ , hello) widget. bind(‘<Double-1>’ , quit) widget. mainloop()