0

After this code runs, I would like the results to appear in a message box as separate lines of text, and not as tuples (which is what is happening now). Essentially I would like to see the results in the message box just as they would if I printed the line in pyscripter:

print ext_str+ext,cnt_str,len(sz),max_str,max(sz),min_str,min(sz), avg_str,calc_avg(sz)

gives a result that looks like this in the message box (but the lines can wrap):

file type:.mxd count: 5 max. size: 3155968 min. size: 467968 avg. size: 1383014.0 file type:.3dd count: 1 max. size: 836608 min. size: 836608 avg. size: 836608.0

I would also like to be able to add a vertical scrollbar to the message box, because right now if the contents are too long there is no way to close the dialog. I can't seem to figure out a way to do it with easygui - I know tkinter could be an option but I don't really know it too well. Can these two things be done? Here is my code:

import os, sys, easygui
from collections import defaultdict

a_dir = easygui.enterbox("Type in directory path:",title='Directory Stats')
if a_dir is None or a_dir == '' :
  sys.exit()
else:
  os.chdir(a_dir)
  mydir = os.listdir(os.curdir)

filedict = {}
ext_str = 'file type:'
cnt_str = 'count:'
max_str = 'max. size:'
min_str = 'min. size:'
avg_str = 'avg. size:'
smmry_title = 'Summary of directory contents: '+ a_dir
stats_lst = []

def calc_avg(num_list):
  return round(sum (num_list) / len(num_list))

for file in mydir:
  fileext = os.path.splitext(file)[1]
  filesz = os.path.getsize(file)
  filedict.setdefault(fileext,[]).append(filesz)

for ext, sz in filedict.items():
  stats = ext_str+ext,cnt_str,len(sz),max_str,max(sz),min_str,min(sz), avg_str,calc_avg(sz)
  stats_lst.append(stats)
  stats_str = '\n'.join(map(str, stats_lst))
msg = easygui.msgbox(msg=stats_str,title=smmry_title)
JIST
  • 1,139
  • 2
  • 8
  • 30
kflaw
  • 424
  • 1
  • 10
  • 26

1 Answers1

0

I know that you can accomplish that by using a textbox or codebox. You should be able to manipulate the output to work for you.

http://easygui.sourceforge.net/tutorial/easygui_pydoc.html#-textbox

Edit:

This is creating what I believe you are looking for. Add stats_print = [] with your other variables around line 20 and you should be good to go. I also cleaned up your stats variable to give you a cleaner output so that each would print on the same line.

for ext, sz in filedict.items():
  stats = (
    ext_str+ext, cnt_str+str(len(sz)), max_str+str(max(sz)),
    min_str+str(min(sz)), avg_str+str(calc_avg(sz))
    )
  stats_lst.append(stats)
for item in stats_lst:
  stats_str = '\n'.join(map(str, item))
  stats_print.append('\n')
  stats_print.append(stats_str)

msg = easygui.codebox(msg="",title=smmry_title, text=stats_print)
Benjooster
  • 544
  • 2
  • 6
  • 20
  • what is the difference between msg and text? seems like text should equal stats_str – kflaw Sep 23 '13 at 21:24
  • i see now.. but still the text shows up in a tuple, like ('file type: mxd', 'max sizeL 360L', etc..) – kflaw Sep 23 '13 at 21:48
  • You're creating a new line in your textbox with the list stats_lst. You've appended that list with the tuple. You're going to have to run another for list in there to append the items line by line to the list. Experiment with writing a `\n` `for item in stats_lst` to your stats_str and display that in the textbox. – Benjooster Sep 23 '13 at 23:18
  • I've edited in an update that seems to do what you're looking for. Hope this helps. – Benjooster Sep 24 '13 at 13:56
  • thanks! Works great. though it doesn't seem like changing the stats variable did anything different, i tested with my old and the result was the same. I also appended another \n to stats_prnt for easier reading of the results – kflaw Sep 24 '13 at 15:01
  • I changed it because when I ran it to make sure it worked it was printing the result on the line below the heading. It just gave it a cleaner view in easygui for me, but doesn't matter at all haha. – Benjooster Sep 24 '13 at 15:10