用于打印的Python Tkinter-方法在从另一个脚本调用后无法正常工作

时间:2017-07-24 13:57:53

标签: python-2.7 tkinter

在你说这是重复版或下注版之前,请仔细阅读。 我在该类中有一个名为app.py的类,有一个名为pip install pyspark --user 的方法,这里是类和方法的一部分

print_raw_records_screen

此方法由另一个外部脚本调用,如

app.py


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


from Tkinter import *
from ttk import *
import os
import mftsession


class Example(Frame):

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

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

#defining a function that that receives the raw records from the mftsession.py and print it to the screen
#this script will be called by the mftsession.py in 
#process_mftfile()
def print_raw_records_screen(self,records):
    self.area.delete(1.0, "end")
    self.area.insert('1.0',records)


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)

    self.filename_area = Entry(self)
    self.filename_area.grid(row=1, column=1, columnspan=5, padx=5, sticky=E+W+S+N)

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

    self.area = Text(self)
    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
    self.set( self.filename)



#do the processing of the file obtained. Populate the file NAME entry or
#send the filename to the analyzeMFT.py
def processFile(self):       
    arguements = "analyzeMFT.py -f "+self.filename+" -d --bodyfull -l -o "+self.filename+".csv"
    os.system( arguements )

    mftsession.MftSession.process_mft_file(self)

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

def set(self, value):
    self.filename_area.delete(0, END)
    self.filename_area.insert(0,value)



def main():

  root = Tk()
  root.geometry("450x350+500+500")
  app = Example(root)
  root.mainloop()  


if __name__ == '__main__':
    main()

app.py通过子路由

调用的脚本analyzeMFT.py
mftsession.py


import csv
import json
import os
import sys
from optparse import OptionParser

import mft

 import app
from Tkinter import *
from ttk import *

class MftSession:
"""Class to describe an entire MFT processing session"""

@staticmethod
def fmt_excel(date_str):
    return '="{}"'.format(date_str)

@staticmethod
def fmt_norm(date_str):
    return date_str


def __init__(self):       

    self.mft = {}
    self.fullmft = {}
    self.folders = {}
    self.debug = False
    self.mftsize = 0

def process_mft_file(self):        

    root = Tk()
    appi = app.Example(root)

    self.sizecheck()

    self.build_filepaths()

    # reset the file reading
    self.num_records = 0
    self.file_mft.seek(0)
    raw_record = self.file_mft.read(1024)

    if self.options.output is not None:
        self.file_csv.writerow(mft.mft_to_csv(None, True, self.options))

    while raw_record != "":
        record = mft.parse_record(raw_record, self.options)
        if self.options.debug:
            print record
            appi.print_raw_records_screen(raw_record )  #THIS FUNCTION WHILE INVOKED THIS WAY IS NOT WORKING
            ..........

以这种方式调用时,它不会向窗口打印任何内容,但是当我在其自己的类中调用它进行测试时,它会打印测试

#!/usr/bin/python
import sys
from os import path

def main(): 


  session = mftsession.MftSession()
  session.mft_options()
  session.open_files()
  session.process_mft_file()



 if __name__ == '__main__':

   if __package__ is None:
        sys.path.append( path.dirname( path.dirname( path.abspath(__file__) ) ) )
    import mftsession
    main()

else:
    import mftsession
    main()     

并在app.py中调用def print_raw_records_screen(self,records): self.area.delete(1.0, "end") self.area.insert('1.0', records)

enter image description here

图像可以说明很多。并总结

我做错了什么?我觉得这是实例化我做错了。我需要指示

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

从我在这里看到的,你有一些问题导致问题。

  1. 您正在app.py上导入mftsession.py,而不是mftsession.py上导入app.py

  2. 您正尝试在appi.print_raw_records_screen(raw_record)类的完全不同的实例上使用Example() appi = app.Example(root)将所有部分一起删除。

  3. 在函数内导入是不好的做法。在每个py文件的开头导入。

  4. 你的代码中发生了很多事情我必须创建一个我自己的Minimal, Complete, and Verifiable example示例来说明文件之间的关系。

    以下是两个文件如何互动的简单示例以及我认为您尝试做事的方式。

    这里我创建了一个名为app.py的主文件:

    from Tkinter import *
    # You might need to import the py file with the package name as well.
    # Change the package name to the package your python files are located in.
    import PACKAGE_NAME.file_b
    
    class Example(Frame):
    
        def __init__(self, parent):
            Frame.__init__(self, parent)
    
            self.parent = parent
            self.filename = ""        
            analize_button = Button(self.parent, text="Analize",  command=self.processFile)
            analize_button.pack()
            self.area = Text(self.parent, height = 2, width = 40)
            self.area.pack()
    
        def print_raw_records_screen(self,records):
            self.area.delete(1.0, "end")
            self.area.insert('1.0',records)
    
        def processFile(self):       
            PACKAGE_NAME.file_b.process_mft_file(self)
    
    
    
    if __name__ == "__main__":
        root = Tk()
        app = Example(root)
        root.mainloop()
    

    我在这里创建了一个名为file_b.py

    的文件
    from Tkinter import *
    
    def process_mft_file(self):        
        record = "Some values assigned to a variable"
        self.print_raw_records_screen(record)
    

    结果看起来像这样:

    enter image description here