Hello Everyone
Thanks for the message of welcome in a previous post. I have many questions but i have sleected this one as it represents my favourite piece of not working code (hahaha) but it has been studied and altered so i got this bugger working .. mostly .

Explain: I backup using 7zip, this works, and it works well, instead of the regular grab a dir and back it up, this does somethign more like .... seek out all the image types in the user folder of a windows machine then compress them up and store them.

So i have a main menu done with tkinter and i press the button, this button leads to this sexy piece of code

def picbackup():
    source = ['-ir!"%USERPROFILE%\\*.jpg"', '-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
    target_dir = '.\\'                                                  # Remember to change this to what you will be using
    today = time.strftime('%d_%m_%Y')             # The current day is the name of the subdirectory in the main directory
    target = '.\\Backup\Pictures_' + today + '.zip'
    zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
#

#
    if os.system(zip_command) == 0: 
        print('Successful backup to', target)
    else:
        print('Backup FAILED')

ok .. this is the issue, while this is not yet compiled, i can see the output of this in my intepreter window so i can see 7zip doing it's job, but my original intention was for a window to pop up and display 7zips output in there, once complete, you could press OK and be back at the main menu. This was proving difficult to research so i switched to a simplier idea using some code i borrowed from a fella who made a ping GUI - and i shuffled my main menu to include a small live action output window so once you click the button, the backup will take place, but inside the output window the action should take place
The output window code looks like this

outputLabel = Label(root,text="7Zip Output Window")
outputLabel.place(x=500,y=425)

outputWindow = Text(root)
outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
outputWindow.place(x=350,y=130)

Problem is ... simply .... it doesn't work, i have tried to put certain combinations into the 7zip section like
shellOutput = zip_command.read()
zip_command.close()
outputWindow.delete('1.0',END)
outputWindow.insert('1.0',shellOutput)

that was between the # # and then 7zip doesn't do the backup no more.

Would love to hear some anyone about either opening a window to display the output with a OK when done or using my now created 7zip output window

Thanks for reading this .. it was alot :)

I don't use Tkinter as I personally hate it :P But I'm going to assume that the Text control has a write function of some kind, just like the wxPython TextCtrls. If that's the case, then you redirect the standard output stream so that it uses your Text control as the object it writes to:

import sys

# your code
outputWindow = Text(root)
outputWindow.config(relief=SUNKEN, bg='beige', width=51, height=17)
outputWindow.place(x=350, y=130)

# redirect stdout
sys.stdout = outputWindow

# and if you want to reset the stdout
# sys.stdout = sys.__stdout__

This is the sort of method I always use for wxPython GUIs. Note that this is a flexible thing though - files are objects with a write command, so they can be used to redirect output too, like if you wanted all the output logged to a file:

import sys

fh = open('log.file', 'w')
sys.stdout = fh
# program here...
fh.close()
sys.stdout = sys.__stdout__

Hope that helps :)

Thanks for the reply.

I understood what you was doing, and i serted this code into my program as such. Also .. it looks like your creating the output window here and now as it was already created in my main menu here, so i don't think it needs creating again? just redirected?

#create the label for output window
outputLabel = Label(root,text="7Zip Output Window")
outputLabel.place(x=500,y=425)

#create the output window
outputWindow = Text(root)
outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
outputWindow.place(x=350,y=130)

How my 7zip code looks now. I also commented out the window creation lines and just left the sys.stdout = outputWindow in there but it still doesn't go to the output window

def picbackup():
    source = ['-ir!"%USERPROFILE%\\*.jpg"', '-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
    target_dir = '.\\'
    today = time.strftime('%d_%m_%Y')
    target = '.\\Backup\Pictures_' + today + '.zip'
    zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
    outputWindow = Text(root)
    outputWindow.config(relief=SUNKEN, bg='beige', width=51, height=17)
    outputWindow.place(x=350, y=130)
    sys.stdout = outputWindow
    if os.system(zip_command) == 0:                                     # Run the backup
        # can a progress bar be implemented or EBC anim ?????
        print('Successful backup to', target)
    else:
        print('Backup FAILED')

Your second idea of outputting to a file wasn't what i wanted but i'll nick that code for another time ehhehe

Anyways, this output still happened in the interpreter window .... i'll show the original code i was trying to duplicate the results of so you can see a bit better ... though i don't know if im a windows user or not .. this code will work great one day .. and then not at all the next with no alterrations .. i use it as reference only.

# Objective: To create a GUI for the ping utility

import os
from tkinter import *

root = Tk()
root.title('Ping GUI')

    # the ping and clearScreen methods
def ping():
    ip = ipField.get('1.0',END)
    f = os.popen('C:\\Windows\\System32\\PING.EXE '+ip)
    shellOutput = f.read()
    f.close()
    outputWindow.delete('1.0',END)
    outputWindow.insert('1.0',shellOutput)
    
def clearScreen():
    outputWindow.delete('1.0',END)

    #create label for IP Address text field
ipLabel = Label(root,text="Enter IP Address/DNS:")
ipLabel.pack(side=LEFT)

    # create the ip address text field
ipField = Text(root)
ipField.pack(side=LEFT)
ipField.config(relief=SUNKEN, bg='beige',width=20,height=1)

    # create the ping button
b = Button(root,text="Ping",command=ping)
b.pack(side=LEFT)

    #create test button
tb = Button(root,text="Clear Screen",command=clearScreen)
tb.pack(side=LEFT)

    #create the label for output window
outputLabel = Label(root,text="Output Window")
outputLabel.pack(side=BOTTOM)

    #create the output window
outputWindow = Text(root)
outputWindow.config(relief=SUNKEN, bg='beige',width=50,height=20)
outputWindow.pack(side=BOTTOM)

root.mainloop()

I need more encouragement :) i mean help!

I need to get better at writing this stuff, reading back my own threads is confusing :(

ok .. better worded (i hope)

I have a selection of buttons for 7zip backups on a main window - that main window ALSO has a nice canvas window .. code looks like this

outputLabel = Label(root,text="7Zip Output Window")
outputLabel.place(x=500,y=425)
outputWindow = Text(root)
outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
outputWindow.place(x=350,y=130)

You press the button for the 7zip backup and you get this code

def picbackup():
    source = ['-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
    target_dir = '.\\'
    today = time.strftime('%d_%m_%Y')
    target = '.\\Backup\Pictures_' + today + '.zip'
    zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
    if os.system(zip_command) == 0: 
#        sys.stdout = outputWindow
        print('Successful backup to', target)
    else:
        print('Backup FAILED')

In terms of function, this works GREAT! does what it is meant to do ... but the 7zip output i can see "compressing file ... etc" is happening in the intpreter window and not the window (canvas?) created and sitting empty on my main menu.

You see see a command i tried but it is commented out right now as it didn't help.

Hopefully worded better and easier to understand.

Well I can see that the line you have redirecting the standard output (stdout) is commented out. So it won't be changing the output to that window you wanted. Or is there something I'm missing...? :P

Yeah

> You see see a command i tried but it is commented out right now as it didn't help.

i did try it .. didn't work, so i comment it out .. it's the way i code ... if somethign doesn't work, i undo what i did and continue to solve the issue .... otherwise if i solve it another way .... i might have other useless code roaming around.

Oh sorry! I didn't see your previous post metioning that :D Does anyone know if this has to do with Python 3.x? Changing the value of sys.sydout always worked for me...

Is this in the right place as well?

is there other code needed for this to work? Just checking ... first time seeing this code ... but also .. i worked in the ping code for me, so i know it works ... unless it is a 7zip issue?

Solved and marking as such .. has new issues but will start a new thread.

For anyoine interested in making 7zip code work and in an output window .. here is the code.

def picbackup():
    source = ['-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
    target_dir = '.\\'
    today = time.strftime('%d_%m_%Y')
    target = '.\\Backup\Pictures_' + today + '.zip'
    zip = os.popen ("D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source)))
    shellOutput = zip.read()
    zip.close()
    outputWindow.delete('1.0',END)
    outputWindow.insert('1.0',shellOutput)
    if os.system(zip_command) == 0:
        # can a progress bar be implemented or EBC anim ?????
        print('Successful backup to', target)
    else:
        print('Backup FAILED')
This question has already been answered. Start a new discussion instead.