Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Button Widget Construction and Enhancement in Python, Study notes of Software Engineering

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

Pre 2010

Uploaded on 08/19/2009

koofers-user-bfx-2
koofers-user-bfx-2 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
WIDGETS AND DATA
COLLECTIONS
CSSE 120—Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Button Widget Construction and Enhancement in Python and more Study notes Software Engineering in PDF only on Docsity!

WIDGETS AND DATACOLLECTIONS CSSE 120—Rose Hulman Institute of Technology

Widgets

"Widget" is a generic name for a component of aGraphical User interface(such as a button, window,menu, or textbox).

Our simple Python graphics module only providesone kind of widget.

We can create our own.

Create a button widget.

Constructor for Button class

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.

Private methods used by init()^ def

__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

Constraints and research

Fixed-width font (Courier). Why?

Fixed font size (I chose 20 point).

Experiments indicated that each character is about16 pixels wide and about 34 pixels tall.

Thus these class variables:

MIN_HEIGHT =

# A button should be at least this tall, # in order to comfortably surround the text. FONT_NAME = 'courier' FONT_SIZE =

CHAR_WIDTH =

# The width of a character (in pixels) of # the chosen font face/size. EXTRA_HORZ_SPACE =

**# Put in this much extra horizontal

space so that the label doesn't run # into the end of the rectangle.**

Enhance the Button Class

From your SVN repository, checkout theButtonWidget project.

In Button.py, add code to guarantee that a button'srectangle will be large enough to enclose its text.

Then go to ColorButtons.py.^ †

Study the code that is there (especially the event loop)Add code to create the buttonList.

Add the

re-enable all

button and make it work.

Commit the new version to your repository.

Pipe Dreams – by Animusic

As you watch, think about how Objects could makethe code for this easier to write.

Each object (ball, string, xylophone key, etc.) knowsits own physical characteristics, position, velocity, aswell as how it reacts to striking or being struck byanother object.

There could be a loop that calls

timePasses()

for

each object in the picture.

Each object does what it would do in that time, anddraws itself in its new position.

Some Basic List Operations

Do the same thing to each object in a list

find the largest number in a list of numbers.

find the second largest element.

Find the point in a list that is farthest away from agiven point.

Find the point in a list which, when chosen as thecenter, can enclose all of the points in the smallestcircle

Write the following functions

1. def doubleAll(list): """ returns a list of numbers that are twice those in the original list. """ **2. def largestInList(numList):

A nonempty list of numbers**

""" returns the largest number in the list. """ **3. def secondLargest(numList):

numList contains at least 2 numbers, all different** """ returns the second largest number in the list """

4. def farthestPoint(pointList, p): """return the point in pointList that is farthest from point p """