Hello, Im trying to get this program to display a receipt of the items bought with its price and total, but so far this is what I've acomplished, I've been trying for a long time now, to make it print the receipt in the win2 but I haven't been able.

helpp pls

from graphics import *
def main():
    win = GraphWin("WalMart Receipt", 600, 900)

    #Title
    center = Point(300,20)
    label = Text(center, "WalMart Receipt Maker")
    label.draw(win)
    label.setSize(20)

    #Item and Price input 1
    center2 = Point(50,50)
    label2 = Text(center2, "Item >>")
    label2.draw(win)
    input2 = Entry(Point(170,55), 20)
    input2.setText("item")
    input2.draw(win)
    centera = Point(350,50)
    labela = Text(centera, "Price >>")
    labela.draw(win)
    inputa = Entry(Point(420,55), 6)
    inputa.setText("0.00")
    inputa.draw(win)
    
    #Item and Price input 2
    center3 = Point(50,100)
    label3 = Text(center3, "Item >>")
    label3.draw(win)
    input3 = Entry(Point(170,105), 20)
    input3.setText("item")
    input3.draw(win)
    centerb = Point(350,100)
    labelb = Text(centerb, "Price >>")
    labelb.draw(win)
    inputb = Entry(Point(420,105), 6)
    inputb.setText("0.00")
    inputb.draw(win)
    

    #Item and Price input 3
    center4 = Point(50,150)
    label4 = Text(center4, "Item >>")
    label4.draw(win)
    input4 = Entry(Point(170,155), 20)
    input4.setText("item")
    input4.draw(win)
    centerc = Point(350,150)
    labelc = Text(centerc, "Price >>")
    labelc.draw(win)
    inputc = Entry(Point(420,155), 6)
    inputc.setText("0.00")
    inputc.draw(win)
    

    #Item and Price input 4
    center5 = Point(50,200)
    label5 = Text(center5, "Item >>")
    label5.draw(win)
    input5 = Entry(Point(170,205), 20)
    input5.setText("item")
    input5.draw(win)
    centerd = Point(350,200)
    labeld = Text(centerd, "Price >>")
    labeld.draw(win)
    inputd = Entry(Point(420,205), 6)
    inputd.setText("0.00")
    inputd.draw(win)
    

    #Item and Price input 5
    center6 = Point(50,250)
    label6 = Text(center6, "Item >>")
    label6.draw(win)
    input6 = Entry(Point(170,255), 20)
    input6.setText("item")
    input6.draw(win)
    centere = Point(350,250)
    labele = Text(centere, "Price >>")
    labele.draw(win)
    inpute = Entry(Point(420,255), 6)
    inpute.setText("0.00")
    inpute.draw(win)
    

    #Item and Price input 6
    center7 = Point(50,300)
    label7 = Text(center7, "Item >>")
    label7.draw(win)
    input7 = Entry(Point(170,305), 20)
    input7.setText("item")
    input7.draw(win)
    centerf = Point(350,300)
    labelf = Text(centerf, "Price >>")
    labelf.draw(win)
    inputf = Entry(Point(420,305), 6)
    inputf.setText("0.00")
    inputf.draw(win)
    

    #Item and Price input 7
    center8 = Point(50,350)
    label8 = Text(center8, "Item >>")
    label8.draw(win)
    input8 = Entry(Point(170,355), 20)
    input8.setText("item")
    input8.draw(win)
    centerg = Point(350,350)
    labelg = Text(centerg, "Price >>")
    labelg.draw(win)
    inputg = Entry(Point(420,355), 6)
    inputg.setText("0.00")
    inputg.draw(win)
    

    #Item and Price input 8
    center9 = Point(50,400)
    label9 = Text(center9, "Item >>")
    label9.draw(win)
    input9 = Entry(Point(170,405), 20)
    input9.setText("item")
    input9.draw(win)
    centerh = Point(350,400)
    labelh = Text(centerh, "Price >>")
    labelh.draw(win)
    inputh = Entry(Point(420,405), 6)
    inputh.setText("0.00")
    inputh.draw(win)
    

    #Item and Price input 9
    center10 = Point(50,450)
    label10 = Text(center10, "Item >>")
    label10.draw(win)
    input10 = Entry(Point(170,455), 20)
    input10.setText("item")
    input10.draw(win)
    centeri = Point(350,450)
    labeli = Text(centeri, "Price >>")
    labeli.draw(win)
    inputi = Entry(Point(420,455), 6)
    inputi.setText("0.00")
    inputi.draw(win)


    #Item and Price input 10
    center11 = Point(50,500)
    label11 = Text(center11, "Item >>")
    label11.draw(win)
    input11 = Entry(Point(170,505), 20)
    input11.setText("item")
    input11.draw(win)
    centerj = Point(350,500)
    labelj = Text(centerj, "Price >>")
    labelj.draw(win)
    inputj = Entry(Point(420,505), 6)
    inputj.setText("0.00")
    inputj.draw(win)
    

    #Item and Price input 11
    center12 = Point(50,550)
    label12 = Text(center12, "Item >>")
    label12.draw(win)
    input12 = Entry(Point(170,555), 20)
    input12.setText("item")
    input12.draw(win)
    centerk = Point(350,550)
    labelk = Text(centerk, "Price >>")
    labelk.draw(win)
    inputk = Entry(Point(420,555), 6)
    inputk.setText("0.00")
    inputk.draw(win)


    #Item and Price input 12
    center13 = Point(50,600)
    label13 = Text(center13, "Item >>")
    label13.draw(win)
    input13 = Entry(Point(170,605), 20)
    input13.setText("item")
    input13.draw(win)
    centerl = Point(350,600)
    labell = Text(centerl, "Price >>")
    labell.draw(win)
    inputl = Entry(Point(420,605), 6)
    inputl.setText("0.00")
    inputl.draw(win)


    #Item and Price input 13
    center14 = Point(50,650)
    label14 = Text(center14, "Item >>")
    label14.draw(win)
    input14 = Entry(Point(170,655), 20)
    input14.setText("item")
    input14.draw(win)
    centerm = Point(350,650)
    labelm = Text(centerm, "Price >>")
    labelm.draw(win)
    inputm = Entry(Point(420,655), 6)
    inputm.setText("0.00")
    inputm.draw(win)


    #Item and Price input 14
    center15 = Point(50,700)
    label15 = Text(center15, "Item >>")
    label15.draw(win)
    input15 = Entry(Point(170,705), 20)
    input15.setText("item")
    input15.draw(win)
    centern = Point(350,700)
    labeln = Text(centern, "Price >>")
    labeln.draw(win)
    inputn = Entry(Point(420,705), 6)
    inputn.setText("0.00")
    inputn.draw(win)


    #Item and Price input 15
    center16 = Point(50,750)
    label16 = Text(center16, "Item >>")
    label16.draw(win)
    input16 = Entry(Point(170,755), 20)
    input16.setText("item")
    input16.draw(win)
    centero = Point(350,750)
    labelo = Text(centero, "Price >>")
    labelo.draw(win)
    inputo = Entry(Point(420,755), 6)
    inputo.setText("0.00")
    inputo.draw(win)


    #print receipt button
    rect5 = Rectangle(Point(240,800), Point(360,850))
    rect5.draw(win)
    center5 = Point(300,825)
    button = Text(center5, "Print Receipt")
    button.draw(win)
    button.setSize(10)
    button.setFill('Orange')

    win.getMouse()
    
    win2 = GraphWin("WalMart Receipt", 600, 900)

    #Title 2
    center = Point(300,20)
    label22 = Text(center, "WalMart Receipt")
    label22.draw(win2)
    label22.setSize(20)

    x = eval(inputa.getText())
    output.setText(x)

    output = Text(Point(170,55), "")
    output.draw(win2)
    
    
    
main()

At least here is little refactoring to loop and function and usefull sum to terminal window:

from graphics import *
def makeinput(win,i):
    #Item and Price input maker
    center2 = Point(50,50*i+50)
    label2 = Text(center2, "Item >>")
    label2.draw(win)
    input2 = Entry(Point(170,55+i*50), 20)
    input2.setText("item")
    input2.draw(win)
    centera = Point(350,50)
    labela = Text(centera, "Price >>")
    labela.draw(win)
    inputa = Entry(Point(420,55+i*50), 6)
    inputa.setText("0.00")
    inputa.draw(win)
    return input2,inputa

def main():
    win = GraphWin("WalMart Receipt", 600, 700)

    #Title
    center = Point(300,20)
    label = Text(center, "WalMart Receipt Maker")
    label.draw(win)
    label.setSize(20)

    inputlist=[]

    for i in range(10):
        inputlist.append(makeinput(win,i))       

    #print receipt button
    rect5 = Rectangle(Point(240,600), Point(360,650))
    rect5.draw(win)
    center5 = Point(300,625)
    button = Text(center5, "Print Receipt")
    button.draw(win)
    button.setSize(12)
    button.setFill('Orange')

    win.getMouse()
    shoppings=[(thing.getText(),price.getText()) for thing, price in inputlist]
    print '\n'.join( "%s price: %s" %(a,b) for a,b in shoppings if b != '0.00' )
    print 'Total\t     %.2f' % sum([float(price) for _,price in shoppings])
    
main()

Another version

from graphics import *

def item_n_price(win, i, items, prices):
    z = 50 * i
    center = Point(50,50+z)
    label = Text(center, "Item >>")
    label.draw(win)
    input = Entry(Point(170,55+z), 20)
    input.setText("item")
    input.draw(win)
    items.append(input)
    center = Point(350,50+z)
    label = Text(center, "Price >>")
    label.draw(win)
    input = Entry(Point(420,55+z), 6)
    input.setText("0.00")
    input.draw(win)
    prices.append(input)
    
def main():
    win = GraphWin("WalMart Receipt", 600, 900)

    #Title
    center = Point(300,20)
    label = Text(center, "WalMart Receipt Maker")
    label.draw(win)
    label.setSize(20)
    items = []
    prices = []
    
    for i in range(15):
        item_n_price(win, i, items, prices)


    #print receipt button
    rect5 = Rectangle(Point(240,800), Point(360,850))
    rect5.draw(win)
    center5 = Point(300,825)
    button = Text(center5, "Print Receipt")
    button.draw(win)
    button.setSize(10)
    button.setFill('Orange')

    win.getMouse()
    
    win2 = GraphWin("WalMart Receipt", 600, 900)

    #Title 2
    center = Point(300,20)
    label22 = Text(center, "WalMart Receipt")
    label22.draw(win2)
    label22.setSize(20)
    
    output = Text(Point(170,55), "")
    x = eval(prices[1].getText())
    output.setText(x)
    output.draw(win2)
    
    win2.getMouse()
    
    
    
main()

ohh, thank you, now its working good :) , but theres a few things I dont understand why they are there, like: "center2 = Point(50,50*i+50)" why do u multiply *i ? ; "return input2,inputa" why do u use return in there? ; and this whole part I dont get it

"shoppings=[(thing.getText(),price.getText()) for thing, price in inputlist]
print '\n'.join( "%s price: %s" %(a,b) for a,b in shoppings if b != '0.00' )
print 'Total\t %.2f' % sum([float(price) for _,price in shoppings])"

At least here is little refactoring to loop and function and usefull sum to terminal window:

from graphics import *
def makeinput(win,i):
    #Item and Price input maker
    center2 = Point(50,50*i+50)
    label2 = Text(center2, "Item >>")
    label2.draw(win)
    input2 = Entry(Point(170,55+i*50), 20)
    input2.setText("item")
    input2.draw(win)
    centera = Point(350,50)
    labela = Text(centera, "Price >>")
    labela.draw(win)
    inputa = Entry(Point(420,55+i*50), 6)
    inputa.setText("0.00")
    inputa.draw(win)
    return input2,inputa

def main():
    win = GraphWin("WalMart Receipt", 600, 700)

    #Title
    center = Point(300,20)
    label = Text(center, "WalMart Receipt Maker")
    label.draw(win)
    label.setSize(20)

    inputlist=[]

    for i in range(10):
        inputlist.append(makeinput(win,i))       

    #print receipt button
    rect5 = Rectangle(Point(240,600), Point(360,650))
    rect5.draw(win)
    center5 = Point(300,625)
    button = Text(center5, "Print Receipt")
    button.draw(win)
    button.setSize(12)
    button.setFill('Orange')

    win.getMouse()
    shoppings=[(thing.getText(),price.getText()) for thing, price in inputlist]
    print '\n'.join( "%s price: %s" %(a,b) for a,b in shoppings if b != '0.00' )
    print 'Total\t     %.2f' % sum([float(price) for _,price in shoppings])
    
main()

thanks you helped me alot , but why do u use "return input2,inputa" in there?

  1. Because those are inputs we need to keep in list to use for getting the articles and prices of all bought items. Otherwise Python's garbage collection would reuse the objects and we had no way to access the inputs as we had no way to access them. We have not individual names for each item, but one list to store them all.
  2. input is not good variable name as it is python function so I kept those names from your original code.
  3. The position of your entries had spacing of 50 so 50*number of item.
## get list of entered values in the inputs from the inputlist (recorded from input2 and inputa returned from construction function
shoppings=[(thing.getText(),price.getText()) for thing, price in inputlist]

## print all lines whose amount is not original zero (value is string not float)
print '\n'.join( "%s price: %s" %(a,b) for a,b in shoppings if b != '0.00' )
## pick up prices from shoppings and make them float number, sum them up
print 'Total\t %.2f' % sum([float(price) for _,price in shoppings])

Oh ok, thank you men , now everything is clear

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.