Python NameError: 名称 'tkFileDialog' 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22213600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:29:35  来源:igfitidea点击:

NameError: name 'tkFileDialog' is not defined

pythonpython-2.7tkinterfile-browser

提问by evamvid

I'm trying to use Tkinter and get the user to choose a certain file. My code looks like this (I'm just starting out with Tkinter)

我正在尝试使用 Tkinter 并让用户选择某个文件。我的代码看起来像这样(我刚开始使用 Tkinter)

from Tkinter import *
from tkFileDialog import *


root = Tk()

root.wm_title("Pages to PDF")
root.wm_iconbitmap('icon.ico')
w = Label(root, text="Please choose a .pages file to convert.") 
y = tkFileDialog.askopenfilename(parent=root)
y.pack()
w.pack()

root.mainloop()

When I run the program, I get an error that says:

当我运行程序时,我收到一条错误消息:

NameError: name 'tkFileDialog' is not defined

I've tried it with a few configurations I found online. None of them have worked; but this is the same basic error every time. How can I fix this?

我已经用我在网上找到的一些配置进行了尝试。他们都没有工作;但每次都是相同的基本错误。我怎样才能解决这个问题?

采纳答案by Ruben Bermudez

You are importing everything from tkFileDialogmodule, so you don't need to write a module-name prefixed tkFileDialog.askopenfilename(), just askopenfilename(), like:

您正在从tkFileDialog模块导入所有内容,因此您无需编写以 为前缀的模块名称tkFileDialog.askopenfilename(),只需askopenfilename(),例如:

from Tkinter import *
from tkFileDialog import *
root = Tk()
root.wm_title("Pages to PDF")

w = Label(root, text="Please choose a .pages file to convert.") 
fileName = askopenfilename(parent=root)

w.pack()
root.mainloop()

回答by Sergio

Try this:

尝试这个:

from Tkinter import *

import tkFileDialog

root = Tk()
root.wm_title("Pages to PDF")
root.wm_iconbitmap('icon.ico')
w = Label(root, text="Please choose a .pages file to convert.") 
y = tkFileDialog.askopenfilename(parent=root)
y.pack()
w.pack()
root.mainloop()

回答by Guenther Leyen

Seems a space name problem. Try this:

似乎是空间名称问题。尝试这个:

try:
    import Tkinter as tk
    import tkFileDialog as fd
except:
    import tkinter as tk
    from tkinter import filedialog as fd

def NewFile():
    print("New File!")
def OpenFile():
    name = fd.askopenfilename()
    print(name)
def About():
    print("This is a simple example of a menu")

class myGUI:
    def __init__(self, root):
        self.root = root

        self.canvas = tk.Canvas(self.root,
                                borderwidth=1,
                                relief="sunken")
        self.canvas.pack( fill=tk.BOTH, expand=tk.YES)

        self.menu = tk.Menu(self.root)
        self.root.config(menu=self.menu)
        self.helpmenu = tk.Menu(self.menu)

        self.filemenu = tk.Menu( self.menu )
        self.menu.add_cascade(label="File", menu=self.filemenu)
        self.filemenu.add_command(label="New", command=NewFile)
        self.filemenu.add_command(label="Open...", command=OpenFile)
        self.filemenu.add_separator()
        self.filemenu.add_command(label="Exit", command=root.destroy)

root = tk.Tk()   
root.title('appName')
myGUI(root)
root.mainloop()