【问题标题】:tkinter child window opens two windows?tkinter 子窗口打开两个窗口?
【发布时间】:2013-09-18 21:01:29
【问题描述】:

当我使用 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()

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

【问题讨论】:

  • 你的脚本在这里只打开两个窗口。您确定第三个与 XWindow 无关(并且特定于 Mac OX)吗?你能说出额外窗口中的内容吗?
  • 是的,我不知道。我只知道我在顶部发布的代码给了我 3 个窗口,而我在下面发布的精简代码只给了两个窗口。子窗口的代码是相同的。正如我所提到的,额外的窗口是空白的,标题为“tk”。
  • 我使用 Linux,并且我没有带有任何脚本的空 Tk 窗口。我的猜测是,所有 tk 脚本都会出现一次 Tk 窗口。如果我是对的,那么如果在 Tk 和 Xwindow 运行时终止后首先运行它,您也将使用您的第二个脚本获得它。

标签: python tkinter


【解决方案1】:

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

【讨论】:

  • 奇怪...我注释掉了 pdfConverter,我仍然得到三个窗口。下周我将不得不在我的 windows 和 linux 计算机上尝试这个代码,看看我是否仍然遇到这个问题。到目前为止,我只在家里测试过。
猜你喜欢
  • 2018-11-17
  • 2018-05-11
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 2017-01-17
  • 2021-02-15
  • 1970-01-01
  • 2013-06-30
相关资源
最近更新 更多