Hello, it is me again with another homework assignment. I am having issues starting this one. Every draft I make for it feels way too long and that I can make it much, much shorter.

First, I am using Python version 2.3.2. I am using graphics.py, buttons.py, and am basing charview.py on dieview.py from Zelle's website. I am running on Window's Vista. I am also suppose to base my program on roller.py from Zelle's website, but I am having difficulty understanding everything regarding the interactions between my program, charview.py, and buttons.py.

The assignment:
This program creates a 4-digit counter with up and down buttons for each digit. The following is required:
-1) The clear button is only active when a non-zero value is displayed
-2) The value must always be between 0 and 9990. Decreasing by an amount greater than display will result in 0000.
-3) The display must show "- - - -" before any clicks.
-4) Must use the Button class from Zelle unchanged
-5) Must create a module named charview (charview.py) containing the class CharView.
-6) Must create counter.py that contains main() and any other support functions.
-7) Window must be 400x300 and arrange the up and down buttons beneath the appropriate digit, with up on top and down below. The clear button should be centered and quit button should be beneath it.

I know charview.py is not complete, but I am drawing nothing but blanks as to where I should start next. Here is what I currently have for charview.py:

#A widget for displaying the digits of the number counter

from graphics import *

class charView:
    """ charView is a widget that displays a graphical representation of a number counter."""
    def __init__(self, win, center, size):
        """Create a view of a digit"""

        # first define some standard values
        self.win = win # save this for drawing pips later
        self.background = "white" # color of the background
        self.foreground = "black" # color of the numbers
        hsize = size / 2.0  # half the size of the die
        
        # create a square for the face
        cx, cy = center.getX(), center.getY()
        p1 = Point(cx-hsize, cy-hsize)
        p2 = Point(cx+hsize, cy+hsize)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill(self.background)

        # Draw an initial value
        self.setValue(1)

    def setValue(self, value):
        """ Set this digit to display value."""
        # turn all digits off

        # turn correct digit value on
        if value == 1:
        elif value == 2:
        elif value == 3:
        elif value == 4:
        elif value == 5:
        elif value == 6:
        elif value == 7:
        elif value == 8:
        elif value == 9:
        else:

I am fairly certain that it will be easy to increase and decrease the value. The hard part is displaying the value when an increase or decrease occurs. As such, here is the code I currently have to counter.py:

from graphics import GraphWin, Point

from button import button
from charView import charView
def main():

    #create the application window
    win = GraphWin("Up-Down Counter")
    win.setCoords(0,0,400,300)
    win.setBackground("cyan")

    #Draw the interface widgets
    thousands = charView(win, Point(49.75,250), 20)
    hundreds = charView(win, Point(149.25,250), 20)
    tens = charView(win, Point(248.75,250), 20)
    ones = charView(win, Point(348.25,250), 20)

    #create the buttons to increase and decrease each digit
    #Spaced by ones, tens, hundreds, and thousands to make reading easier
    oIB = Button(win, Point(348.25,200), 6, 1, "+1") # oIB stands for onesIncreaseButton
    oIB.activate()
    oDB = Button(win, Point(348.25,150), 6, 1, "-1")    #oDB stands for onesDecreaseButton
    oDB.activate()
    
    tIB = Button(win, Point(248.75,200), 6, 1, "+10")   #tIB stands for tensIncreaseButton
    tIB.activate()
    tDB = Button(win, Point(248.75,150), 6, 1, "-10")   #tDB stands for tensDecreaseButton
    tDB.activate()
    
    hIB = Button(win, Point(149.75,200), 6, 1, "+100")  #hIB stands for hundredsIncreaseButton
    hIB.activate()
    hDB = Button(win, Point(149.75,150), 6, 1, "-100")  #hDB stands for hundredsDecreaseButton
    hDB.activate()
    
    kIB = Button(win, Point(49.75,200), 6, 1, "+1000")    #kIB stands for thousandsIncreaseButton
    kIB.activate()
    kDB = Button(win, Point(49.75,150), 6, 1, "-1000")    #kDB stands for thousandsDecreaseButton
    kDB.activate()

    #create the clear and quit buttons
    clearButton = Button(win, Point(200,100), 2, 1, "Clear")
    quitButton = Button(win, Point(200,50), 2, 1, "Quit")

    while not quitButton.clicked(pt):
        pt = win.getMouse()

That is as far as I have been able to get on my own. This has been assigned for about a week, and I have about a week left to complete it. I would prefer suggestions over someone else solving this assignment for me. Thank you to anyone who offers suggestions.

I have continued working on the assignment, and this I what I have accomplished in the last 24 hours:

upDownCounter.py

from graphics import GraphWin, Point

from button import Button
from charView import CharView
def main():
    #create the application window
    win = GraphWin("Up-Down Counter")
    win.setCoords(0,0,400,300)
    win.setBackground("cyan")

    #Draw the interface widgets
    thousands = CharView(win, Point(49.75,250), 20)
    hundreds = CharView(win, Point(149.25,250), 20)
    tens = CharView(win, Point(248.75,250), 20)
    ones = CharView(win, Point(348.25,250), 20)

    #create the definitions
    count = 0
    #create the buttons to increase and decrease each digit
    #Spaced by ones, tens, hundreds, and thousands to make reading easier
    oIB = Button(win, Point(348.25,200), 6, 1, "+1") # oIB stands for onesIncreaseButton
    oIB.activate()
    oDB = Button(win, Point(348.25,150), 6, 1, "-1")    #oDB stands for onesDecreaseButton
    oDB.activate()
    
    tIB = Button(win, Point(248.75,200), 6, 1, "+10")   #tIB stands for tensIncreaseButton
    tIB.activate()
    tDB = Button(win, Point(248.75,150), 6, 1, "-10")   #tDB stands for tensDecreaseButton
    tDB.activate()
    
    hIB = Button(win, Point(149.75,200), 6, 1, "+100")  #hIB stands for hundredsIncreaseButton
    hIB.activate()
    hDB = Button(win, Point(149.75,150), 6, 1, "-100")  #hDB stands for hundredsDecreaseButton
    hDB.activate()
    
    kIB = Button(win, Point(49.75,200), 6, 1, "+1000")    #kIB stands for thousandsIncreaseButton
    kIB.activate()
    kDB = Button(win, Point(49.75,150), 6, 1, "-1000")    #kDB stands for thousandsDecreaseButton
    kDB.activate()

    #create the clear and quit buttons
    clearButton = Button(win, Point(200,100), 2, 1, "Clear")
    quitButton = Button(win, Point(200,50), 2, 1, "Quit")

    pt = win.getMouse()
    while not quitButton.clicked(pt):
        if oIB.clicked(pt):
            count += 1            
        if oDB.clicked(pt):
            count -= 1            
        if tIB.clicked(pt):
            count += 10
        if tDB.clicked(pt):
            count -= 10
        if hIB.clicked(pt):
            count += 100
        if hDB.clicked(pt):
            count -= 100
        if kIB.clicked(pt):
            count += 1000
        if kDB.clicked(pt):
            count -= 1000
        if clearButton.clicked(pt):
            count = str(count)
            count = "----"
if __name__ == '__main__':
    main()

charView.py

from graphics import *

class CharView:
    """ charView is a widget that displays a graphical representation of a number counter."""
    def __init__(self, win, center, size):
        """Create a view of a digit, e.g.: d1 = GDie(myWin, Point(40,50), 20) creates a die
        centered at (40,50) having sides of length 20."""

        # first define some standard values
        self.win = win # save this for drawing pips later
        self.background = "white" # color of the background
        self.foreground = "black" # color of the numbers
        hsize = size / 2.0  # half the size of the die
        
        # create a square for the face
        cx, cy = center.getX(), center.getY()
        p1 = Point(cx-hsize, cy-hsize)
        p2 = Point(cx+hsize, cy+hsize)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill(self.background)

        # Create 7 circles for standard pip locations
        self.zero = self.__makeDigit(cx, cy, '0')
        self.one = self.__makeDigit(cx, cy, '1')
        self.two = self.__makeDigit(cx, cy, '2')
        self.three = self.__makeDigit(cx, cy, '3')
        self.four = self.__makeDigit(cx, cy, '4')
        self.five = self.__makeDigit(cx, cy, '5')
        self.six = self.__makeDigit(cx, cy, '6')
        self.seven = self.__makeDigit(cx, cy, '7')
        self.eight = self.__makeDigit(cx, cy, '8')
        self.nine = self.__makeDigit(cx, cy, '9')
        self.blank = self.__makeDigit(cx, cy, '-')

        # Draw an initial value
        self.setValue(0)
        
    def __makeDigit(self, x, y, num):
        """Internal helper method to draw a pip at (x,y)"""
        digit = Text(Point(x,y), num)
        digit.setFill(self.background)
        digit.setOutline(self.background)
        digit.draw(self.win)
        return digit

    def setValue(self, value):
        """ Set this die to display value."""
        # turn all pips off
        self.zero.setFill(self.background)
        self.one.setFill(self.background)
        self.two.setFill(self.background)
        self.three.setFill(self.background)
        self.four.setFill(self.background)
        self.five.setFill(self.background)
        self.six.setFill(self.background)
        self.seven.setFill(self.background)
        self.eight.setFill(self.background)
        self.nine.setFill(self.background)

        # turn correct pips on
        if count == "----":
            self.blank.setFill(self.foreground)

        else:
            self.blank.setFill(self.background)
            
            if value == 0:
                self.zero.setFill(self.foreground)

            elif value == 1:
                self.one.setFill(self.foreground)

            elif value == 2:
                self.two.setFill(self.foreground)

            elif value == 3:
                self.three.setFill(self.foreground)

            elif value == 4:
                self.four.setFill(self.foreground)

            elif value == 5:
                self.five.setFill(self.foreground)

            elif value == 6:
                self.six.setFill(self.foreground)

            elif value == 7:
                self.seven.setFill(self.foreground)

            elif value == 8:
                self.eight.setFill(self.foreground)

            elif value == 9:
                self.nine.setFill(self.foreground)

My professor sent my class two images of what should be displayed. The first image is what should be displayed when you run the program. The second image is what should be displayed after clicking increase and decrease.

http://img249.imageshack.us/i/progasgn6apdfadobereade.png/
Image 1

http://img405.imageshack.us/i/progasgn6pdfadobereader.png/
Image 2


Any suggestions will be appreciated.

I spoke with my professor, and this is what I currently have.

from graphics import GraphWin, Point

from button import Button
from charView import CharView
def main():
    #create the application window
    win = GraphWin("Up-Down Counter")
    win.setCoords(0,0,400,300)
    win.setBackground("cyan")

    #Draw the interface widgets
    thousands = CharView(win, Point(49.75,250), 50)
    hundreds = CharView(win, Point(149.25,250), 50)
    tens = CharView(win, Point(248.75,250), 50)
    ones = CharView(win, Point(348.25,250), 50)

    #create the definitions
    counter = 0
    value = "----"
    
    #create the buttons to increase and decrease each digit
    #Spaced by ones, tens, hundreds, and thousands to make reading easier
    oIB = Button(win, Point(348.25,200), 100, 30, "+1") # oIB stands for onesIncreaseButton
    oIB.activate()
    oDB = Button(win, Point(348.25,150), 100, 30, "-1")    #oDB stands for onesDecreaseButton
    oDB.activate()
    
    tIB = Button(win, Point(248.75,200), 100, 30, "+10")   #tIB stands for tensIncreaseButton
    tIB.activate()
    tDB = Button(win, Point(248.75,150), 100, 30, "-10")   #tDB stands for tensDecreaseButton
    tDB.activate()
    
    hIB = Button(win, Point(149.75,200), 100, 30, "+100")  #hIB stands for hundredsIncreaseButton
    hIB.activate()
    hDB = Button(win, Point(149.75,150), 100, 30, "-100")  #hDB stands for hundredsDecreaseButton
    hDB.activate()
    
    kIB = Button(win, Point(49.75,200), 100, 30, "+1000")    #kIB stands for thousandsIncreaseButton
    kIB.activate()
    kDB = Button(win, Point(49.75,150), 100, 30, "-1000")    #kDB stands for thousandsDecreaseButton
    kDB.activate()

    #create the clear and quit buttons
    clearButton = Button(win, Point(200,100), 200, 50, "Clear")
    quitButton = Button(win, Point(200,50), 200, 50, "Quit")
    quitButton.activate()

    pt = win.getMouse()
    while not quitButton.clicked(pt):
        if oIB.clicked(pt):
            counter += 1
            if counter < 10:
                value = "000" + str(counter)
            ones.setValue(value[3])
        if oDB.clicked(pt):
            counter -= 1
            value = "000" + str(counter)
            ones.setValue(value[3])
        if tIB.clicked(pt):
            counter += 1
            value = "00" + str(counter)
            tens.setValue(value[2])
        if tDB.clicked(pt):
            counter -= 1
            value = "00" + str(counter)
            tens.setValue(value[2])
        if hIB.clicked(pt):
            counter += 1
            value = "0" + str(counter)
            hundreds.setValue(value[1])
        if hDB.clicked(pt):
            counter -= 1
            value = "0" + str(counter)
            hundreds.setValue(value[1])
        if kIB.clicked(pt):
            counter += 1
            value = str(counter)
            thousands.setValue(value[0])
        if kDB.clicked(pt):
            counter -= 1
            value = str(counter)
            thousands.setValue(value[0])
            #clearButton.activate()
            #if clearButton.clicked(pt):
                #counter = 0
                #ones.setValue(0)
                #tens.setValue(0)
                #hundreds.setValue(0)
                #thousands.setValue(0)
            
        pt = win.getMouse()

    win.close() #close the program

    
if __name__ == '__main__':
    main()

charview

#charView.py
#A widget for displaying the digits of the number counter

from graphics import *

class CharView:
    """ charView is a widget that displays a graphical representation of a number counter."""
    def __init__(self, win, center, size):
        """Create a view of a digit, e.g.: d1 = GDie(myWin, Point(40,50), 20) creates a die
        centered at (40,50) having sides of length 20."""

        # first define some standard values
        self.win = win # save this for drawing pips later
        self.background = "white" # color of the background
        self.foreground = "black" # color of the numbers
        self.fontSize = 10
        hsize = size / 2.0  # half the size of the die
        
        # create a square for the face
        cx, cy = center.getX(), center.getY()
        center = Point(cx, cy)
        p1 = Point(cx-hsize, cy-hsize)
        p2 = Point(cx+hsize, cy+hsize)
        rect = Rectangle(p1,p2)
        rect.draw(win)
        rect.setFill(self.background)

        # Create 7 circles for standard pip locations
        self.text = Text(center, "-")
        self.text.setTextColor(self.foreground)
        self.text.setSize(self.fontSize)
        self.text.draw(self.win)
        # Draw an initial value
        self.setValue(0)
        
    def setValue(self, value):
        """ Set this digit to display value."""
        self.text.setText(value)

I am stuck getting the counter to work properly. If anyone could help me before April 7th, which is when the assignment is due, it would be appreciated.

Edited 6 Years Ago by tkpanther: n/a

This article has been dead for over six months. Start a new discussion instead.