tkinter子窗口打开两个窗口?

时间:2013-09-14 05:48:24

标签: python tkinter

当我使用Toplevel()打开一个带有Tkinter的子窗口时,会打开两个窗口而不是一个窗口。这是我的代码:

import os
import sys
from Tkinter import *
import tkFileDialog
from pdfConverter import *

#Some important parameters
docDir = '/Users/Person/Python/12_PythonPackages/01_PyDocSearch/Library'

currFilePath = os.getcwd()
fileList = []
allPossibleFiles = os.listdir(docDir)
for file in allPossibleFiles:
    if '.pdf' in file or '.doc' in file:
        fileList.append(file)

def startSearch():
    filePath = input_dir.get()
    findText = textToSearch.get()
    status.set("Searching now")


    def buildIndex():
        try:
            fileName = fileList.pop()
            statusStr = "Status: Working on file: " + str(fileName)
            print statusStr
            status.set(statusStr)
            if '.pdf' in fileName:
                doc = convert_pdf(docDir + '/' + fileName)
                #doc = convert_pdf(docDir + '/' + fileName)
                if findText in doc:
                    foundSpot = doc.find(findText)
                    print doc[foundSpot-100:foundSpot+100]
                else:
                    print 'Text not found'
            #doc.close()
            #infile = open(filePath + '/' + fileName,'r')
            #infile.close()
        except IndexError:
            statusStr = "Status: Done! You can press 'Quit' now"
            print statusStr
            status.set(statusStr)
            return
        #root.after(100,fixFiles)
    root.after(10,buildIndex)

def input_file():
    #Get the input directory.
    directory1 = tkFileDialog.askdirectory(mustexist=True)
    input_dir.set(directory1)

def output_file():
    #Get the input directory.
    directory2 = tkFileDialog.askdirectory(mustexist=True)
    output_dir.set(directory2)

class App:
    def __init__(self, master):

        l = []
        l.append(Label(master, text=""))
        l.append(Label(master, text=" Program: PyDocSearcher"))
        l.append(Label(master, text=" Version: 0.1"))
        l.append(Label(master, text=""))
        l.append(Label(master, text=""))
        for i in range(len(l)):
            l[i].grid(row = i, columnspan = 4, sticky = W)

        i += 1
        inputrow = []
        inputrow.append(Label(master, text="Enter directory to search:"))
        inputrow.append(Entry(root, width = 40, textvariable=input_dir))
        inputrow.append(Button(root, text='Browse', command=input_file))
        i += 1
        for j in range(len(inputrow)):
            inputrow[j].grid(row = i, column=j, sticky = W)

        i += 1
        inputrow = []
        inputrow.append(Label(master, text="Search for text:"))
        inputrow.append(Entry(root, width = 40, textvariable=textToSearch))
        i += 1
        for j in range(len(inputrow)):
            inputrow[j].grid(row = i, column=j, sticky = W)

        i += 1
        spacer = Label(master, text="")
        spacer.grid(row = i)

        i += 1
        status.set("Status: Enter your selections and press 'Fix Files!'")
        statuslabel = Label(master, textvariable=status, relief = RIDGE, width = 80, pady = 5, anchor=W)
        bFixFiles = Button(root, text='Search!', command = startSearch)
        bQuit = Button(root, text='Quit', command = root.destroy)

        statuslabel.grid(row=i, column = 0, columnspan = 2)
        bFixFiles.grid(row=i, column=2, sticky=E)
        bQuit.grid(row=i, column=3, sticky=W)

        top = Toplevel()
        top.title("About this application...")

        msg = Message(top, text="about_message")
        button = Button(top, text="Dismiss", command=top.destroy)

        button.pack()
        msg.pack()



root = Tk()
root.title("PyDocSearcher")
input_dir = StringVar()
textToSearch = StringVar()
status = StringVar()
choice = IntVar()
app = App(root)

# start Tkinter
root.mainloop()

除了main之外,上面的代码还给了我两个子窗口。使用按钮和消息,其中一个子窗口看起来很好。另一个只是一个标有“tk”的小空白窗口。

现在,如果我将代码减少到最低限度,它似乎工作正常。我只需要主窗口和我想要的一个子窗口:

from Tkinter import *
import tkFileDialog

class App:
    def __init__(self, master):
        inputrow = Label(master, text="This is some text")
        inputrow.grid(row=1)
        bQuit = Button(root, text='Quit', command = root.destroy)
        bQuit.grid(row=2, column=3, sticky=W)

        top = Toplevel()
        top.title("About this application...")

        msg = Message(top, text="about_message")
        button = Button(top, text="Dismiss", command=top.destroy)
        button.pack()
        msg.pack()

root = Tk()
root.title("Test Title")
app = App(root)
# start Tkinter
root.mainloop()

关于为什么我可能会获得额外的子窗口的任何想法?我无法弄清楚可能导致额外窗口弹出的原因。

1 个答案:

答案 0 :(得分:0)

我在我的Mac上运行了第一个代码块,并且我按照你的描述获得了三个窗口。我只看到两个。由于我不得不注释掉pdfConverter的导入才能使它工作,也许这就是罪魁祸首?尝试将该导入添加到最小化版本,看看是否有额外的窗口。也许pdfConverter正在为你创建一个窗口。