Hi Everyone. Consider this part two after solving part one of my output window issie

basically 7zip i needed to output its text to a output window i had created inside my main menu where the button "backup" exists.

The window within the 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)

and when you click the backup button this piece of code executes

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

ok ... this is great cause it works ... but i have niggly issues i would like to solve.

1. The output only appears AFTER 7zip has completed. (i would like to see it happen in real time
2. No scroll bars (i have extensive researched this and found when i try to put the working source code in .. it puts scroll bars on the ENTIRE MAIN MENE (root?) window .. NOT the small output window.
3. the output that is show is showing the TOP of the output and not the bottom .... ii do have code for this embedded in my scroll bar code BUT like above ... the scroll bars are attaching to the wrong window.

If anyone can help me .. the code i have been using to help me is here. this works great cause it has scrolls, shows the bottom of the text instantly ... i'm not sure how it works in a live continous output situation but i guess we'll see.

# searching a long text for a string and scrolling to it
# use ctrl+c to copy, ctrl+x to cut selected text,
# ctrl+v to paste, and ctrl+/ to select all

import tkinter as tk

def display(data):
    """creates a text display area with a vertical scrollbar"""
    scrollbar = tk.Scrollbar(root)
    text1 = tk.Text(root, yscrollcommand=scrollbar.set)
    text1.insert(0.0, data)
    scrollbar.config(command=text1.yview)
    scrollbar.pack(side='right', fill='y')
    text1.pack(side='left', expand=0, fill='both')
    
    # could bring the search string in from a listbox selection
    pattern = "Chapter 3"
    # line.char(lines start at 1, characters at 0)
    start = "1.0"
    # returns eg. "31.0" --> line 31, character 0
    index = text1.search(pattern, start)
    # <strong class="highlight">scroll</strong> text until index line is visible
    # might move to the top line of text field
    text1.see(index)
    
    
root = tk.Tk()

str1 = """\
Chapter 1
.
.
.
.
.
.
.
Chapter 2
.
.
.
.
.
.
.
.
Chapter 3
.
.
.
.
.
.
.
.
The End
"""

display(str1)

root.mainloop()

Thankyou for your time ... and so far .. thansk to everyone as i have gained heaps using these forums to seek out answers, snippet code .. all great stuff.

Recommended Answers

All 3 Replies

Note that module os calls do not allow your Python program
to run in the background, whereas module subprocess calls do.

ok last option i have then is this... and it is beyond my experience here so any help would be great.

When i click my button and that script executes that runs 7Zip, is it possible ot make a small window pop up, say something like "please wait while i compress your files" and say play a small busy animation. The window would need to lock the user from clicking the main window and really force them to wait... once complete, the anim is replaced with a nice "OK" button that closes the mini window and leaves you back at the main menu?

I ask alot hahhahaha but i've tried to be reasonable with my program and it is not playing fair :)

I can't help with GUI stuff as I can't use Tkinter, only wxPython. However, if you decided to use os system calls, you can always use threading to run the os calls in one thread, and use a second to update your GUI.

Or like Bumsfeld said, you could go with the subprocess module. That is probably a better bet though as os system calls are never ideal.

As for your message window thing, in wxPython there is a wx.Dialog derivative called wx.MessageDialog which keeps focus until it is done. I'm sure Tkinter has a similar thing.

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.