2012-02-11 168 views
30

我正在为Python3中的程序首次编写浏览按钮。我一直在搜索互联网和这个网站,甚至是Python标准库。filedialog,tkinter和打开文件

我已经找到示例代码和很肤浅的事物解释,但是我一直没能找到任何解决我直接遇到的问题的东西,或者足够好的解释,所以我可以根据自己的需要定制代码。

下面是相关片段:

Button(self, text = "Browse", command = self.load_file, width = 10)\ 
     .grid(row = 1, column = 0, sticky = W) ..... 


def load_file(self): 

    filename = filedialog.askopenfilename(filetypes = (("Template files", "*.tplate") 
                 ,("HTML files", "*.html;*.htm") 
                 ,("All files", "*.*"))) 
    if filename: 
     try: 
      self.settings["template"].set(filename) 
     except: 
      messagebox.showerror("Open Source File", "Failed to read file \n'%s'"%filename) 
      return 

的方法是一些代码,我连同我自己的自定义的方式找到了一个混合体。看起来我终于有了工作(有点儿),尽管它不完全是我需要的。

当我激活“浏览”按钮时出现此错误:NameError: global name 'filedialog' is not defined

我发现一路上遇到了相当类似的问题,但是我提到的所有解决方案都提到了。我进入IDLE的'filedialog'帮助部分,但没有从那里收集任何东西。

会有人介意提供一个细分和一点点的指导;我的书中没有一本具体解决它,而且我检查了提供给其他人的所有解决方案 - 我迷路了。

+3

你输入了吗? '从tkinter import filed filedogog' – 2012-02-11 11:50:43

回答

50

你得到的异常告诉你filedialog是不是在你的命名空间。 filedialog(和BTW messagebox)是一个Tkinter的模块,所以它不与from tkinter import *

>>> from tkinter import * 
>>> filedialog 
Traceback (most recent call last): 
    File "<interactive input>", line 1, in <module> 
NameError: name 'filedialog' is not defined 
>>> 

你应该使用,例如只需输入:

>>> from tkinter import filedialog 
>>> filedialog 
<module 'tkinter.filedialog' from 'C:\Python32\lib\tkinter\filedialog.py'> 
>>> 

>>> import tkinter.filedialog as fdialog 

>>> from tkinter.filedialog import askopenfilename 

因此,这会为你的浏览按钮做:

from tkinter import * 
from tkinter.filedialog import askopenfilename 
from tkinter.messagebox import showerror 

class MyFrame(Frame): 
    def __init__(self): 
     Frame.__init__(self) 
     self.master.title("Example") 
     self.master.rowconfigure(5, weight=1) 
     self.master.columnconfigure(5, weight=1) 
     self.grid(sticky=W+E+N+S) 

     self.button = Button(self, text="Browse", command=self.load_file, width=10) 
     self.button.grid(row=1, column=0, sticky=W) 

    def load_file(self): 
     fname = askopenfilename(filetypes=(("Template files", "*.tplate"), 
              ("HTML files", "*.html;*.htm"), 
              ("All files", "*.*"))) 
     if fname: 
      try: 
       print("""here it comes: self.settings["template"].set(fname)""") 
      except:      # <- naked except is a bad idea 
       showerror("Open Source File", "Failed to read file\n'%s'" % fname) 
      return 


if __name__ == "__main__": 
    MyFrame().mainloop() 

enter image description here

+2

谢谢。你知道,我从tkinter中导入了它们(只是没有完全正确),而且因为我没有完全正确地完成它,所以我认定我的错误在某个地方,我没有犯任何错误。我的问题是:我认为'从tkinter import *'导入所有的tkinter。那么为什么这些必须单独导入?你能指点我一些文件吗?再次感谢 – Icsilk 2012-02-12 00:09:39

+0

我没有找到任何与简单的链接,以点解释。也许你有更多的运气。检查第一个Python [参考](http:// docs。python.org/reference/simple_stmts.html#the-import-statement)和[docs](http://docs.python.org/tutorial/modules.html#packages) – joaquin 2012-02-12 08:20:07

+0

这个解决方案的开头句子告诉你为什么你需要这两个陈述。 filedialog是一个模块,所以它不会使用“from tkinter import *”导入,而必须单独导入。 “from tkinter import filedialog” – RufusVS 2017-06-30 02:56:53

3

您是否尝试将自身前缀添加到fileName并替换Button上方的方法?随着自我,它在方法之间变得可见。

... 

def load_file(self): 
    self.fileName = filedialog.askopenfilename(filetypes = (("Template files", "*.tplate") 
                ,("HTML files", "*.html;*.htm") 
                ,("All files", "*.*"))) 
... 
0

我必须先指定单个命令,然后用*把所有的命令。

from tkinter import filedialog 
from tkinter import * 
+0


”from tkinter import *“ – James 2015-09-17 12:56:08