pythonformattingscrollbareasygui

formatting content in easygui message box and adding scrollbar


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)

Solution

  • 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)