2012-02-11 8 views
30

मैं पाइथन 3 में किसी प्रोग्राम के लिए ब्राउज़ बटन को कोड करने पर पहली बार काम कर रहा हूं। मैं इंटरनेट और इस साइट, और यहां तक ​​कि पायथन मानक लाइब्रेरी खोज रहा हूं।दायर किया गया, 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

मुझे रास्ते में काफी समान समस्याएं मिली हैं लेकिन मैंने सुझाए गए सभी समाधानों को कवर किया है। मैं आईडीएलई के 'दायरियलॉग' सहायता अनुभाग में गया लेकिन वहां से कुछ भी नहीं मिला।

क्या कोई इस पर ब्रेक डाउन और थोड़ा मार्गदर्शन प्रदान करेगा; मेरी कोई भी पुस्तक विशेष रूप से इसे संबोधित नहीं करती है, और मैंने दूसरों को प्रदान किए गए सभी समाधानों की जांच की है - मैं खो गया हूं।

+3

क्या आपने इसे आयात किया है? 'tkinter आयात दाखिल से' –

उत्तर

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 import * से' tkinter के सभी आयात किया। तो इन्हें अलग से आयात क्यों किया जाना चाहिए? क्या आप मुझे इसके बारे में कुछ दस्तावेज की ओर इंगित कर सकते हैं? धन्यवाद फिर से – Icsilk

+0

मुझे बिंदु स्पष्टीकरण के लिए सरल, कोई लिंक नहीं मिला। शायद आपके पास और भाग्य है। पहले पायथन [संदर्भ] की जांच करें (http: // दस्तावेज़।python.org/reference/simple_stmts.html#the-import-statement) और [डॉक्स] (http://docs.python.org/tutorial/modules.html#packages) – joaquin

+0

इस समाधान की उद्घाटन वाक्य आपको बताती है कि आप क्यों दो बयान की जरूरत है। दायरियलॉग एक मॉड्यूल है, इसलिए इसे "tkinter import * से" आयात नहीं किया जाता है, और इसे अलग से आयात किया जाना चाहिए। – RufusVS

3

क्या आपने फ़ाइल नाम में स्वयं उपसर्ग जोड़ने और बटन के ऊपर विधि को बदलने का प्रयास किया था? स्वयं के साथ, यह विधियों के बीच दिखाई देता है।

... 

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

"टिंकर आयात दायर से"
"tkinter import * से" – James

संबंधित मुद्दे