Python 将文本从文本框(GUI)复制到文件中

Python 将文本从文本框(GUI)复制到文件中,python,tkinter,Python,Tkinter,我已经构建了一个GUI,如果按下按钮,函数的输出就会进入文本框。现在我想通过将内容写入文本文件来创建一个报告 文本框的内容包括: Checks Status Remarks ECHConnect checked Structure disintegrates ECHQUALITY checked All 171665 elements are suitable ECHPenetra checked

我已经构建了一个GUI,如果按下按钮,函数的输出就会进入文本框。现在我想通过将内容写入文本文件来创建一个报告

文本框的内容包括:

Checks       Status          Remarks

ECHConnect   checked         Structure disintegrates

ECHQUALITY   checked         All 171665 elements are suitable


ECHPenetra   checked         EChPenet: 26093 elements penetrate/touch each other

ECHCha       not checked     

ECHCoincid   checked         20 coincident elements

ECHEdgeLen   checked         Shortest edge length = 0.2669
有命令吗

代码如下:

#!/usr/bin/python
import Tkinter as tk
from tkFileDialog   import *
import re

class StageGui:
    prot=0
    log=0
    def __init__(self,parent,tex):
        self.tex=tex
        self.f = tk.Frame(main, width=500, height=500)
        self.b1=tk.Button(main,text='Import prot file',command=self.callback).pack(side='left',anchor='nw')
        self.b2=tk.Button(main,text='Import log file',command=self.callback1).pack(side='left',anchor='nw')
        self.xf = tk.Frame(self.f,relief='groove', borderwidth=2)
        self.button=tk.Button(self.xf, text="CHECK",command=self.check).pack(side='left', padx=5,pady=8)
        self.button2=tk.Button(self.xf, text="SUMMARY",command=summary).pack(side='left', padx=5,pady=8)
        self.button3=tk.Button(self.xf, text="INERTIA",command=inertia).pack(side='left', padx=5,pady=8)
        self.button1=tk.Button(self.xf, text="CREATE REPORT").pack(side='left', padx=5,pady=8)
        self.xf.place(relx=0.01, rely=0.35, anchor='nw')
        self.label1=tk.Label(self.f, text='standard checks').place(relx=.06, rely=0.35,anchor='w')
        self.f.pack(side='left',fill='y',expand=1)

    def callback(self):
        name= askopenfilename()
        StageGui.prot=name
        self.printstatements(StageGui.prot)
    def printstatements(self,name):
        self.tex.insert('end','\nthe file has been imported \n')
        s='the path of the imported file is {}\n'.format(name)
        self.tex.insert('end',s)
        self.tex.see(tk.END)
        return
    def callback1(self):
        name1= askopenfilename()
        StageGui.log=name1
        self.printstatements(StageGui.log)
    def check(self):
        file=open(StageGui.prot,'r')
        a,b,c='Checks','Status','Remarks'
        mess='\n{0:10s} \t {1:10s} \t {2:100s}\n'.format(a,b,c)
        self.tex.insert('end',mess)
        count_string_occurance(file)

def count_string_occurance(file):
    string = "test"
    f = file
    contents = f.read()
    commcheck=['ECHConnect','ECHQUALITY','ECHPenetra','ECHCha','ECHCoincid','ECHEdgeLen','EMSameNorm','EEBeam','NEQ','Outline','NCHeck_nodes','GDelete']
    remcheck=['Label','INERTIA','Summary']
    special=['ECht']
    s='ECht'
    num=[]
    rnum=[]
    snum=[]
    for i in range(len(commcheck)):
        string=commcheck[i]
        num.append(contents.count(string))
    s=['connected','elements are suitable','penetrations','minimum','coincident elements','Shortest','to be reoriented','modified according to','multiple nodes found','Free edges displayed in','nodes are displayed','missing']
    p=['disintegrates','failed','touch','maximum','no coincident elements','Longest','reoriented','linked','multiple nodes found','free','nodes are displayed','elements deleted']
    for i in range(len(num)):
        if num[i]>0:
            status='checked'
            with open(StageGui.log,'r') as fy:
                for line in fy:
                    if s[i] in line:
                        mess='\n{0:10s} \t {1:10s} \t {2:100s}\n'.format(commcheck[i],status,line)
                        tex.insert('end',mess)
                        break
                    elif p[i] in line:
                        mess= '\n{0:10s} \t {1:10s} \t {2:100s}\n'.format(commcheck[i],status,line)
                        tex.insert('end',mess)
        else:
            status='not checked'
            new= '\n{0:10s} \t {1:10s} \t \n'.format(commcheck[i],status)
            tex.insert('end',new)
    for i in range(len(remcheck)):
        nstring=remcheck[i]
        rnum.append(contents.count(nstring))
    for i in range(len(rnum)):
        if rnum[i]>0:
            status='checked'
            mess='\n{0:10s} \t {1:10s} \t \n'.format(remcheck[i],status)
            tex.insert('end',mess)
        else:
            status='not checked'
            new= '\n{0:10s} \t {1:10s} \t \n'.format(remcheck[i],status)
            tex.insert('end',new)

    nstring1=special[0]
    flag=0
    if nstring1=='ECht':
        searchstring='degenerated'
        with open(StageGui.log,'r') as fz:
            for line in fz:
                if searchstring in line:
                    status='checked'
                    mess='\n{0:10s} \t {1:10s} \t {2:100s}\n'.format(nstring1,status,line)
                    tex.insert('end',mess)
                    flag=1
                    break
            if flag==0:
                status2='not checked'
                mess2='\n{0:10s} \t {1:10s} \t \n'.format(nstring1,status2)
                tex.insert('end',mess2)
    f.close()

def summary():
    tex.delete('1.0','end')
    with open(StageGui.prot,'r') as input:
        intable = False
        for line in input:
                if line.strip() == "Summary":
                intable = True
                if line.strip() == "*  ============================== INERTIA ==============================":
                        intable = False
                if intable and line.strip().startswith("*"):
                        z=(line.strip())
                tex.insert('end','\n')
                tex.insert('end',z)
                tex.insert('end','\n')

def inertia():
    tex.delete('1.0','end')
    with open(StageGui.prot,'r') as input:
        intable = False
        for line in input:
                if line.strip() == "*  ============================== INERTIA ==============================":
                        intable = True
            if line.strip() == "Summary":
                intable = False
                if intable and line.strip().startswith("*"):
                        z=(line.strip())
                tex.insert('end','\n')
                tex.insert('end',z)
                tex.insert('end','\n')




main = tk.Tk()
tex= tk.Text(main,width=100)
tex.pack(side='bottom',anchor='s',fill='both',expand='yes')
stagegui=StageGui(main,tex)
main.title('prototype')
main.geometry('800x800+300+300')
main.mainloop()

因此,基本上有两种方法可以采用:第一种,我通常更喜欢,是跟踪正在写入窗口的值,并以相同的方式将它们分别写入文件。但是,如果你真的想从窗口阅读,很简单:

outtext = tex.get('1.0', 'end')
outfile = open('whatever', 'w')
outfile.write(outtext)
outfile.close()

如果你发布到目前为止的代码会有所帮助。我已经用代码更新了帖子。你能检查一下吗@williammcbrine你的缩进搞错了,请修复。很抱歉缩进错误,但是你给出的代码行很有用,非常感谢。。。。。。。