当我使用 Tkinter 使用 Toplevel() 打开一个子窗口时,会打开两个窗口而不是一个。这是我的代码:
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()
上面的代码除了主窗口外,还给了我两个子窗口。其中一个子窗口与按钮和消息看起来很好。另一个只是一个标题为“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()
关于为什么我可能会得到那个额外的子窗口的任何想法?我不知道是什么可能导致那个额外的窗口弹出。