-->

Output from python console

2019-08-24 01:35发布

问题:

I have the following problem, I have separate python functions in different files. That I call when I need them.

Currently, I am trying to create gui with tkinter and I need to display output from functions that I call. Since I suppose to read what python console generates I tried to use subprocess but it didn't work out, I suppose I have to read stdout and then pass it to tkinter listbox, but methods that I tried didn't work.

from Tkinter import *
import ConfigParser, sys
import subprocess as sub
from my_update import *
from  mac_generator import *



class testing:

    def __init__(self, parent):
        self.parent=parent
        status_file = "status.ini"
        status = ConfigParser.ConfigParser()
        status.read(status_file)
        self.mac_last = status.get('status', 'mac_last')
        self.parent=parent
        Button(parent, text="Upgrade", command=self.upgradeproc).pack(side=BOTTOM, padx=10, pady=10)
        self.ff=Frame(self.parent, bd=1, relief=SUNKEN)
        Label(self.ff, text="LAST MAC: " + self.mac_last, justify=LEFT, anchor=W, width=28).pack()
        self.ff.pack(side=TOP, pady=15)


    def upgradeproc(self):
        list = Listbox(root, width=50, height=30)
        list.pack()
        proc = sub.Popen(testgen(), stdout=sub.PIPE)
        for line in iter(proc.stdout.readline, ''):
            list.insert(END, line)



if __name__ == '__main__':
    root = Tk()
    root.resizable(width=FALSE, height=FALSE)
    root.geometry("350x350")
    root.title('Test')
    testing(root)
    root.mainloop()