Updated for Python 1.4

This commit is contained in:
Guido van Rossum 1996-07-30 18:57:18 +00:00
parent c30e95f4b0
commit 89cb67bb64
43 changed files with 731 additions and 961 deletions

View file

@ -8,18 +8,15 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT.pack(side=LEFT, fill=BOTH)
# a hello button
self.hi_there = Button(self, {'text': 'Hello',
'command' : self.printit})
self.hi_there.pack({'side': 'left'})
self.hi_there = Button(self, text='Hello',
command=self.printit)
self.hi_there.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -7,16 +7,15 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
# all of these work..
self.draw.create_polygon("0", "0", "10", "0", "10", "10", "0" , "10", {"tags" : "thing"})
self.draw.pack({'side': 'left'})
self.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="blue")
self.draw.pack(side=LEFT)
def moveThing(self, *args):
# move 1/10 of an inch every 1/10 sec (1" per second, smoothly)

View file

@ -6,29 +6,24 @@ from Tkinter import *
# Tkinter is smart enough to start the system if it's not already going.
class Test(Frame):
def printit(self):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
self.speed = Scale(self, {"orient": "horiz",
"from" : -100,
"to" : 100})
self.speed = Scale(self, orient=HORIZONTAL, from_=-100, to=100)
self.speed.pack({'side': 'bottom', "fill" : "x"})
self.speed.pack(side=BOTTOM, fill=X)
# all of these work..
self.draw.create_polygon("0", "0", "10", "0", "10", "10", "0" , "10", {"tags" : "thing"})
self.draw.pack({'side': 'left'})
self.draw.create_rectangle(0, 0, 10, 10, tags="thing", fill="blue")
self.draw.pack(side=LEFT)
def moveThing(self, *args):
velocity = self.speed.get()
@ -37,8 +32,6 @@ class Test(Frame):
self.draw.move("thing", str, str)
self.after(10, self.moveThing)
def __init__(self, master=None):
Frame.__init__(self, master)
Pack.config(self)

View file

@ -18,8 +18,9 @@ class App(Frame):
# Note that here is where we bind a completely different callback to
# the same event. We pass "+" here to indicate that we wish to ADD
# this callback to the list associated with this event type. Not specifying "+" would
# simply override whatever callback was defined on this event.
# this callback to the list associated with this event type.
# Not specifying "+" would simply override whatever callback was
# defined on this event.
self.entrythingy.bind('<Key-Return>', self.print_something_else, "+")
def print_contents(self, event):

View file

@ -7,18 +7,16 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
# see the other demos for other ways of specifying coords for a polygon
self.draw.create_polygon("0i", "0i", "3i", "0i", "3i", "3i", "0i" , "3i")
self.draw.pack({'side': 'left'})
self.draw.create_rectangle(0, 0, "3i", "3i", fill="black")
self.draw.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -10,15 +10,15 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'bg': 'red',
'fg': 'white',
'height' : 3,
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT',
background='red',
foreground='white',
height=3,
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.canvasObject = Canvas(self, {"width" : "5i", "height" : "5i"})
self.canvasObject.pack({'side': 'left'})
self.canvasObject = Canvas(self, width="5i", height="5i")
self.canvasObject.pack(side=LEFT)
def mouseDown(self, event):
# canvas x and y take the screen coords from the event and translate
@ -29,12 +29,13 @@ class Test(Frame):
def mouseMotion(self, event):
# canvas x and y take the screen coords from the event and translate
# them into the coordinate system of the canvas object
x = self.canvasObject.canvasx(event.x, self.griddingsize)
y = self.canvasObject.canvasy(event.y, self.griddingsize)
x = self.canvasObject.canvasx(event.x, self.griddingSize)
y = self.canvasObject.canvasy(event.y, self.griddingSize)
if (self.startx != event.x) and (self.starty != event.y) :
self.canvasObject.delete(self.rubberbandBox)
self.rubberbandBox = self.canvasObject.create_rectangle(self.startx, self.starty, x, y)
self.rubberbandBox = self.canvasObject.create_rectangle(
self.startx, self.starty, x, y)
# this flushes the output, making sure that
# the rectangle makes it to the screen
# before the next event is handled

View file

@ -9,26 +9,24 @@ class Test(Frame):
###################################################################
def mouseDown(self, event):
# see if we're inside a dot. If we are, it
# gets tagged as "current" for free by tk.
if not event.widget.find_withtag("current"):
# gets tagged as CURRENT for free by tk.
if not event.widget.find_withtag(CURRENT):
# there is no dot here, so we can make one,
# and bind some interesting behavior to it.
# ------
# create a dot, and mark it as current
fred = self.draw.create_oval(event.x - 10, event.y -10, event.x +10, event.y + 10,
{"fill" : "green", "tag" : "current"})
# create a dot, and mark it as CURRENT
fred = self.draw.create_oval(
event.x - 10, event.y -10, event.x +10, event.y + 10,
fill="green", tags=CURRENT)
self.draw.bind(fred, "<Any-Enter>", self.mouseEnter)
self.draw.bind(fred, "<Any-Leave>", self.mouseLeave)
self.lastx = event.x
self.lasty = event.y
def mouseMove(self, event):
self.draw.move("current", event.x - self.lastx, event.y - self.lasty)
self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
self.lastx = event.x
self.lasty = event.y
@ -36,25 +34,22 @@ class Test(Frame):
###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
###################################################################
def mouseEnter(self, event):
# the "current" tag is applied to the object the cursor is over.
# the CURRENT tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "red"})
def mouseLeave(self, event):
# the "current" tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "blue"})
self.draw.itemconfig(CURRENT, fill="red")
def mouseLeave(self, event):
# the CURRENT tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig(CURRENT, fill="blue")
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw.pack({'side': 'left'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
self.draw = Canvas(self, width="5i", height="5i")
self.draw.pack(side=LEFT)
Widget.bind(self.draw, "<1>", self.mouseDown)
Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)

View file

@ -10,11 +10,10 @@ class Test(Frame):
# remember where the mouse went down
self.lastx = event.x
self.lasty = event.y
def mouseMove(self, event):
# whatever the mouse is over gets tagged as "current" for free by tk.
self.draw.move("current", event.x - self.lastx, event.y - self.lasty)
# whatever the mouse is over gets tagged as CURRENT for free by tk.
self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
self.lastx = event.x
self.lasty = event.y
@ -22,27 +21,24 @@ class Test(Frame):
###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
###################################################################
def mouseEnter(self, event):
# the "current" tag is applied to the object the cursor is over.
# the CURRENT tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "red"})
self.draw.itemconfig(CURRENT, fill="red")
def mouseLeave(self, event):
# the "current" tag is applied to the object the cursor is over.
# the CURRENT tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "blue"})
self.draw.itemconfig(CURRENT, fill="blue")
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw.pack({'side': 'left'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
self.draw = Canvas(self, width="5i", height="5i")
self.draw.pack(side=LEFT)
fred = self.draw.create_oval(0, 0, 20, 20,
{"fill" : "green", "tag" : "selected"})
fill="green", tags="selected")
self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)

View file

@ -11,20 +11,20 @@ class Test(Frame):
###################################################################
def mouseDown(self, event):
# see if we're inside a dot. If we are, it
# gets tagged as "current" for free by tk.
# gets tagged as CURRENT for free by tk.
if not event.widget.find_withtag("current"):
if not event.widget.find_withtag(CURRENT):
# we clicked outside of all dots on the canvas. unselect all.
# re-color everything back to an unselected color
self.draw.itemconfig("selected", {"fill" : UNSELECTED_COLOR})
self.draw.itemconfig("selected", fill=UNSELECTED_COLOR)
# unselect everything
self.draw.dtag("selected")
else:
# mark as "selected" the thing the cursor is under
self.draw.addtag("selected", "withtag", "current")
self.draw.addtag("selected", "withtag", CURRENT)
# color it as selected
self.draw.itemconfig("selected", {"fill": SELECTED_COLOR})
self.draw.itemconfig("selected", fill=SELECTED_COLOR)
self.lastx = event.x
self.lasty = event.y
@ -38,40 +38,36 @@ class Test(Frame):
def makeNewDot(self):
# create a dot, and mark it as current
fred = self.draw.create_oval(0, 0, 20, 20,
{"fill" : SELECTED_COLOR, "tag" : "current"})
fill=SELECTED_COLOR, tags=CURRENT)
# and make it selected
self.draw.addtag("selected", "withtag", "current")
self.draw.addtag("selected", "withtag", CURRENT)
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
################
# make the canvas and bind some behavior to it
################
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
Widget.bind(self.draw, "<1>", self.mouseDown)
Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
# and other things.....
self.button = Button(self, {"text" : "make a new dot",
"command" : self.makeNewDot,
"fg" : "blue"})
self.button = Button(self, text="make a new dot", foreground="blue",
command=self.makeNewDot)
self.label = Message(self,
{"width" : "5i",
"text" : SELECTED_COLOR + " dots are selected and can be dragged.\n" +
UNSELECTED_COLOR + " are not selected.\n" +
"Click in a dot to select it.\n" +
"Click on empty space to deselect all dots." })
message = ("%s dots are selected and can be dragged.\n"
"%s are not selected.\n"
"Click in a dot to select it.\n"
"Click on empty space to deselect all dots."
) % (SELECTED_COLOR, UNSELECTED_COLOR)
self.label = Message(self, width="5i", text=message)
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.label.pack({"side" : "bottom", "fill" : "x", "expand" : 1})
self.button.pack({"side" : "bottom", "fill" : "x"})
self.draw.pack({'side': 'left'})
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.label.pack(side=BOTTOM, fill=X, expand=1)
self.button.pack(side=BOTTOM, fill=X)
self.draw.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -6,17 +6,16 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.drawing = Canvas(self, {"width" : "5i", "height" : "5i"})
self.drawing = Canvas(self, width="5i", height="5i")
# make a shape
pgon = self.drawing.create_polygon("10", "10", "110", "10", "110", "110", "10" , "110",
{"fill" : "red",
"tags" : "weee foo groo"})
pgon = self.drawing.create_polygon(
10, 10, 110, 10, 110, 110, 10 , 110,
fill="red", tags=("weee", "foo", "groo"))
# this is how you query an object for its attributes
# config options FOR CANVAS ITEMS always come back in tuples of length 5.
@ -31,15 +30,14 @@ class Test(Frame):
option_value = self.drawing.itemconfig(pgon, "stipple")
print "pgon's current stipple value is -->", option_value[4], "<--"
option_value = self.drawing.itemconfig(pgon, "fill")
print "pgon's current fill value is -->", option_value[4], "<-- when he is usually colored -->", option_value[3], "<--"
print "pgon's current fill value is -->", option_value[4], "<--"
print " when he is usually colored -->", option_value[3], "<--"
## here we print out all the tags associated with this object
option_value = self.drawing.itemconfig(pgon, "tags")
print "pgon's tags are", option_value[4]
self.drawing.pack({'side': 'left'})
self.drawing.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -7,15 +7,14 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
self.button = Button(self, {"text" : "this is a button",
"command" : self.printhi})
self.button = Button(self, text="this is a button",
command=self.printhi)
# note here the coords are given in pixels (form the
# upper right and corner of the window, as usual for X)
@ -23,11 +22,9 @@ class Test(Frame):
# whatever...use the "anchor" option to control what point of the
# widget (in this case the button) gets mapped to the given x, y.
# you can specify corners, edges, center, etc...
self.draw.create_window(300, 300, {"window" : self.button})
self.draw.pack({'side': 'left'})
self.draw.create_window(300, 300, window=self.button)
self.draw.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -10,27 +10,23 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.question = Label(self, {"text": "Can Find The BLUE Square??????",
Pack : {"side" : "top"}})
self.QUIT = Button(self, {'text': 'QUIT',
'bg': 'red',
"height" : "3",
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
spacer = Frame(self, {"height" : "0.25i",
Pack : {"side" : "bottom"}})
self.question = Label(self, text="Can Find The BLUE Square??????")
self.question.pack()
self.QUIT = Button(self, text='QUIT', background='red',
height=3, command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
spacer = Frame(self, height="0.25i")
spacer.pack(side=BOTTOM)
# notice that the scroll region (20" x 20") is larger than
# displayed size of the widget (5" x 5")
self.draw = Canvas(self, {"width" : "5i",
"height" : "5i",
"bg" : "white",
"scrollregion" : "0i 0i 20i 20i"})
self.draw = Canvas(self, width="5i", height="5i",
background="white",
scrollregion=(0, 0, "20i", "20i"))
self.draw.scrollX = Scrollbar(self, {"orient" : "horizontal"})
self.draw.scrollY = Scrollbar(self, {"orient" : "vertical"})
self.draw.scrollX = Scrollbar(self, orient=HORIZONTAL)
self.draw.scrollY = Scrollbar(self, orient=VERTICAL)
# now tie the three together. This is standard boilerplate text
self.draw['xscrollcommand'] = self.draw.scrollX.set
@ -40,16 +36,13 @@ class Test(Frame):
# draw something. Note that the first square
# is visible, but you need to scroll to see the second one.
self.draw.create_polygon("0i", "0i", "3.5i", "0i", "3.5i", "3.5i", "0i" , "3.5i")
self.draw.create_polygon("10i", "10i", "13.5i", "10i", "13.5i", "13.5i", "10i" , "13.5i", "-fill", "blue")
self.draw.create_rectangle(0, 0, "3.5i", "3.5i", fill="black")
self.draw.create_rectangle("10i", "10i", "13.5i", "13.5i", fill="blue")
# pack 'em up
self.draw.scrollX.pack({'side': 'bottom',
"fill" : "x"})
self.draw.scrollY.pack({'side': 'right',
"fill" : "y"})
self.draw.pack({'side': 'left'})
self.draw.scrollX.pack(side=BOTTOM, fill=X)
self.draw.scrollY.pack(side=RIGHT, fill=Y)
self.draw.pack(side=LEFT)
def scrollCanvasX(self, *args):

View file

@ -1,58 +1,57 @@
from Tkinter import *
from Dialog import Dialog
# this shows how to create a new window with a button in it that can create new windows
# this shows how to create a new window with a button in it
# that can create new windows
class Test(Frame):
def printit(self):
print "hi"
def makeWindow(self):
# there is no Tkinter interface to the dialog box. Making one would mean putting
# a few wrapper functions in the Tkinter.py file.
# even better is to put in a SUIT-like selection of commonly-used dialogs.
"""Create a top-level dialog with some buttons.
This uses the Dialog class, which is a wrapper around the Tcl/Tk
tk_dialog script. The function returns 0 if the user clicks 'yes'
or 1 if the user clicks 'no'.
"""
# the parameters to this call are as follows:
d = Dialog(
self, ## name of a toplevel window
title="fred the dialog box",## title on the window
text="click on a choice", ## message to appear in window
bitmap="info", ## bitmap (if any) to appear;
## if none, use ""
# legal values here are:
# string what it looks like
# ----------------------------------------------
# error a circle with a slash through it
# grey25 grey square
# grey50 darker grey square
# hourglass use for "wait.."
# info a large, lower case "i"
# questhead a human head with a "?" in it
# question a large "?"
# warning a large "!"
# @fname X bitmap where fname is the path to the file
#
default=0, # the index of the default button choice.
# hitting return selects this
strings=("yes", "no"))
# values of the 'strings' key are the labels for the
# buttons that appear left to right in the dialog box
return d.num
fred = Toplevel() # a toplevel window that the dialog goes into
# this function returns the index of teh button chosen. In this case, 0 for "yes" and 1 for "no"
print self.tk.call("tk_dialog", # the command name
fred, # the name of a toplevel window
"fred the dialog box", # the title on the window
"click on a choice", # the message to appear in the window
"info", # the bitmap (if any) to appear. If no bitmap is desired, pass ""
# legal values here are:
# string what it looks like
# ----------------------------------------------
# error a circle with a slash through it
# grey25 grey square
# grey50 darker grey square
# hourglass use for "wait.."
# info a large, lower case "i"
# questhead a human head with a "?" in it
# question a large "?"
# warning a large "!"
# @fname any X bitmap where fname is the path to the file
#
"0", # the index of the default button choice. hitting return selects this
"yes", "no") # all remaining parameters are the labels for the
# buttons that appear left to right in the dialog box
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
# a hello button
self.hi_there = Button(self, {'text': 'Make a New Window',
'command' : self.makeWindow})
self.hi_there.pack({'side': 'left'})
self.hi_there = Button(self, text='Make a New Window',
command=self.makeWindow)
self.hi_there.pack(side=LEFT)
def __init__(self, master=None):

View file

@ -11,10 +11,10 @@ class App(Frame):
self.entrythingy = Entry()
self.entrythingy.pack()
self.button = Button(self, {"text" : "Uppercase The Entry", "command" : self.upper})
self.button = Button(self, text="Uppercase The Entry",
command=self.upper)
self.button.pack()
# here we have the text in the entry widget tied to a variable.
# changes in the variable are echoed in the widget and vice versa.
# Very handy.
@ -22,7 +22,7 @@ class App(Frame):
# the other variable types that can be shadowed
self.contents = StringVar()
self.contents.set("this is a variable")
self.entrythingy.config({"textvariable":self.contents})
self.entrythingy.config(textvariable=self.contents)
# and here we get a callback when the user hits return. we could
# make the key that triggers the callback anything we wanted to.

View file

@ -15,9 +15,8 @@ class Test(Frame):
def createWidgets(self):
# a hello button
self.hi_there = Button(self, {'text': 'Hello'})
self.hi_there.pack({'side': 'left'})
self.hi_there = Button(self, text='Hello')
self.hi_there.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -37,7 +37,6 @@ from Tkinter import *
def new_file():
print "opening new file"
def open_file():
print "opening OLD file"
@ -55,53 +54,43 @@ def print_anchovies():
def makeCommandMenu():
# make menu button
Command_button = Menubutton(mBar, {'text': 'Simple Button Commands',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Command_button = Menubutton(mBar, text='Simple Button Commands',
underline=0)
Command_button.pack(side=LEFT, padx="2m")
# make the pulldown part of the File menu. The parameter passed is the master.
# we attach it to the button as a python attribute called "menu" by convention.
# hopefully this isn't too confusing...
Command_button.menu = Menu(Command_button)
# just to be cute, let's disable the undo option:
Command_button.menu.add('command', {"label" : "Undo"} )
Command_button.menu.add_command(label="Undo")
# undo is the 0th entry...
Command_button.menu.entryconfig(0, {"state" : "disabled"})
Command_button.menu.entryconfig(0, state=DISABLED)
Command_button.menu.add('command', {'label': 'New...',
'underline': 0,
'command' : new_file})
Command_button.menu.add('command', {'label': 'Open...',
'underline': 0,
'command' : open_file})
Command_button.menu.add('command', {'label': 'Different Font',
'underline': 0,
'font' : '-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
'command' : print_something})
Command_button.menu.add_command(label='New...', underline=0,
command=new_file)
Command_button.menu.add_command(label='Open...', underline=0,
command=open_file)
Command_button.menu.add_command(label='Different Font', underline=0,
font='-*-helvetica-*-r-*-*-*-180-*-*-*-*-*-*',
command=print_something)
# we can make bitmaps be menu entries too. File format is X11 bitmap.
# if you use XV, save it under X11 bitmap format. duh-uh.,..
# Command_button.menu.add('command', {'bitmap' : '@/home/mjc4y/ftp/tcl/tk3.6/library/demos/bitmaps/face'})
Command_button.menu.add('command', {'bitmap' : '@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm'})
Command_button.menu.add_command(
bitmap="info")
#bitmap='@/home/mjc4y/dilbert/project.status.is.doomed.last.panel.bm')
# this is just a line
Command_button.menu.add('separator')
# change the color
Command_button.menu.add('command', {'label': 'Quit',
'underline': 0,
'background' : 'red',
'activebackground' : 'green',
'command': 'exit'})
Command_button.menu.add_command(label='Quit', underline=0,
background='red',
activebackground='green',
command=Command_button.quit)
# set up a pointer from the file menubutton back to the file menu
Command_button['menu'] = Command_button.menu
@ -111,10 +100,8 @@ def makeCommandMenu():
def makeCascadeMenu():
# make menu button
Cascade_button = Menubutton(mBar, {'text': 'Cascading Menus',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Cascade_button = Menubutton(mBar, text='Cascading Menus', underline=0)
Cascade_button.pack(side=LEFT, padx="2m")
# the primary pulldown
Cascade_button.menu = Menu(Cascade_button)
@ -126,24 +113,24 @@ def makeCascadeMenu():
Cascade_button.menu.choices.wierdones = Menu(Cascade_button.menu.choices)
# then you define the menus from the deepest level on up.
Cascade_button.menu.choices.wierdones.add('command', {'label' : 'avacado'})
Cascade_button.menu.choices.wierdones.add('command', {'label' : 'belgian endive'})
Cascade_button.menu.choices.wierdones.add('command', {'label' : 'beefaroni'})
Cascade_button.menu.choices.wierdones.add_command(label='avacado')
Cascade_button.menu.choices.wierdones.add_command(label='belgian endive')
Cascade_button.menu.choices.wierdones.add_command(label='beefaroni')
# definition of the menu one level up...
Cascade_button.menu.choices.add('command', {'label' : 'Chocolate'})
Cascade_button.menu.choices.add('command', {'label' : 'Vanilla'})
Cascade_button.menu.choices.add('command', {'label' : 'TuttiFruiti'})
Cascade_button.menu.choices.add('command', {'label' : 'WopBopaLoopBapABopBamBoom'})
Cascade_button.menu.choices.add('command', {'label' : 'Rocky Road'})
Cascade_button.menu.choices.add('command', {'label' : 'BubbleGum'})
Cascade_button.menu.choices.add('cascade', {'label' : 'Wierd Flavors',
'menu' : Cascade_button.menu.choices.wierdones})
Cascade_button.menu.choices.add_command(label='Chocolate')
Cascade_button.menu.choices.add_command(label='Vanilla')
Cascade_button.menu.choices.add_command(label='TuttiFruiti')
Cascade_button.menu.choices.add_command(label='WopBopaLoopBapABopBamBoom')
Cascade_button.menu.choices.add_command(label='Rocky Road')
Cascade_button.menu.choices.add_command(label='BubbleGum')
Cascade_button.menu.choices.add_cascade(
label='Wierd Flavors',
menu=Cascade_button.menu.choices.wierdones)
# and finally, the definition for the top level
Cascade_button.menu.add('cascade', {'label' : 'more choices',
'menu' : Cascade_button.menu.choices})
Cascade_button.menu.add_cascade(label='more choices',
menu=Cascade_button.menu.choices)
Cascade_button['menu'] = Cascade_button.menu
@ -152,10 +139,9 @@ def makeCascadeMenu():
def makeCheckbuttonMenu():
global fred
# make menu button
Checkbutton_button = Menubutton(mBar, {'text': 'Checkbutton Menus',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Checkbutton_button = Menubutton(mBar, text='Checkbutton Menus',
underline=0)
Checkbutton_button.pack(side=LEFT, padx='2m')
# the primary pulldown
Checkbutton_button.menu = Menu(Checkbutton_button)
@ -163,13 +149,13 @@ def makeCheckbuttonMenu():
# and all the check buttons. Note that the "variable" "onvalue" and "offvalue" options
# are not supported correctly at present. You have to do all your application
# work through the calback.
Checkbutton_button.menu.add('checkbutton', {'label': 'Pepperoni'})
Checkbutton_button.menu.add('checkbutton', {'label': 'Sausage'})
Checkbutton_button.menu.add('checkbutton', {'label': 'Extra Cheese'})
Checkbutton_button.menu.add_checkbutton(label='Pepperoni')
Checkbutton_button.menu.add_checkbutton(label='Sausage')
Checkbutton_button.menu.add_checkbutton(label='Extra Cheese')
# so here's a callback
Checkbutton_button.menu.add('checkbutton', {'label': 'Anchovy',
'command' : print_anchovies})
Checkbutton_button.menu.add_checkbutton(label='Anchovy',
command=print_anchovies)
# and start with anchovies selected to be on. Do this by
# calling invoke on this menu option. To refer to the "anchovy" menu
@ -196,10 +182,9 @@ def makeCheckbuttonMenu():
def makeRadiobuttonMenu():
# make menu button
Radiobutton_button = Menubutton(mBar, {'text': 'Radiobutton Menus',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Radiobutton_button = Menubutton(mBar, text='Radiobutton Menus',
underline=0)
Radiobutton_button.pack(side=LEFT, padx='2m')
# the primary pulldown
Radiobutton_button.menu = Menu(Radiobutton_button)
@ -207,16 +192,16 @@ def makeRadiobuttonMenu():
# and all the Radio buttons. Note that the "variable" "onvalue" and "offvalue" options
# are not supported correctly at present. You have to do all your application
# work through the calback.
Radiobutton_button.menu.add('radiobutton', {'label': 'Republican'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Commie'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Facist'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Labor Party'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Torie'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Independent'})
Radiobutton_button.menu.add('radiobutton', {'label': 'Anarchist'})
Radiobutton_button.menu.add('radiobutton', {'label': 'No Opinion'})
Radiobutton_button.menu.add_radiobutton(label='Republican')
Radiobutton_button.menu.add_radiobutton(label='Democrat')
Radiobutton_button.menu.add_radiobutton(label='Libertarian')
Radiobutton_button.menu.add_radiobutton(label='Commie')
Radiobutton_button.menu.add_radiobutton(label='Facist')
Radiobutton_button.menu.add_radiobutton(label='Labor Party')
Radiobutton_button.menu.add_radiobutton(label='Torie')
Radiobutton_button.menu.add_radiobutton(label='Independent')
Radiobutton_button.menu.add_radiobutton(label='Anarchist')
Radiobutton_button.menu.add_radiobutton(label='No Opinion')
# set up a pointer from the file menubutton back to the file menu
Radiobutton_button['menu'] = Radiobutton_button.menu
@ -225,25 +210,22 @@ def makeRadiobuttonMenu():
def makeDisabledMenu():
Dummy_button = Menubutton(mBar, {'text': 'Dead Menu',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Dummy_button = Menubutton(mBar, text='Dead Menu', underline=0)
Dummy_button.pack(side=LEFT, padx='2m')
# this is the standard way of turning off a whole menu
Dummy_button["state"] = "disabled"
Dummy_button["state"] = DISABLED
return Dummy_button
#################################################
#### Main starts here ...
root = Tk()
# make a menu bar
mBar = Frame(root, {'relief': 'raised',
'bd': 2,
Pack: {'side': 'top',
'fill': 'x'}})
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)
Command_button = makeCommandMenu()
Cascade_button = makeCascadeMenu()

View file

@ -43,33 +43,23 @@ def open_file():
def makeFileMenu():
# make menu button : "File"
File_button = Menubutton(mBar, {'text': 'File',
'underline': 0,
Pack: {'side': 'left',
'padx': '1m'}})
# make the pulldown part of the File menu. The parameter passed is the master.
# we attach it to the File button as a python attribute called "menu" by convention.
# hopefully this isn't too confusing...
File_button = Menubutton(mBar, text='File', underline=0)
File_button.pack(side=LEFT, padx="1m")
File_button.menu = Menu(File_button)
# add an item. The first param is a menu entry type,
# must be one of: "cascade", "checkbutton", "command", "radiobutton", "seperator"
# see menu-demo-2.py for examples of use
File_button.menu.add('command', {'label': 'New...',
'underline': 0,
'command' : new_file})
File_button.menu.add_command(label='New...', underline=0,
command=new_file)
File_button.menu.add('command', {'label': 'Open...',
'underline': 0,
'command' : open_file})
File_button.menu.add('command', {'label': 'Quit',
'underline': 0,
'command': 'exit'})
File_button.menu.add_command(label='Open...', underline=0,
command=open_file)
File_button.menu.add_command(label='Quit', underline=0,
command='exit')
# set up a pointer from the file menubutton back to the file menu
File_button['menu'] = File_button.menu
@ -78,22 +68,20 @@ def makeFileMenu():
def makeEditMenu():
Edit_button = Menubutton(mBar, {'text': 'Edit',
'underline': 0,
Pack: {'side': 'left',
'padx' : '1m'}})
Edit_button = Menubutton(mBar, text='Edit', underline=0)
Edit_button.pack(side=LEFT, padx="1m")
Edit_button.menu = Menu(Edit_button)
# just to be cute, let's disable the undo option:
Edit_button.menu.add('command', {"label" : "Undo"} )
Edit_button.menu.add('command', label="Undo")
# undo is the 0th entry...
Edit_button.menu.entryconfig(0, {"state" : "disabled"})
Edit_button.menu.entryconfig(0, state=DISABLED)
# and these are just for show. No "command" callbacks attached.
Edit_button.menu.add('command', {"label" : "Cut"} )
Edit_button.menu.add('command', {"label" : "Copy"} )
Edit_button.menu.add('command', {"label" : "Paste"} )
Edit_button.menu.add_command(label="Cut")
Edit_button.menu.add_command(label="Copy")
Edit_button.menu.add_command(label="Paste")
# set up a pointer from the file menubutton back to the file menu
Edit_button['menu'] = Edit_button.menu
@ -107,10 +95,8 @@ root = Tk()
# make a menu bar
mBar = Frame(root, {'relief': 'raised',
'bd': 2,
Pack: {'side': 'top',
'fill': 'x'}})
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)
File_button = makeFileMenu()
Edit_button = makeEditMenu()
@ -119,7 +105,6 @@ Edit_button = makeEditMenu()
# This allows for scanning from one menubutton to the next.
mBar.tk_menuBar(File_button, Edit_button)
root.title('menu demo')
root.iconname('packer')

View file

@ -4,19 +4,15 @@ from Tkinter import *
class Test(Frame):
def createWidgets(self):
self.Gpanel = Frame(self, {'width': '1i',
'height' : '1i',
'bg' : 'green'})
self.Gpanel.pack({'side' : 'left'})
self.Gpanel = Frame(self, width='1i', height='1i',
background='green')
self.Gpanel.pack(side=LEFT)
# a QUIT button
self.Gpanel.QUIT = Button(self.Gpanel, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.Gpanel.QUIT.pack( {'side': 'left'})
self.Gpanel.QUIT = Button(self.Gpanel, text='QUIT',
foreground='red',
command=self.quit)
self.Gpanel.QUIT.pack(side=LEFT)
def __init__(self, master=None):

View file

@ -4,25 +4,18 @@ from Tkinter import *
class Test(Frame):
def createWidgets(self):
self.Gpanel = Frame(self, {'width': '1i',
'height' : '1i',
'bg' : 'green'})
self.Gpanel = Frame(self, width='1i', height='1i',
background='green')
# this line turns off the recalculation of geometry by masters.
self.Gpanel.tk.call('pack', 'propagate', str(self.Gpanel), "0")
self.Gpanel.pack({'side' : 'left'})
self.Gpanel.propagate(0)
self.Gpanel.pack(side=LEFT)
# a QUIT button
self.Gpanel.QUIT = Button(self.Gpanel, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.Gpanel.QUIT.pack( {'side': 'left'})
self.Gpanel.QUIT = Button(self.Gpanel, text='QUIT', foreground='red',
command=self.quit)
self.Gpanel.QUIT.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -5,8 +5,7 @@ from Tkinter import *
def do_motion(event):
app.button.place({'x' : event.x,
'y' : event.y})
app.button.place(x=event.x, y=event.y)
def dothis():
print 'calling me!'
@ -16,27 +15,20 @@ def createWidgets(top):
# and the window containing is 400x400. We do this
# simply to show that this is possible. The rest of the
# area is inaccesssible.
f = Frame(top, {'width' : '200',
'height' : '200',
'bg' : 'green'})
f = Frame(top, width=200, height=200, background='green')
# note that we use a different manager here.
# This way, the top level frame widget resizes when the
# application window does.
f.pack({'fill' : 'both',
'expand' : 1})
f.pack(fill=BOTH, expand=1)
# now make a button
f.button = Button(f, {'fg' : 'red',
'text' : 'amazing',
'command' : dothis})
f.button = Button(f, foreground='red', text='amazing', command=dothis)
# and place it so that the nw corner is
# 1/2 way along the top X edge of its' parent
f.button.place({'relx' : '0.5',
'rely' : '0.0',
'anchor' : 'nw'})
f.button.place(relx=0.5, rely=0.0, anchor=NW)
# allow the user to move the button SUIT-style.
f.bind('<Control-Shift-Motion>', do_motion)

View file

@ -7,23 +7,20 @@ class Test(Frame):
def createWidgets(self):
# a hello button
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
self.hi_there = Button(self, text='Hello',
command=self.printit)
self.hi_there.pack(side=LEFT)
self.hi_there = Button(self, {'text': 'Hello',
'command' : self.printit})
self.hi_there.pack({'side': 'left'})
# note how Packer defaults to side=TOP
# note how Packer defaults to {'side': 'top'}
self.guy2 = Button(self, {'text': 'button 2'})
self.guy2 = Button(self, text='button 2')
self.guy2.pack()
self.guy3 = Button(self, {'text': 'button 3'})
self.guy3 = Button(self, text='button 3')
self.guy3.pack()
def __init__(self, master=None):

View file

@ -3,8 +3,7 @@ from Tkinter import *
# This is a program that tests the placer geom manager
def do_motion(event):
app.button.place({'x' : event.x,
'y' : event.y})
app.button.place(x=event.x, y=event.y)
def dothis():
print 'calling me!'
@ -14,27 +13,20 @@ def createWidgets(top):
# and the window containing is 400x400. We do this
# simply to show that this is possible. The rest of the
# area is inaccesssible.
f = Frame(top, {'width' : '200',
'height' : '200',
'bg' : 'green'})
f = Frame(top, width=200, height=200, background='green')
# place it so the upper left hand corner of
# the frame is in the upper left corner of
# the parent
f.place({'relx' : '0.0',
'rely' : '0.0'})
f.place(relx=0.0, rely=0.0)
# now make a button
f.button = Button(f, {'fg' : 'red',
'text' : 'amazing',
'command' : dothis})
f.button = Button(f, foreground='red', text='amazing', command=dothis)
# and place it so that the nw corner is
# 1/2 way along the top X edge of its' parent
f.button.place({'relx' : '0.5',
'rely' : '0.0',
'anchor' : 'nw'})
f.button.place(relx=0.5, rely=0.0, anchor=NW)
# allow the user to move the button SUIT-style.
f.bind('<Control-Shift-Motion>', do_motion)

View file

@ -5,31 +5,28 @@ import string
class Pong(Frame):
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
## The playing field
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw = Canvas(self, width="5i", height="5i")
## The speed control for the ball
self.speed = Scale(self, {"orient": "horiz",
"label" : "ball speed",
"from" : -100,
"to" : 100})
self.speed = Scale(self, orient=HORIZONTAL, label="ball speed",
from_=-100, to=100)
self.speed.pack({'side': 'bottom', "fill" : "x"})
self.speed.pack(side=BOTTOM, fill=X)
# The ball
self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i", {"fill" : "red"})
self.ball = self.draw.create_oval("0i", "0i", "0.10i", "0.10i",
fill="red")
self.x = 0.05
self.y = 0.05
self.velocity_x = 0.3
self.velocity_y = 0.5
self.draw.pack({'side': 'left'})
self.draw.pack(side=LEFT)
def moveBall(self, *args):
if (self.x > 5.0) or (self.x < 0.0):
@ -44,8 +41,6 @@ class Pong(Frame):
self.draw.move(self.ball, `deltax` + "i", `deltay` + "i")
self.after(10, self.moveBall)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -8,26 +8,23 @@ class Test(Frame):
###################################################################
def mouseDown(self, event):
# see if we're inside a dot. If we are, it
# gets tagged as "current" for free by tk.
# gets tagged as CURRENT for free by tk.
if not event.widget.find_withtag("current"):
if not event.widget.find_withtag(CURRENT):
# there is no dot here, so we can make one,
# and bind some interesting behavior to it.
# ------
# create a dot, and mark it as current
fred = self.draw.create_oval(event.x - 10, event.y -10, event.x +10, event.y + 10,
{"fill" : "green", "tag" : "current"})
self.draw.bind(fred, "<Any-Enter>", self.mouseEnter)
self.draw.bind(fred, "<Any-Leave>", self.mouseLeave)
fred = self.draw.create_oval(
event.x - 10, event.y -10, event.x +10, event.y + 10,
fill="green")
self.draw.tag_bind(fred, "<Enter>", self.mouseEnter)
self.draw.tag_bind(fred, "<Leave>", self.mouseLeave)
self.lastx = event.x
self.lasty = event.y
def mouseMove(self, event):
self.draw.move("current", event.x - self.lastx, event.y - self.lasty)
self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
self.lastx = event.x
self.lasty = event.y
@ -37,23 +34,21 @@ class Test(Frame):
def mouseEnter(self, event):
# the "current" tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "red"})
print self.tk.splitlist(self.draw.coords("current"))
self.draw.itemconfig(CURRENT, fill="red")
print self.draw.coords(CURRENT)
def mouseLeave(self, event):
# the "current" tag is applied to the object the cursor is over.
# this happens automatically.
self.draw.itemconfig("current", {"fill" : "blue"})
self.draw.itemconfig(CURRENT, fill="blue")
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
self.draw.pack({'side': 'left'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
self.draw = Canvas(self, width="5i", height="5i")
self.draw.pack(side=LEFT)
Widget.bind(self.draw, "<1>", self.mouseDown)
Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)

View file

@ -26,34 +26,30 @@ class Test(Frame):
# 'variable' is the name of the variable that all these radio buttons share
# 'value' is the value this variable takes on when the radio button is selected
# 'anchor' makes the text appear left justified (default is centered. ick)
self.radioframe.choc = Radiobutton (self.radioframe, {"text" : "Chocolate Flavor",
"variable" : self.flavor,
"value" : "chocolate",
"anchor" : "w",
Pack : {"side" : "top", "fill" : "x"}})
self.radioframe.choc = Radiobutton(
self.radioframe, text="Chocolate Flavor",
variable=self.flavor, value="chocolate",
anchor=W)
self.radioframe.choc.pack(fill=X)
self.radioframe.straw = Radiobutton (self.radioframe, {"text" : "Strawberry Flavor",
"variable" : self.flavor,
"anchor" : "w",
"value" : "strawberry",
Pack : {"side" : "top", "fill" : "x"}})
self.radioframe.lemon = Radiobutton (self.radioframe, {"text" : "Lemon Flavor",
"anchor" : "w",
"variable" : self.flavor,
"value" : "lemon",
Pack : {"side" : "top", "fill" : "x"}})
self.radioframe.straw = Radiobutton(
self.radioframe, text="Strawberry Flavor",
variable=self.flavor, value="strawberry",
anchor=W)
self.radioframe.straw.pack(fill=X)
self.radioframe.lemon = Radiobutton(
self.radioframe, text="Lemon Flavor",
variable=self.flavor, value="lemon",
anchor=W)
self.radioframe.lemon.pack(fill=X)
# this is a text entry that lets you type in the name of a flavor too.
self.entry = Entry(self, {"textvariable" : self.flavor,
Pack : {"side" : "top", "fill" : "x"}})
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.entry = Entry(self, textvariable=self.flavor)
self.entry.pack(fill=X)
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
def __init__(self, master=None):

View file

@ -5,15 +5,15 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'bg': 'red',
'fg': 'white',
'height' : 3,
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT',
background='red',
foreground='white',
height=3,
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.canvasObject = Canvas(self, {"width" : "5i", "height" : "5i"})
self.canvasObject.pack({'side': 'left'})
self.canvasObject = Canvas(self, width="5i", height="5i")
self.canvasObject.pack(side=LEFT)
def mouseDown(self, event):
# canvas x and y take the screen coords from the event and translate
@ -29,7 +29,8 @@ class Test(Frame):
if (self.startx != event.x) and (self.starty != event.y) :
self.canvasObject.delete(self.rubberbandBox)
self.rubberbandBox = self.canvasObject.create_rectangle(self.startx, self.starty, x, y)
self.rubberbandBox = self.canvasObject.create_rectangle(
self.startx, self.starty, x, y)
# this flushes the output, making sure that
# the rectangle makes it to the screen
# before the next event is handled
@ -50,7 +51,7 @@ class Test(Frame):
Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown)
Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion)
Widget.bind(self.canvasObject, "<Button1-ButtonRelease>", self.mouseUp)
test = Test()

View file

@ -5,15 +5,15 @@ class Test(Frame):
print "hi"
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'bg': 'red',
'fg': 'white',
'height' : 3,
'command': self.quit})
self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT',
background='red',
foreground='white',
height=3,
command=self.quit)
self.QUIT.pack(side=BOTTOM, fill=BOTH)
self.canvasObject = Canvas(self, {"width" : "5i", "height" : "5i"})
self.canvasObject.pack({'side': 'left'})
self.canvasObject = Canvas(self, width="5i", height="5i")
self.canvasObject.pack(side=LEFT)
def mouseDown(self, event):
# canvas x and y take the screen coords from the event and translate
@ -29,7 +29,8 @@ class Test(Frame):
if (self.startx != event.x) and (self.starty != event.y) :
self.canvasObject.delete(self.rubberbandLine)
self.rubberbandLine = self.canvasObject.create_line(self.startx, self.starty, x, y)
self.rubberbandLine = self.canvasObject.create_line(
self.startx, self.starty, x, y)
# this flushes the output, making sure that
# the rectangle makes it to the screen
# before the next event is handled

View file

@ -11,25 +11,21 @@ class Test(Frame):
self.slider.set(0)
def createWidgets(self):
self.slider = Scale(self, {"from" : 0,
'to': 100,
"orient" : "horizontal",
"length" : "3i",
"label" : "happy slider",
'command' : self.print_value})
self.slider = Scale(self, from_=0, to=100,
orient=HORIZONTAL,
length="3i",
label="happy slider",
command=self.print_value)
self.reset = Button(self, {'text': 'reset slider',
'command': self.reset})
self.reset = Button(self, text='reset slider',
command=self.reset)
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.slider.pack({'side': 'left'})
self.reset.pack({'side': 'left'})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.slider.pack(side=LEFT)
self.reset.pack(side=LEFT)
self.QUIT.pack(side=LEFT, fill=BOTH)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -11,22 +11,18 @@ class New_Button(Button):
def createWidgets(top):
f = Frame(top)
f.pack()
f.QUIT = Button(f, {'text': 'QUIT',
'fg': 'red',
'command': top.quit})
f.QUIT.pack({'side': 'left', 'fill': 'both'})
f.QUIT = Button(f, text='QUIT', foreground='red', command=top.quit)
f.QUIT.pack(side=LEFT, fill=BOTH)
# a hello button
f.hi_there = New_Button(f, {'text': 'Hello'})
f.hi_there = New_Button(f, text='Hello')
# we do this on a different line because we need to reference f.hi_there
f.hi_there.config({'command' : f.hi_there.callback})
f.hi_there.pack({'side': 'left'})
f.hi_there.config(command=f.hi_there.callback)
f.hi_there.pack(side=LEFT)
f.hi_there.counter = 43
root = Tk()
createWidgets(root)
root.mainloop()

View file

@ -19,29 +19,27 @@ from Tkinter import *
def makePoliticalParties():
def makePoliticalParties(var):
# make menu button
Radiobutton_button = Menubutton(mBar, {'text': 'Political Party',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Radiobutton_button = Menubutton(mBar, text='Political Party',
underline=0)
Radiobutton_button.pack(side=LEFT, padx='2m')
# the primary pulldown
Radiobutton_button.menu = Menu(Radiobutton_button)
Radiobutton_button.menu.add('radiobutton', {'label': 'Republican',
'variable' : party,
'value' : 1})
Radiobutton_button.menu.add_radiobutton(label='Republican',
variable=var, value=1)
Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat',
'variable' : party,
'variable' : var,
'value' : 2})
Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian',
'variable' : party,
'variable' : var,
'value' : 3})
party.set(2)
var.set(2)
# set up a pointer from the file menubutton back to the file menu
Radiobutton_button['menu'] = Radiobutton_button.menu
@ -49,29 +47,26 @@ def makePoliticalParties():
return Radiobutton_button
def makeFlavors():
def makeFlavors(var):
# make menu button
Radiobutton_button = Menubutton(mBar, {'text': 'Flavors',
'underline': 0,
Pack: {'side': 'left',
'padx': '2m'}})
Radiobutton_button = Menubutton(mBar, text='Flavors',
underline=0)
Radiobutton_button.pack(side=LEFT, padx='2m')
# the primary pulldown
Radiobutton_button.menu = Menu(Radiobutton_button)
Radiobutton_button.menu.add('radiobutton', {'label': 'Strawberry',
'variable' : flavor,
'value' : 'Strawberry'})
Radiobutton_button.menu.add_radiobutton(label='Strawberry',
variable=var, value='Strawberry')
Radiobutton_button.menu.add('radiobutton', {'label': 'Chocolate',
'variable' : flavor,
'value' : 'Chocolate'})
Radiobutton_button.menu.add_radiobutton(label='Chocolate',
variable=var, value='Chocolate')
Radiobutton_button.menu.add('radiobutton', {'label': 'Rocky Road',
'variable' : flavor,
'value' : 'Rocky Road'})
Radiobutton_button.menu.add_radiobutton(label='Rocky Road',
variable=var, value='Rocky Road')
# choose a default
flavor.set("Chocolate")
var.set("Chocolate")
# set up a pointer from the file menubutton back to the file menu
Radiobutton_button['menu'] = Radiobutton_button.menu
@ -82,7 +77,7 @@ def makeFlavors():
def printStuff():
print "party is", party.get()
print "flavor is", flavor.get()
print ""
print
#################################################
#### Main starts here ...
@ -90,27 +85,24 @@ root = Tk()
# make a menu bar
mBar = Frame(root, {'relief': 'raised',
'bd': 2,
Pack: {'side': 'top',
'fill': 'x'}})
mBar = Frame(root, relief=RAISED, borderwidth=2)
mBar.pack(fill=X)
# make two application variables,
# one to control each radio button set
party = IntVar()
flavor = StringVar()
Radiobutton_button = makePoliticalParties()
Radiobutton_button2 = makeFlavors()
Radiobutton_button = makePoliticalParties(party)
Radiobutton_button2 = makeFlavors(flavor)
# finally, install the buttons in the menu bar.
# This allows for scanning from one menubutton to the next.
mBar.tk_menuBar(Radiobutton_button, Radiobutton_button2)
b = Button(root, {"text": "print party and flavor",
"command" : printStuff,
"fg": "red"})
b.pack({"side" : "top"})
b = Button(root, text="print party and flavor", foreground="red",
command=printStuff)
b.pack(side=TOP)
root.title('menu demo')
root.iconname('menu demo')

View file

@ -1,6 +1,7 @@
from Tkinter import *
# this shows how to create a new window with a button in it that can create new windows
# this shows how to create a new window with a button in it
# that can create new windows
class Test(Frame):
def printit(self):
@ -8,24 +9,21 @@ class Test(Frame):
def makeWindow(self):
fred = Toplevel()
fred.label = Button(fred, {'text': "This is window number " + `self.windownum` + "." ,
'command' : self.makeWindow})
fred.label = Button(fred,
text="This is window number %d." % self.windownum,
command=self.makeWindow)
fred.label.pack()
self.windownum = self.windownum + 1
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack(side=LEFT, fill=BOTH)
# a hello button
self.hi_there = Button(self, {'text': 'Make a New Window',
'command' : self.makeWindow})
self.hi_there.pack({'side': 'left'})
self.hi_there = Button(self, text='Make a New Window',
command=self.makeWindow)
self.hi_there.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -8,22 +8,19 @@ class Test(Frame):
def makeWindow(self):
fred = Toplevel()
fred.label = Label(fred, {'text': "Here's a new window",})
fred.label = Label(fred, text="Here's a new window")
fred.label.pack()
def createWidgets(self):
self.QUIT = Button(self, {'text': 'QUIT',
'fg': 'red',
'command': self.quit})
self.QUIT = Button(self, text='QUIT', foreground='red',
command=self.quit)
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT.pack(side=LEFT, fill=BOTH)
# a hello button
self.hi_there = Button(self, {'text': 'Make a New Window',
'command' : self.makeWindow})
self.hi_there.pack({'side': 'left'})
self.hi_there = Button(self, text='Make a New Window',
command=self.makeWindow)
self.hi_there.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)

View file

@ -4,15 +4,15 @@ import sys
sys.path.append("/users/mjc4y/projects/python/tkinter/utils")
from TkinterUtils import *
# this shows how to create a new window with a button in it that can create new windows
# this shows how to create a new window with a button in it that
# can create new windows
class Test(Frame):
def makeWindow(self, *args):
fred = Toplevel()
fred.label = Canvas (fred, {"width" : "2i",
"height" : "2i"})
fred.label = Canvas (fred, width="2i", height="2i")
fred.label.create_line("0", "0", "2i", "2i")
fred.label.create_line("0", "2i", "2i", "0")
@ -22,14 +22,12 @@ class Test(Frame):
def createWidgets(self):
self.QUIT = QuitButton(self)
self.QUIT.pack({'side': 'left', 'fill': 'both'})
self.QUIT.pack(side=LEFT, fill=BOTH)
self.makeWindow = Button(self, {'text': 'Make a New Window',
'width' : 50,
'height' : 20,
'command' : self.makeWindow})
self.makeWindow.pack({'side': 'left'})
self.makeWindow = Button(self, text='Make a New Window',
width=50, height=20,
command=self.makeWindow)
self.makeWindow.pack(side=LEFT)
def __init__(self, master=None):
Frame.__init__(self, master)