Hi I think this topic is beyond my lame python abbilities.
I hope the program can run, it is writen by using error messages until it worked.
I am using python 33.
I am trying to learn info on adding leading zeros to the output.(File & Windows)
I am not sure if the variable that produces output is a list or int sometimes I think it is both :)possibly the varible (g)
either way, I am lost and have tried different things all day.
So if you have any Idea THANKS!

Output of File:
No leading zeros

4 5 33 48 57 - 41
2 27 38 54 58 - 4
8 24 30 53 57 - 46
8 17 25 42 55 - 15
15 22 34 36 47 - 7

Desired output of File:
Leading zeros

04 05 33 48 57 - 41
02 27 38 54 58 - 04
08 24 30 53 57 - 46
08 17 25 42 55 - 15
15 22 34 36 47 - 07

from tkinter import*
import sys, random, tkinter 
ticket_price = 2
i=0
total = 0



def calculatetotals():
    valid = set('12345')
    ticket_entry = tik.get()
    if ticket_entry not in valid:
        print('We permit values in {}.'.format(valid))
        label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
    else:
        label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
        mytik=int(ticket_entry)
        total = mytik*ticket_price              
        Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=1)      
        Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)        
        Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white'\
               ,command = nextx).grid(row=8,column=1)        
aApp=Tk()                                                                                          
aApp.geometry('580x170+200+270')
aApp.title("LOTTO")
tik=StringVar()
label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
label1.grid(row=0,column=2)
label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
label2.grid(row=1,column=1)
label3=Label(aApp,text="How Many Would You Like?",fg='black')
label3.grid(row=2,column=1)                                                                                                   
mytik = Entry(aApp,textvariable=tik)
mytik.grid(row=2,column=2)                                                                            
button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue'\
               ,command=calculatetotals)
button1.grid(row=6,column=1)

def nextx():

             Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black'\
                    ,command = nextz).grid(row=8,column=1)
             ticket_entry = tik.get()
             #######################################
             # outputs to Window and File("C:\output.txt","a")
             #######################################
             i = 0           
             while i < int(ticket_entry):                   
               L = list(range(1,60))
               random.shuffle(L)                                            
               g = L[:5]
               g.sort()
               f = L[5:6]                                               
               drawing = '  '.join( ['  '.join(str(G)  for  G  in g),' -',str(f[0])])
               label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)
               text_file = open("C:\output.txt","a")
               text_file.write('\n')
               text_file.write(drawing)
               text_file.close()                     
               i+=1
              #########################################
              # Finnished output to window and File
               #########################################
app=tkinter.Tk()
app.geometry('600x400+75+75')
app.title(string="     NUMBERS      ")    

Button(app, text="Quit", command=app.quit).pack()
def  nextz():    
                              Button(aApp,text='Push   again to  <Quit> : To see if you match Push <Compare> In The New Winning Number Window, '\
                                     ,fg='yellow',bg='black',command = quit).grid(row=8,column=1)                                   
                              L = list(range(1,60))
                              random.shuffle(L)
                              g = L[:5]
                              g.sort()
                              f = L[5:6]               
                              drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])

                              aBpp=tkinter.Tk()
                              aBpp.geometry('200x100+705+705')
                              aBpp.title(string="Winning Number")
                              label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)                             
                              Button(aBpp, text="Compare", command=aBpp.quit).pack()                             
                              return

tkinter.mainloop()
app.mainloop()
aApp.mainloop()

Recommended Answers

Why are your indentations so odd?

Jump to Post

Here is the code, reindented with tim peter's reindent (not sure it would work without python 2)

from tkinter import*
import sys, random, tkinter
ticket_price = 2
i=0
total = 0
def …
Jump to Post

At line 49 (in my version), replace str() with zstr() and define

def zstr(ob, width = 2):
    return '{x:0>{width}s}'.format(x = str(ob), width = width)
Jump to Post

As I said above, use

        drawing = '  '.join( ['  '.join(zstr(G)  for  G  in g),' -',zstr(f[0])])

and the above function zstr().

Jump to Post

All 13 Replies

Why are your indentations so odd?

Here is the code, reindented with tim peter's reindent (not sure it would work without python 2)

from tkinter import*
import sys, random, tkinter
ticket_price = 2
i=0
total = 0
def calculatetotals():
    valid = set('12345')
    ticket_entry = tik.get()
    if ticket_entry not in valid:
        print('We permit values in {}.'.format(valid))
        label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
    else:
        label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
        mytik=int(ticket_entry)
        total = mytik*ticket_price
        Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=1)
        Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)
        Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white'\
               ,command = nextx).grid(row=8,column=1)
aApp=Tk()
aApp.geometry('580x170+200+270')
aApp.title("LOTTO")
tik=StringVar()
label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
label1.grid(row=0,column=2)
label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
label2.grid(row=1,column=1)
label3=Label(aApp,text="How Many Would You Like?",fg='black')
label3.grid(row=2,column=1)
mytik = Entry(aApp,textvariable=tik)
mytik.grid(row=2,column=2)
button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue'\
               ,command=calculatetotals)
button1.grid(row=6,column=1)
def nextx():
    Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black'\
           ,command = nextz).grid(row=8,column=1)
    ticket_entry = tik.get()
    #######################################
    # outputs to Window and File("C:\output.txt","a")
    #######################################
    i = 0
    while i < int(ticket_entry):
        L = list(range(1,60))
        random.shuffle(L)
        g = L[:5]
        g.sort()
        f = L[5:6]
        drawing = '  '.join( ['  '.join(str(G)  for  G  in g),' -',str(f[0])])
        label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)
        text_file = open("C:\output.txt","a")
        text_file.write('\n')
        text_file.write(drawing)
        text_file.close()
        i+=1
       #########################################
       # Finnished output to window and File
        #########################################
app=tkinter.Tk()
app.geometry('600x400+75+75')
app.title(string="     NUMBERS      ")
Button(app, text="Quit", command=app.quit).pack()
def  nextz():
    Button(aApp,text='Push   again to  <Quit> : To see if you match Push <Compare> In The New Winning Number Window, '\
           ,fg='yellow',bg='black',command = quit).grid(row=8,column=1)
    L = list(range(1,60))
    random.shuffle(L)
    g = L[:5]
    g.sort()
    f = L[5:6]
    drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])
    aBpp=tkinter.Tk()
    aBpp.geometry('200x100+705+705')
    aBpp.title(string="Winning Number")
    label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)
    Button(aBpp, text="Compare", command=aBpp.quit).pack()
    return
tkinter.mainloop()
app.mainloop()
aApp.mainloop()

Here is a way to produce a string with leading zeros:

>>> "{:0>1}".format(5)
'5'
>>> "{:0>2}".format(5)
'05'
>>> "{:0>3}".format(5)
'005'

Use 4 space characters and no tab to indent python code.

OK I understand "{:0>2}".format(5)
'05' but i can't find a place in the program to put it.
or I am not sure it will work with a list.

At line 49 (in my version), replace str() with zstr() and define

def zstr(ob, width = 2):
    return '{x:0>{width}s}'.format(x = str(ob), width = width)

OK I understand "{:0>2}".format(5)
'05' but i can't find a place in the program to put it.
or I am not sure it will work with a list.
since drawing = 4 26 31 38 39 and f= 5
and g = [4, 26, 31, 38, 39] and f=[5] in this example.
You think I need some string formatting functions?

format(5)

As I said above, use

        drawing = '  '.join( ['  '.join(zstr(G)  for  G  in g),' -',zstr(f[0])])

and the above function zstr().

nevermind i don't understand anything at all
like this

drawing = ' '.join( [' '.join(zstr(G) for G in g),' -',str(f[0])])
label5=tkinter.Label(app,text = drawing ).pack(padx=1,pady=2)
text_file = open("C:\output.txt","a")
text_file.write('\n')
text_file.write(drawing)
text_file.close()
i+=1

def zstr(ob, width = 2):
 drawing = ' '.join( [' '.join(zstr(G) for G in g),' -',zstr(f[0])])
label5=tkinter.Label(app,text = drawing ).pack(padx=1,pady=2)
text_file = open("C:\output.txt","a")
text_file.write('\n')
text_file.write(drawing)
text_file.close()
i+=1

Iam tatal lost in this

zstr does not change to violet color is this a python33 thing?

Here is the complete code

from tkinter import*
import sys, random, tkinter
ticket_price = 2
i=0
total = 0
def calculatetotals():
    valid = set('12345')
    ticket_entry = tik.get()
    if ticket_entry not in valid:
        print('We permit values in {}.'.format(valid))
        label4 = Label(aApp,text = 'We permit values in 1, 2, 3, 4 or 5!',fg = 'blue').grid(row=7,column=1)
    else:
        label4 = Label(aApp,text = '                                                                        ',fg = 'blue').grid(row=7,column=1)
        mytik=int(ticket_entry)
        total = mytik*ticket_price
        Label(aApp,text= "You purchased:$%.2f \n" %  total).grid(row=7,column=1)
        Label(aApp,text= "\nNumber Of Tckets: %.f\n" % mytik).grid(row=6,column=1)
        Button(aApp,text="Click Here To Draw Your Tickets!",fg='blue',bg='white'\
               ,command = nextx).grid(row=8,column=1)
aApp=Tk()
aApp.geometry('580x170+200+270')
aApp.title("LOTTO")
tik=StringVar()
label1 = Label(aApp,text = "Welcome To Lotto.",fg = 'blue')
label1.grid(row=0,column=2)
label2=Label(aApp,text="Tickets Are ${:.2f} Each.".format(ticket_price),fg='red')
label2.grid(row=1,column=1)
label3=Label(aApp,text="How Many Would You Like?",fg='black')
label3.grid(row=2,column=1)
mytik = Entry(aApp,textvariable=tik)
mytik.grid(row=2,column=2)
button1=Button(aApp,text="Your Purchse\nClick Here",fg='blue'\
               ,command=calculatetotals)
button1.grid(row=6,column=1)
def nextx():
    Button(aApp,text="Click Here to see the new winning number.",fg='lightgreen',bg='black'\
           ,command = nextz).grid(row=8,column=1)
    ticket_entry = tik.get()
    #######################################
    # outputs to Window and File("C:\output.txt","a")
    #######################################
    i = 0
    while i < int(ticket_entry):
        L = list(range(1,60))
        random.shuffle(L)
        g = L[:5]
        g.sort()
        f = L[5:6]
        drawing = '  '.join( ['  '.join(zstr(G)  for  G  in g),' -',zstr(f[0])])
        label5=tkinter.Label(app,text = drawing  ).pack(padx=1,pady=2)
        text_file = open("C:\output.txt","a")
        text_file.write('\n')
        text_file.write(drawing)
        text_file.close()
        i+=1
       #########################################
       # Finnished output to window and File
        #########################################
app=tkinter.Tk()
app.geometry('600x400+75+75')
app.title(string="     NUMBERS      ")
Button(app, text="Quit", command=app.quit).pack()
def  nextz():
    Button(aApp,text='Push   again to  <Quit> : To see if you match Push <Compare> In The New Winning Number Window, '\
           ,fg='yellow',bg='black',command = quit).grid(row=8,column=1)
    L = list(range(1,60))
    random.shuffle(L)
    g = L[:5]
    g.sort()
    f = L[5:6]
    drawing = '  '.join( [' - '.join(str(G)  for  G  in g),'  -  ',str(f[0])])
    aBpp=tkinter.Tk()
    aBpp.geometry('200x100+705+705')
    aBpp.title(string="Winning Number")
    label6=tkinter.Label(aBpp,text = drawing).pack(padx=1,pady=2)
    Button(aBpp, text="Compare", command=aBpp.quit).pack()
    return
def zstr(ob, width = 2):
    return '{x:0>{width}s}'.format(x = str(ob), width = width)
tkinter.mainloop()
app.mainloop()
aApp.mainloop()

Is this what you mean at number 49.?

 drawing = '  '.join( ['  '.join(zstr(G)  for  G  in g),' -',zstr(f[0])])

I'm not sure where this goes also:
Does it go inside the while loop at 43.
and does anything go between def & return?

def zstr(ob, width = 2):
#
#
return '{x:0>{width}s}'.format(x = str(ob), width = width) 

Ok I will try it your way
thanks so much i hope this works

I ran the code and all it does is go here How are you running this?

>>> ================= RESTART =================
>>> 
>>> 

ok nevermind the above it worked on another copy with your changes
Thank so much!

Be a part of the DaniWeb community

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