1

Toplevel() を使用して Tkinter で子ウィンドウを開くと、1 つではなく 2 つのウィンドウが開きます。これは私のコードです:

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()

上記のコードでは、メインに加えて 2 つの子ウィンドウが表示されます。子ウィンドウの 1 つは、ボタンとメッセージで問題なく表示されます。もう 1 つは、'tk' というタイトルの小さな空白のウィンドウです。

ここで、コードを最小限に減らすと、問題なく動作するように見えます。メイン ウィンドウと必要な 1 つの子ウィンドウを取得するだけです。

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()

余分な子ウィンドウを取得する理由について何か考えはありますか? その余分なウィンドウがポップアップする原因がわかりません。

4

1 に答える 1

0

Mac で最初のコード ブロックを実行しましたが、説明したように 3 つのウィンドウが表示されませんでした。2つしか見えません。pdfConverter のインポートをコメントアウトして機能させる必要があったため、それが原因でしょうか? そのインポートを最小化されたバージョンに追加してみて、余分なウィンドウが表示されるかどうかを確認してください。おそらく、pdfConverter がウィンドウを作成しています。

于 2013-09-14T11:55:40.097 に答える