0

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)

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
evamvid
  • 831
  • 6
  • 18
  • 40
  • y is being used incorrectly, I edited the answer below with correct usage. Result of askopenfilename() is a string not a Tkinter object. – Daniel Jul 01 '14 at 14:03

3 Answers3

6

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

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()
user3666197
  • 1
  • 6
  • 50
  • 92
Ruben Bermudez
  • 2,293
  • 14
  • 22
  • Thanks! How can I use the output of the filebox? Will the path it outputs be set equal to `y`? Thanks! – evamvid Mar 06 '14 at 03:17
  • yes, then you can open it with `open(...)` see http://tkinter.unpythonic.net/wiki/tkFileDialog – Ruben Bermudez Mar 06 '14 at 03:21
  • so in this case it would be `open(y)`? – evamvid Mar 06 '14 at 03:26
  • And what if I don't want to open it? In this case, I want to set the full path that would be returned equal to a variable that I can use in a different part of the script. (I've already written the other part of the script) – evamvid Mar 06 '14 at 03:29
  • `open(...)` have two (three, but the third one is not necessary) parameters: `open(filename, openmode)`. `openmode ` indicates if you are opening the file for reading, writing... http://docs.python.org/2/library/functions.html#open – Ruben Bermudez Mar 06 '14 at 03:29
  • you don't need to open it, the path is saved in `y` s you can use it wherever you want. – Ruben Bermudez Mar 06 '14 at 03:30
  • When I tried to use `print (y)`, it gave the error ``NameError: global name `y` is not defined``. – evamvid Mar 06 '14 at 03:35
  • Meh. Never Mind. Variable Scope; I had used y in a function that popped up the filebrowser when a button was pressed. I then went and tried to print `y` in a different function. =) – evamvid Mar 06 '14 at 03:41
  • I just corrected the usage of askopenfilename() result - but it's a community edit, I didn't realize I wasn't logged in. – Daniel Jul 01 '14 at 14:05
1

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()
PR1
  • 47
  • 1
  • 8
Sergio
  • 21
  • 1
0

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()