代码在CMD中运行,但不在GUI中运行?

时间:2015-01-21 13:32:05

标签: python tkinter

这是我在python CMD中运行的代码:

import mailbox
import pprint
f = open("results.txt","w")
mbox = mailbox.mbox('c:\documents and  settings\student\desktop\mail\mailall.mbox')
count = 0
for msg in mbox:
    pprint.pprint(msg._headers, stream = f)
    if msg['Delivered-To'] == 'example@example.co.uk':
        count += 1 
f.close()
print(count)

然而,当我在GUI中运行它时,它会导致GUI崩溃(闪烁黑色并且不会打开)。这是GUI:

import datetime
from tkinter import filedialog
import tkinter


class App:

    def __init__(self, master):
        self.master = master

        # call start to initialize to create the UI elemets
        self.start()

    def start(self):
        self.master.title("Extract Email Headers")

        self.now = datetime.datetime.now()

        # CREATE A TEXT/LABEL
        # create a variable with text
        label01 = "Please select the .mbox file you would like to analyse"
        # put "label01" in "self.master" which is the window/frame
        # then, put in the first row (row=0) and in the 2nd column (column=1),
        # align it to "West"/"W"
        tkinter.Label(
            self.master, text=label01).grid(row=0, column=0, sticky=tkinter.W)

        # CREATE A TEXTBOX
            self.filelocation = tkinter.Entry(self.master)
        self.filelocation["width"] = 60
        self.filelocation.focus_set()
        self.filelocation.grid(row=1, column=0)

        # CREATE A BUTTON WITH "ASK TO OPEN A FILE"
        # see: def browse_file(self)
        self.open_file = tkinter.Button(
            self.master, text="Browse...", command=self.browse_file)
        # put it beside the filelocation textbox
        self.open_file.grid(row=1, column=1)



        # now for a button
        self.submit = tkinter.Button(
            self.master, text="Execute!", command=self.start_processing,
            fg="red")
        self.submit.grid(row=3, column=0)

    def start_processing(self):
        import mailbox
        import pprint
        f = open("results.txt","w")
        mbox = mailbox.mbox('c:\documents and settings\student\desktop\mail\mailall.mbox')
        count = 0
        for msg in mbox:
            pprint.pprint(msg._headers, stream = f)
                if msg['Delivered-To'] == 'example@example.co.uk':
                    count += 1 
        f.close()
        print(count)

    def browse_file(self):
        # put the result in self.filename
        self.filename = filedialog.askopenfilename(title="Open a file...")

        # this will set the text of the self.filelocation
        self.filelocation.insert(0, self.filename)

root = tkinter.Tk()
app = App(root)
root.mainloop()

如果我把流程代码拿出并放入"传递",GUI工作,所以我知道它的代码正在抛弃它。感谢

0 个答案:

没有答案