Hello all,

I normally lurk around and solve most of my issues by reading other threads. I have run into an issue and I am trying to figure out what I am missing. I am rather new at this so thanks for any advice.

What I am attempting to due is call to a module from my main program to access a Open Dialog window. The issue seems to be within my module that looks similar to:

def GetFile():
      from Tkinter import Tk
      import tkFileDialog


       toplevel = Tk()
       toplevel.withdraw()


        filename = tkFileDialog.askopenfilename()

I am calling GetFile from the command of a button. When i click the button it throws a NamerError at toplevel = Tk() saying that global name Tkinter is not defined.

I have no formal training on this so I am hoping I am making sense.

I am guessing I have syntax messed up and thing not in the right place, but it this the best way to go about calling a dialog box from a module?

Recommended Answers

All 2 Replies

Reformulated program to function only without knowing specific use case:

import os
from Tkinter import Tk
import tkFileDialog

toplevel = Tk()
toplevel.withdraw()

filename = tkFileDialog.askopenfilename()

if os.path.isfile(filename):
    for line in open(filename,'r'):
        print line,
else: print 'No file chosen'

Thanks this worked perfect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.