_tkinter.TclError:无法调用“顶级”命令:应用程序已被破坏

时间:2019-03-19 06:17:45

标签: tkinter

我目前正在开发一个程序,该程序需要用户输入,搜索具有相同名称的txt和png文件,如果这些文件存在,则将结果显示在新窗口中。该程序还会显示网络摄像头。当我仅尝试打开文件而不带视频流时,该程序将运行。同样,如果我仅尝试显示视频流,则该程序有效。但是,如果我尝试同时执行这两个操作,则会出现标题中给出的错误。这是我的代码,

import tkinter,time,os,cv2
from tkinter import *
from PIL import ImageTk, Image

#Create main window
master = Tk()
master.title("Main window")
master.minsize(150,100)
master.geometry("1500x1000")

# Create a frame
app = Frame(master, bg="white")
app.pack(side="bottom")
# Create a label in the frame
lmain = Label(app)
lmain.pack(side="bottom")

# Capture from camera
cap = cv2.VideoCapture(0)
threshold = 7
path = "C:/Users/Desktop/"

def getentry():
    entried = e.get()
    if len(str(entried)) == threshold:

        #Make error message empty
        ErrorMessage.configure(text = "")
        ErrorMessage.update()

        img_name = "{}.png".format(entried)
        txt_name = "{}.txt".format(entried)

        #if that file exists
        if(os.path.isfile(path+img_name)):
            #create a new window
            new_window = Toplevel(master) *****Problem is here******
            #open the txt file
            with open(path+txt_name, "r") as f:
                content = tkinter.Label(new_window, text=f.read())
                content.pack(side="bottom")
            #open the image file
            img = ImageTk.PhotoImage(Image.open(path+img_name))
            l1 = tkinter.Label(new_window, image=img)
            l1.image = img
            l1.pack(side="top")
        else:
            ErrorMessage.configure(text = "error")
            ErrorMessage.update()

    elif len(str(entried)) > threshold:
        ErrorMessage.configure(text = "err2")
        ErrorMessage.update()
    else:
        ErrorMessage.configure(text = "err3")
        ErrorMessage.update()

# function for video streaming
def video_stream():
    _, frame = cap.read()
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(1, video_stream) 


#Label for error message
ErrorMessage = Label(master,text = '', fg = 'red',font = "Times 13 bold")
ErrorMessage.pack(side=TOP,padx=2, pady=7)

#A label and entry for the user
Label(master, text="Enter here:").pack(side=TOP, padx=2)
e = StringVar()
Entry(master, width=20, textvariable=e).pack(side=TOP)

#Buttons
Search_button = Button(master,text ="Search", command=getentry,height=2,width=10).pack(side="top")

Video_button = Button(master,text ="Video", command=video_stream,height=2,width=10).pack(side="top")

mainloop()

0 个答案:

没有答案
相关问题