将脚本的输出打印到python中的窗口 [英] Printing the output of a script to a window in python

查看:66
本文介绍了将脚本的输出打印到python中的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为analyzeMFT python 程序创建一个GUI.到目前为止,这是我所拥有的

I am trying to create a GUI for the analyzeMFT python program. So far this is what i have

#!/usr/bin/python
  # -*- coding: utf-8 -*-


from Tkinter import *
from ttk import *
import analyzeMFT


class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent
        self.filename = ""        
        self.initUI()


    def initUI(self):

        self.parent.title("Mtf Analyzer")

        #initializing and configuring menubar

        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)

        fileMenu = Menu(menubar)
        fileMenu.add_command(label="Open file", command=self.fileOpen)
        fileMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenu)


        #specify grid row and column spaces

        self.pack(fill=BOTH, expand=True)

        self.columnconfigure(1, weight=1)
        self.columnconfigure(3, pad=7)
        self.rowconfigure(3, weight=1)
        self.rowconfigure(5, pad=7)

        lbl = Label(self, text="File Name")
        lbl.grid(row=1, column=0, sticky=W, pady=4, padx=5)

        filename_area = Entry(self)
        filename_area.grid(row=1, column=1, columnspan=3, 
            padx=5)

        analize_button = Button(self, text="Analize",  command=self.processFile)
        analize_button.grid(row=1, column=4, padx=5)

        area = Text(self)
        area.grid(row=2, column=1, columnspan=2, rowspan=4, 
            padx=5, sticky=E+W+S+N)



        #configure the raw output view


    def onExit(self):
        self.quit()

    #this function selects and opens the file to analize
    def fileOpen(self):
        from tkFileDialog import askopenfilename

        Tk().withdraw() 
        self.filename = askopenfilename() 

        #populate the filename field



    #do the processing of the file obtained. Populate the file NAME entry or
    #send the filename to the analyzeMFT.py
    def processFile(self):
        filei = "-f "+self.filename
        arguements = [filei, '-o '+self.filename+' .csv']
        analyzeMFT.main( arguements )


    #get and set methods for the entry field
    def get(self):
        return self.filename_area.get()

    def set(self, value):
        filename_area.set(value)


def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main()

此代码创建了一个 gui,我可以使用analyzeMFT 程序选择要分析的文件.有两个问题.

This code creates a gui that i can chose a file to analyze using the analyzeMFT program. There are two problems.

1.analyzeMFT 旨在在命令行上运行.我无法将我从 GUI 获得的文件名传递给analyzeMFT.py 这里是analyzeMFT.py 的内容

1.analyzeMFT is meant to be run on command line. I am not able to pass the file name I get from the GUI to the analyzeMFT.py Here are the contents of analyzeMFT.py

    #!/usr/bin/python

try:
    from analyzemft import mftsession 
except:
    from .analyzemft import mftsession

def main(): 
    session = mftsession.MftSession()
    session.mft_options()
    session.open_files()
    session.process_mft_file()

if __name__ == "__main__":
    main()

接下来,当我在 cmd 中运行analyzerMFT 并启用调试模式时,它会在屏幕上打印每个细节.我怎样才能将它指向我在下面显示的窗口

Next, when i run the analyzerMFT in cmd and enable debug mode, it prints every detail on the screen. How can I direct that out to a window i have shown below

我希望它看起来像这样

对不起,如果解释很长.我已经为此工作了好几天.

Sorry if the explanation is very long. I've been working on it for days.

推荐答案

您可以在文本框中使用 insert() ,而不是像通常用于向控制台显示结果的那样使用打印.

Instead of using print like you would normally use to display results to the console you can use insert() on your text box.

第一次改变:

area = Text(self)
area.grid(row=2, column=1, columnspan=2, rowspan=4, 
            padx=5, sticky=E+W+S+N)

致:

self.area = Text(self)
self.area.grid(row=2, column=1, columnspan=2, rowspan=4, 
            padx=5, sticky=E+W+S+N)

这样您的文本框就被设置为我们可以使用的类属性.

This way your text box is set up as a class attribute that we can use.

然后使用:

self.area.delete(1.0, "end") # to clear text box 
self.area.insert("end", your_output variable or string) # to insert content

这篇关于将脚本的输出打印到python中的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆