2012-02-11 232 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