tkinter 子窗口打开两个窗口? [英] tkinter child window opens two windows?

查看:61
本文介绍了tkinter 子窗口打开两个窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I open a child window with Tkinter using Toplevel(), two windows open instead of just one. This is my code:

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"的空白小窗口.

The above code gives me TWO child windows in addition to the main. One of the child windows looks fine with the button and the message. The other one is just a small blank window with the title 'tk'.

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

Now, if I reduce my code to the bare minimum, it seems to work fine. I just get the main window and the one child window that I want:

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

关于为什么我可能会得到那个额外的子窗口的任何想法?我不知道是什么原因导致弹出额外的窗口.

Any ideas as to why I might be getting that extra child window? I can't figure out what might be causing that extra window to pop up.

推荐答案

我在我的 mac 上运行了第一个代码块,但我没有像你描述的那样获得三个窗口.我只看到两个.由于我必须注释掉 pdfConverter 的导入才能使其工作,也许这就是罪魁祸首?尝试将该导入添加到您的最小化版本中,看看您是否获得了额外的窗口.也许 pdfConverter 正在为您创建一个窗口.

I ran the first block of code on my mac and I did not get three windows as you describe. I see only two. Since I had to comment out the import of pdfConverter to get it to work, maybe that's the culprit? Try adding that import to your minimized version and see if you get the extra window. Perhaps pdfConverter is creating a window for you.

这篇关于tkinter 子窗口打开两个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆