LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python showing command output in a frame/label (https://www.linuxquestions.org/questions/programming-9/python-showing-command-output-in-a-frame-label-4175551789/)

barunparichha 08-26-2015 08:23 AM

Python showing command output in a frame/label
 
Hi,

How to redirect python command output to a frame/label in python ?

Code:

import os
res = os.system("ls -l");

Above python script just shows this output to my terminal. I want to display this on some python window like frame/label.


Regards,
Barun

weibullguy 08-26-2015 11:38 AM

You'll have to use some sort of GUI toolkit. Which one you use will dictate how to display text in a label or other widget.

barunparichha 08-27-2015 12:29 AM

GUI toolkit will just ease the process of writing your code/design.
But my question is rather very simple and irrelevant of any toolkit you use.

Lets say one of my python function is executing some script as given above, and I have to show this output/error(if any) on a python GUI frame instead of showing on a terminal.
Can you paste me the code to redirect this output to the frame ?

N.B.
This could only be answered by someone who has done some scripting in python. So fine if you do not know this.

HMW 08-27-2015 01:49 AM

Quote:

Originally Posted by barunparichha (Post 5411727)
GUI toolkit will just ease the process of writing your code/design.

We must live on different planets, because I find writing GUI wrappers a pain in the b*tt!

Quote:

Originally Posted by barunparichha (Post 5411727)
Lets say one of my python function is executing some script as given above, and I have to show this output/error(if any) on a python GUI frame instead of showing on a terminal.
Can you paste me the code to redirect this output to the frame ?

Yes I can. But I won't. Instead, here are a couple of links that will help you:
https://docs.python.org/3.0/library/tkinter.html
https://docs.python.org/3/library/tk.html
http://www.tkdocs.com/tutorial/index.html

And here's a very simple code snippet to get you started:
Code:

#!/usr/bin/env python3

from tkinter import *

root = Tk()

aWidget = Label(root, text="Hello LinuxQuestions!")
aWidget.pack()

root.mainloop()

Best regards,
HMW

dugan 08-27-2015 12:18 PM

Quote:

Originally Posted by barunparichha (Post 5411727)
GUI toolkit will just ease the process of writing your code/design.
But my question is rather very simple and irrelevant of any toolkit you use.

No, you do need to choose a GUI toolkit. You need use that toolkit to create the window that will display your output. If the toolkit you want is the one that IDLE uses, then HMW already posted a snippet for that.

Here's one for PySide:

Code:

#!/usr/bin/env python

from PySide import QtGui
import subprocess
import sys


    def main():
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        window.setText(subprocess.check_output(['ls', '-l']))
        sys.exit(app.exec_)()


class Window(QtGui.QMainWindow):

        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
            self._text = QtGui.QTextEdit()
            self._text.setReadOnly(True)
            self.setCentralWidget(self._text)

        def setText(self, text):
            self._text.setText(text)


if __name__ == '__main__':
    main()



All times are GMT -5. The time now is 12:39 AM.