[Edu-sig] Python Help - Thanks
Randy Latimer
rlatimer@tjhsst.edu
Tue, 20 May 2003 08:58:11 -0400 (EDT)
One of my students is working on a Python + Tkinter in order to construct
a "Drawing" program.
class base - constructs the entire window for her drawing interface
class panel - constructs a vertical tool bar on the left side of the
interface. Functions will exist for Circle, Square,
Triangle, Color, Rotate, Erase, Pen
class easel - constructs the drawing portion of the window.
Here's the problem:
She's defining a a DrawCircle function in class easel (the drawing
canvas)
The button for "Circle" is defined in the "panel" - tool bar.
She's trying to call the DrawCircle function when she left clicks
on the Circle button.
But nothing happens now when the Circel button is pressed.
Thanks for any advice out there -
Randy Latimer, rlatimer@tjhsst.edu
Here's the program, "Vers3.py"
from Tkinter import *
import sys
#class of the geometric figures. Base is the frame it's called in
class base(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
self.pack()
self.Makebase()
def Makebase(self):
panel(self).pack(side=LEFT)
easel(self).pack(side=RIGHT)
class panel(Frame):
def __init__(self, parent=Frame, **config):
Frame.__init__(self, parent, config)
MyCanvas = Canvas()
MyEasel = easel(MyCanvas)
self.pack(side = TOP, fill = BOTH)
self.MakeGUI(MyEasel,MyCanvas)
def MakeGUI(self,MyEasel,MyCanvas):
Circle = Button (self,text = "Circle") #creates the
#button on the root frame
Circle.pack(side = TOP,fill = X, anchor = N)
#'packs' the button, putting it on the frame and
# specifying resizing options
Circle.bind('<Button-1>', self.circle(self, MyEasel, MyCanvas))
# registers mouse event and sends it to the circle()
# function
Square = Button (self, text = "Square")
Square.pack(side = TOP, fill = X, anchor = N)
Square.bind('<Button-1>', self.square)
Triangle = Button (self, text = "Triangle")
Triangle.pack(side = TOP, fill = X, anchor = N)
Triangle.bind('<Button-1>', self.triangle)
Color = Button (self, text = "Color")
Color.pack(side = TOP, fill = X, anchor = N)
Color.bind('<Button-1>', self.color)
Rotate = Button (self, text = "Rotate")
Rotate.pack(side = TOP, fill = X, anchor = N)
Rotate.bind('<Button-1>', self.rotate)
Erase = Button (self, text = "Erase")
Erase.pack(side = TOP, fill = X, anchor = N)
Erase.bind('<Button-1>', self.erase)
Pen = Button (self, text = "Pen")
Pen.pack(side = TOP, fill = X, anchor = N)
Pen.bind('<Button-1>', self.pen)
def circle(self, event, MyEasel, MyCanvas):
MyEasel.DrawCircle(MyCanvas)
#print "eventually I'll draw a circle but for now
# I just quit"
#import sys; sys.exit() #kills windows
def square(self, event):
print "eventually I'll draw a square but for now I just quit"
import sys; sys.exit() #kills windows
def triangle(self, event):
print "eventually I'll draw a triangle but for now I just quit"
import sys; sys.exit() #kills windows
def color(self, event):
print "eventually I'll change colors but for now I just quit"
import sys; sys.exit() #kills windows
def rotate(self, event):
print "eventually I'll rotate objects but for now I just quit"
import sys; sys.exit() #kills windows
def erase(self, event):
print "eventually I'll erase but for now I just quit"
import sys; sys.exit() #kills windows
def pen(self, event):
print "eventually I'll draw with a pen but for now I just quit"
import sys; sys.exit() #kills windows
class easel(Canvas):
def __init__(self, parent=Frame, **config):
Canvas.__init__(self, parent, config)
self.pack(side = TOP, fill = BOTH, expand = YES)
canvas = Canvas(self, width = 500, height=200, bg = 'white')
canvas.config(scrollregion =(0,0,500,2000))
sbar = Scrollbar(self)
sbar.config(command = canvas.yview)
canvas.config(yscrollcommand=sbar.set)
sbar.pack(side = RIGHT, fill=Y)
canvas.pack(side = RIGHT, expand = YES, fill = BOTH)
self.canvas = canvas
def DrawCircle(self, canvas):
canvas.create_oval(10,20,200,200,width=2,fill="blue")
if __name__ == '__main__': base().mainloop()