i want output in a window

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

i want output in a window

 
0
  #1
Jul 24th, 2009
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

  1. def picbackup():
  2. source = ['-ir!"%USERPROFILE%\\*.jpg"', '-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
  3. target_dir = '.\\' # Remember to change this to what you will be using
  4. today = time.strftime('%d_%m_%Y') # The current day is the name of the subdirectory in the main directory
  5. target = '.\\Backup\Pictures_' + today + '.zip'
  6. zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
  7. #
  8.  
  9. #
  10. if os.system(zip_command) == 0:
  11. print('Successful backup to', target)
  12. else:
  13. 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

  1. outputLabel = Label(root,text="7Zip Output Window")
  2. outputLabel.place(x=500,y=425)
  3.  
  4. outputWindow = Text(root)
  5. outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
  6. 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
Last edited by Aiban; Jul 24th, 2009 at 10:11 am.
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: i want output in a window

 
0
  #2
Jul 24th, 2009
I don't use Tkinter as I personally hate it 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:
  1. import sys
  2.  
  3. # your code
  4. outputWindow = Text(root)
  5. outputWindow.config(relief=SUNKEN, bg='beige', width=51, height=17)
  6. outputWindow.place(x=350, y=130)
  7.  
  8. # redirect stdout
  9. sys.stdout = outputWindow
  10.  
  11. # and if you want to reset the stdout
  12. # 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:
  1. import sys
  2.  
  3. fh = open('log.file', 'w')
  4. sys.stdout = fh
  5. # program here...
  6. fh.close()
  7. sys.stdout = sys.__stdout__
Hope that helps
Last edited by shadwickman; Jul 24th, 2009 at 3:20 pm.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

Re: i want output in a window

 
0
  #3
Jul 25th, 2009
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?

  1. #create the label for output window
  2. outputLabel = Label(root,text="7Zip Output Window")
  3. outputLabel.place(x=500,y=425)
  4.  
  5. #create the output window
  6. outputWindow = Text(root)
  7. outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
  8. 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


  1. def picbackup():
  2. source = ['-ir!"%USERPROFILE%\\*.jpg"', '-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
  3. target_dir = '.\\'
  4. today = time.strftime('%d_%m_%Y')
  5. target = '.\\Backup\Pictures_' + today + '.zip'
  6. zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
  7. outputWindow = Text(root)
  8. outputWindow.config(relief=SUNKEN, bg='beige', width=51, height=17)
  9. outputWindow.place(x=350, y=130)
  10. sys.stdout = outputWindow
  11. if os.system(zip_command) == 0: # Run the backup
  12. # can a progress bar be implemented or EBC anim ?????
  13. print('Successful backup to', target)
  14. else:
  15. 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.
  1. # Objective: To create a GUI for the ping utility
  2.  
  3. import os
  4. from tkinter import *
  5.  
  6. root = Tk()
  7. root.title('Ping GUI')
  8.  
  9. # the ping and clearScreen methods
  10. def ping():
  11. ip = ipField.get('1.0',END)
  12. f = os.popen('C:\\Windows\\System32\\PING.EXE '+ip)
  13. shellOutput = f.read()
  14. f.close()
  15. outputWindow.delete('1.0',END)
  16. outputWindow.insert('1.0',shellOutput)
  17.  
  18. def clearScreen():
  19. outputWindow.delete('1.0',END)
  20.  
  21. #create label for IP Address text field
  22. ipLabel = Label(root,text="Enter IP Address/DNS:")
  23. ipLabel.pack(side=LEFT)
  24.  
  25. # create the ip address text field
  26. ipField = Text(root)
  27. ipField.pack(side=LEFT)
  28. ipField.config(relief=SUNKEN, bg='beige',width=20,height=1)
  29.  
  30. # create the ping button
  31. b = Button(root,text="Ping",command=ping)
  32. b.pack(side=LEFT)
  33.  
  34. #create test button
  35. tb = Button(root,text="Clear Screen",command=clearScreen)
  36. tb.pack(side=LEFT)
  37.  
  38. #create the label for output window
  39. outputLabel = Label(root,text="Output Window")
  40. outputLabel.pack(side=BOTTOM)
  41.  
  42. #create the output window
  43. outputWindow = Text(root)
  44. outputWindow.config(relief=SUNKEN, bg='beige',width=50,height=20)
  45. outputWindow.pack(side=BOTTOM)
  46.  
  47. root.mainloop()

I need more encouragement i mean help!
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

Re: i want output in a window

 
0
  #4
Jul 25th, 2009
I need to get better at writing this stuff, reading back my own threads is confusing
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 76
Reputation: Clueless86 is an unknown quantity at this point 
Solved Threads: 1
Clueless86 Clueless86 is offline Offline
Junior Poster in Training

Re: i want output in a window

 
0
  #5
Jul 25th, 2009
Ah, don't feel bad.. I tend to confuse people the way I write/word things as well..
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

Re: i want output in a window

 
0
  #6
Jul 25th, 2009
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

  1. outputLabel = Label(root,text="7Zip Output Window")
  2. outputLabel.place(x=500,y=425)
  3. outputWindow = Text(root)
  4. outputWindow.config(relief=SUNKEN, bg='beige',width=51,height=17)
  5. outputWindow.place(x=350,y=130)
You press the button for the 7zip backup and you get this code
  1. def picbackup():
  2. source = ['-ir!"%USERPROFILE%\*.bmp"', '-ir!"%USERPROFILE%\*.tif"', '-ir!"%USERPROFILE%\\*.gif"']
  3. target_dir = '.\\'
  4. today = time.strftime('%d_%m_%Y')
  5. target = '.\\Backup\Pictures_' + today + '.zip'
  6. zip_command = "D:\\Backup\\7Zip\\7z.exe a -bd -ssw {0} {1}".format(target, ' '.join(source))
  7. if os.system(zip_command) == 0:
  8. # sys.stdout = outputWindow
  9. print('Successful backup to', target)
  10. else:
  11. 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.
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: i want output in a window

 
0
  #7
Jul 25th, 2009
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...?
Last edited by shadwickman; Jul 25th, 2009 at 4:15 am.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

Re: i want output in a window

 
0
  #8
Jul 25th, 2009
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.
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: i want output in a window

 
0
  #9
Jul 25th, 2009
Oh sorry! I didn't see your previous post metioning that Does anyone know if this has to do with Python 3.x? Changing the value of sys.sydout always worked for me...
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 42
Reputation: Aiban is an unknown quantity at this point 
Solved Threads: 0
Aiban Aiban is offline Offline
Light Poster

Re: i want output in a window

 
0
  #10
Jul 25th, 2009
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?
--
Ivan Wheelwright
Python 2.6 + tkinter + XP
Student of Japanese
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC