Error in trying to open dialog box from a widget button in python
Question: I'm new to python. I'm trying to open a dialog box to get a value from within
a widget that does a list of other staff allready.

But getting errors and can't figure out what to do.

Here's my code:

    
    
    import Tkinter,Tkconstants,tkFileDialog
    from Tkinter import *
    import csv
    import numpy
    import math
    import numpy.random as nrnd
    import matplotlib.pyplot as plt
    import shutil
    import tkMessageBox
    global filesavepath
    class App:
        def __init__(self,master):
            self.mymaster=master
            frame=Frame(master)
            frame.pack()
            self.importbutton=Button(frame,text='Import Data',command=self.importdata)
            self.importbutton.pack()
            self.executebutton=Button(frame,text='Execute',command=self.popup)
            self.executebutton.pack()
            self.distribution_rep=Button(frame,text='Repeat Purchase Score Distribution',command=self.distrepbutton)
            self.distribution_rep.pack()
            self.distribution_churn=Button(frame,text='Churn Probability Distribution',command=self.distchurnbutton)
           self.distribution_churn.pack()
           self.exitbutton=Button(frame,text='Exit',command=self.exitapp)
           self.exitbutton.pack()
           self.file_opt=options={}
           options['defaultextension']=''
           options['filetypes']=[('allfiles','.*'),('textfiles','.txt')]
           options['initialdir']='C:\\'
           options['initialfile']='myfile.txt'
           options['parent']=root
           options['title']='Thisisatitle'
        def importdata(self):
            filename=tkFileDialog.askopenfilename(**self.file_opt)
            filesavepath="C:/input_full.csv"
            shutil.copy2(filename,filesavepath)
            if filename:
                return open(filename,'r')
    
        def popup(self):
            top = self.top = Tkinter.Toplevel(self)
            myLabel = Tkinter.Label(top, text='Enter your username below')
            myLabel.pack()
    
            self.myEntryBox = Tkinter.Entry(top)
            self.myEntryBox.pack()
    
            mySubmitButton = Tkinter.Button(top, text='Done', command=self.execbutton)
            mySubmitButton.pack()
        def execbutton(self):
            if self.myEntryBox.get() != "":
                self.timevalue = self.myEntryBox.get()
                self.top.destroy()
            execfile("Repeat Purchase Algo in python v6")
            tkMessageBox.showinfo("Job Done", "Probability Computation completed")       
        def send(self):
            global timevalue
            timevalue=self.myEntryBox.get()
            self.top.destroy()
        def distrepbutton(self):
            plt.hist(prob,bins=10,normed=TRUE)
            plt.xlabel('Probability')
            plt.title('Histogram of Repeat Purchase Probability')
            plt.show()
        def distchurnbutton(self):
            plt.hist(churn_prob,bins=10,normed=TRUE)
            plt.ylabel('Probability')
            plt.title('Histogram of Churn Probability')
            plt.show()
        def exitapp(self):
            self.mymaster.destroy()
    
    root=Tk()
    root.title('Repeat Puchase Widget')
    app=App(root)
    root.mainloop()
    

So as may be apparent to you, I'm importing dataset with an Import button,
executing some analysis in another code through a button called Execute, and
then showing some graphs.

What I wanted was to open a pop up kind of window on click of "Execute" button
that will input a value. But I'm getting the following error:

    
    
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
        return self.func(*args)
      File "C:/Python27/widget_repeat_purchase_v4", line 42, in popup
        top = self.top = Tkinter.Toplevel(self)
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 2017, in __init__
        BaseWidget.__init__(self, master, 'toplevel', cnf, {}, extra)
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
        BaseWidget._setup(self, master, cnf)
      File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
        self.tk = master.tk
    AttributeError: App instance has no attribute 'tk'
    

I've no idea what to do. Please help.


Answer: When you create the toplevel widget, you are passing `self` as the first
argument. Tkinter requires that this be a parent widget. However, in your code
`self` does not represent a widget.

In your specific case you want to pass in `self.mymaster` rather than `self`:

    
    
    top = self.top = Tkinter.Toplevel(self.mymaster)
    

