选择Python tkinter验证文件

选择Python tkinter验证文件,python,tkinter,Python,Tkinter,我在Python3.7中使用Tkinter,让用户使用askopenfilename窗口选择文件。我还有一个“运行”按钮。我想检查一下文件是否已打开。如果没有打开任何文件,我希望看到一条错误消息,如果选择了一个文件,我希望程序运行 以下是我目前掌握的代码: import tkinter as tk from tkinter import * from tkinter import ttk from tkinter import filedialog class GetInfo: def

我在Python3.7中使用Tkinter,让用户使用askopenfilename窗口选择文件。我还有一个“运行”按钮。我想检查一下文件是否已打开。如果没有打开任何文件,我希望看到一条错误消息,如果选择了一个文件,我希望程序运行

以下是我目前掌握的代码:

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog

class GetInfo:
    def __init__(self, master):
        self.master = master
        self.FileLabel = Label(master, text="Open the File to Use", font=("Arial Bold", 13)).grid(row=9, column=0,
                                                                                                  sticky=W)
        openFileCommand = master.register(self.FileOpen)
        self.file_button = ttk.Button(master, text="Select File",
                                      command=openFileCommand).grid(row=10, column=0, sticky=W, pady=10)

        runCommand = master.register(self.getInput)
        exitCommand = master.register(self.getCancel)

        self.run_button = ttk.Button(master, text="Run",
                                     command=runCommand).grid(row=20, column=0, sticky=W)

        self.cancel_button = ttk.Button(master, text="Cancel",
                                        command=exitCommand).grid(row=20, column=1, sticky=W)

    def FileOpen(self):
        self.File = filedialog.askopenfilename(title="Open the file",
                                               filetypes=(("Files", "*.txt"), ("All Files", "*")))
        self.file_only = self.File.split('/')[-1]

    def getInput(self):
        if self.File is None:
            self.warning_window = tk.showerror('Error', 'Please select a file to use.')
        else:
            self.close_box_window = tk.messagebox.askokcancel('Running', "Running the program, default = 'ok'")
            if self.close_box_window == True:
                root.destroy()
            else:
                return

    def getCancel(self):

        self.MsgBox_window = tk.messagebox.askokcancel("Exit", "Are you sure you want to exit?", icon="warning",
                                                       default='cancel')
        if self.MsgBox_window == False:
            return
        else:
            root.destroy()


root = Tk()
root.geometry('800x500')
gui = GetInfo(root)

root.mainloop()

File = gui.file_only

我一直收到一个错误:

AttributeError: 'GetInfo' object has no attribute 'File'

我以前就有这个功能(没有对self.File代码进行if/else检查)。我不知道怎样才能得到我想要的。任何帮助都将不胜感激。

看起来很简单,您需要初始化
文件

class GetInfo:
    def __init__(self, master):
        self.File = None
另外,为了避免一些粗心的错误,您可以使用if语句来判断函数
FileOpen()
中的文件名:


唯一的解释是,您正在调用使用该属性的代码,然后再设置该属性。那么,是否没有办法执行我想要执行的操作?对不起,这对Tkinter来说是个新问题,这可能是个简单的问题。在你的代码中,我找不到
GenBank\u文件
。它在哪里?@Unionicola:我不知道你的意思。当然,有一种方法可以做你想做的事。在尝试使用它之前,您只需确保您仅定义了
file\u
。顺便说一下,您发布的代码没有给出您所说的错误。@BryanOakley谢谢。我正在努力更好地理解类和Tkinter,但我将尝试定义变量。
    def FileOpen(self):
        filename = filedialog.askopenfilename(title="Open the file",
                                               filetypes=(("Files", "*.txt"), ("All Files", "*")))
        if filename: # when cancel, it will be ""
            self.File = filename
            self.file_only = self.File.split('/')[-1]