







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth look into the creation and enhancement of a button widget in python, using the rose hulman institute of technology's csse 120 module as a reference. The button class, its methods and instance variables, and solutions to improve the constructor's functionality. Additionally, it discusses potential improvements using object-oriented programming and basic list operations.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!
class Button: """ A Button widget similar to the one in Zelle 10.6, but with the extra property of making sure the Button's text fits inside the rectangle """ FONT_NAME = 'courier' FONT_SIZE =
def init( self, win, label, center, width=0, height=0): """Initialize a Button with the given characteristics. """ self _._setRect(width, height, center) self _._setText(label, center) self .enable() self .rect.draw(win) self .text.draw(win) class variables (used as constants here) put complex tasks intoseparate methods, tokeep constructor simple methods (like __setRect) whose names begin with two underscores (and donot also end with two underscores) are intended to be used as "private" helpermethods, only called from other methods from the same class.
__setRect( self, width, height, center): """ Internal method. Called by the constructor. Create the rectangular border of the button """ centerX, centerY = center.getX(), center.getY() self.minX = centerX - width/2 self.minY = centerY - height/2 self.maxX = centerX + width/2 self.maxY = centerY + height/2 self.rect = Rectangle(Point(self.minX, self.minY), Point(self.maxX, self.maxY)) def __setText( self, label, center): """ Internal method. Called by the constructor. Create the Text object for the button's label""" self.text = Text(center, label) self.text.setFace(Button.FONT_NAME) self.text.setSize(Button.FONT_SIZE) Note the useof the classvariables
# A button should be at least this tall, # in order to comfortably surround the text. FONT_NAME = 'courier' FONT_SIZE =
# The width of a character (in pixels) of # the chosen font face/size. EXTRA_HORZ_SPACE =
**# Put in this much extra horizontal
1. def doubleAll(list): """ returns a list of numbers that are twice those in the original list. """ **2. def largestInList(numList):
""" returns the largest number in the list. """ **3. def secondLargest(numList):
4. def farthestPoint(pointList, p): """return the point in pointList that is farthest from point p """