tkinter askopenfilenames() 在第二次激活时关闭 GUI

问题描述

以下是第一遍工作。选择文件/秒,激活查看按钮和定义,查看文件。都好。 尝试选择另一个文件时,在选择过程中,GUI 会突然关闭。 我试图找出需要重置的变量,但这并没有改变。 在所有进口之后:


root = tk.Tk()
root.title("Motion GUI")
root.minsize(width=1000,height=700)
root.maxsize(width=1400,height = 900)

mainframe = ttk.Frame(root,padding="3 3 12 12")
mainframe.grid(column=0,row=0,sticky=(N,W,E,S))
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)

WidgetFrame = ttk.Frame(mainframe,borderwidth=2,relief='ridge',height = 60)
WidgetFrame.grid(column=0,sticky="E,W")

def CreateWidgets():
    
    link_Label = Label(WidgetFrame,text ="Select File(s): ",bg = "#E8D579") 
    link_Label.grid(row = 1,column = 0,pady = 5,padx = 5) 
       
    WidgetFrame.sourceText = Entry(WidgetFrame,width = 50,textvariable = sourceLocation) 
    WidgetFrame.sourceText.grid(row = 1,column = 1,padx = 5,columnspan = 2) 
     
    source_browseButton = Button(WidgetFrame,text ="browse",command = Sourcebrowse,width = 15) 
    source_browseButton.grid(row = 1,column = 3,padx = 5) 
     
    viewButton = Button(WidgetFrame,text ="View File(s)",command = ViewFile,width = 15) 
    viewButton.grid(row = 3,padx = 5) 

def Sourcebrowse(): 

    WidgetFrame.files_list = list(filedialog.askopenfilenames(initialdir ="/mnt/data/Motion_Data/Motion_Clips",title="Press shift key plus Left mouse click to select multiple files")) 

def ViewFile():
    # plays all selected files one by one,keeps speed selected in 1st clip unless changed..
    files_list = WidgetFrame.files_list

    for f in files_list:
        player.playlist_append(f)

    player.playlist_pos = 0
    player.wait_for_playback

sourceLocation = StringVar()
destinationLocation = StringVar() 
file_list = StringVar()
CreateWidgets() 
     
root.mainloop() 

我错过了什么?

解决方法

这是包含所有按钮和功能的完整代码。顺便说一句,所有其他按钮和功能都可以正常工作(即:复制/移动/删除):

#!/usr/bin/python3

import os
import shutil 
import tkinter as tk 
from tkinter import *
from tkinter import  filedialog,messagebox
from tkinter import ttk
import webbrowser

if os.environ.get('DISPLAY','') == "":
    print('no display found.Using :0.0')
    os.environ.__setitem__('DISPLAY',':0.0')

##### MPV.py HAS TO BE copied into the directory which imports mpv  ######
import mpv
player = mpv.MPV(ytdl=True,input_default_bindings=True,input_vo_keyboard=True,osc=True)
media = open('mPlaylist.u3e','r').read().splitlines()
# Creating object of tk class 
root = tk.Tk()
root.title("Motion GUI")
root.minsize(width=1000,height=700)
root.maxsize(width=1400,height = 900)

mainframe = ttk.Frame(root,padding="3 3 12 12")
mainframe.grid(column=0,row=0,sticky=(N,W,E,S))
root.columnconfigure(0,weight=1)
root.rowconfigure(0,weight=1)

WidgetFrame = ttk.Frame(mainframe,borderwidth=2,relief='ridge',height = 60)
WidgetFrame.grid(column=0,sticky="E,W")

# Setting the title and background color disabling the resizing property 
root.geometry("830x420") 
root.title("View/Copy/Move/Delete mkv's") 
root.config(background = "gray")

def CreateWidgets():
    
    WidgetFrame.link_Label = Label(WidgetFrame,text ="Select File(s): ",bg = "#E8D579") 
    WidgetFrame.link_Label.grid(row = 1,column = 0,pady = 5,padx = 5) 
    
    
    WidgetFrame.sourceText = Entry(WidgetFrame,width = 50,textvariable = sourceLocation) 
    WidgetFrame.sourceText.grid(row = 1,column = 1,padx = 5,columnspan = 2) 
     
    WidgetFrame.source_browseButton = Button(WidgetFrame,text ="Browse",command = SourceBrowse,width = 15) 
    WidgetFrame.source_browseButton.grid(row = 1,column = 3,padx = 5) 
     
    WidgetFrame.destinationLabel = Label(WidgetFrame,text ="Select The Destination : ",bg ="#E8D579") 
    WidgetFrame.destinationLabel.grid(row = 2,padx = 5) 
     
    WidgetFrame.destinationText = Entry(WidgetFrame,textvariable = destinationLocation) 
    WidgetFrame.destinationText.grid(row = 2,columnspan = 2) 
     
    WidgetFrame.dest_browseButton = Button(WidgetFrame,command = DestinationBrowse,width = 15) 
    WidgetFrame.dest_browseButton.grid(row = 2,padx = 5) 

    WidgetFrame.viewButton = Button(WidgetFrame,text ="View File(s)",command = ViewFile,width = 15) 
    WidgetFrame.viewButton.grid(row = 3,padx = 5) 
        
    WidgetFrame.copyButton = Button(WidgetFrame,text ="Copy File",command = CopyFile,width = 15) 
    WidgetFrame.copyButton.grid(row = 3,padx = 5) 
     
    WidgetFrame.moveButton = Button(WidgetFrame,text ="Move File",command = MoveFile,width = 15) 
    WidgetFrame.moveButton.grid(row = 3,column = 2,padx = 5) 

    WidgetFrame.moveButton = Button(WidgetFrame,text ="Delete File(s)",command = DeleteFile,padx = 5)

    WidgetFrame.MotionButton = Button(WidgetFrame,text="IP Streams",command=MotionHTTP,font="LUCIDA 12")
    WidgetFrame.MotionButton.grid(row=3,column=5,pady=5,padx=5)
    
    WidgetFrame.exitButton = Button(WidgetFrame,text="Quit",command=root.destroy,font="LUCIDA 12 bold")
    WidgetFrame.exitButton.grid(row = 3,column = 6,padx = 5)
    
def MotionHTTP():
    webbrowser.open("http://192.168.0.26:8080")

def SourceBrowse(): 
     
 
    WidgetFrame.files_list = list(filedialog.askopenfilenames(initialdir ="/home/rainer/Videos",title="Press shift key plus Left mouse click to select multiple files")) 
    files_list = []
    WidgetFrame.sourceText.insert('1',WidgetFrame.files_list)
     
def DestinationBrowse(): 
    destinationdirectory = filedialog.askdirectory(initialdir ="/mnt/data/Motion_Data/Motion_Clips") 
    WidgetFrame.destinationText.insert('1',destinationdirectory) 

def ViewFile():
    # plays all selected files one by one,keeps speed selected in 1st clip unless changed..
    files_list = WidgetFrame.files_list
        for f in files_list:
            print(f)
        player.playlist_append(f)
        player.playlist(files_list)
        #player.playlist_pos = 0
    player.wait_for_playback

    playlist.close()

def CopyFile(): 
    files_list = WidgetFrame.files_list 
    destination_location = destinationLocation.get() 
    # Looping through the files present in the list 
        shutil.copy(f,destination_location)
     
def MoveFile(): 
    files_list = WidgetFrame.files_list 
    destination_location = destinationLocation.get() 
    for f in files_list: 
        shutil.move(f,destination_location) 

def DeleteFile(): 
     
    files_list = WidgetFrame.files_list 
    for f in files_list: 
         
        os.remove(f)
 
# Creating tkinter variable 
sourceLocation = StringVar()
destinationLocation = StringVar()  
files_list = StringVar()
#destinationLocation = StringVar(value='/home/rainer/Music')
#file_list = StringVar()
# Calling the CreateWidgets() function 
CreateWidgets() 
     
# Defining infinite loop 
root.mainloop()