#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Using a variable in a entry problem.


    Newbie question, I am trying to use a entry to append the dbf part of a shapefile. I am using python 3.3. For some reason the file saves before the entry runs. I have been trying to fix this but so far have been unsuccessful. I figure there is something basic to python I have been missing.


    Code:
        import shapefile
        from tkinter import filedialog 
        import tkinter as tk
        class application:
            def __init__(self,window):
                """ Initalize the Application """
                self.myentrybox = tk.Entry(window)
                self.myentrybox.pack()
                self.myentrybox.insert(0,"some default value")
                self.myentrybox.bind("<Return>",self.Enter)
    
            def Enter(self,event):
               """ Someone Pressed Enter """
               print  (self.myentrybox.get())
        aep = 'self.myentrybox.get()'
        root=tk.Tk()
        myapp = application(root)
        r = shapefile.Reader('Mississippi')
        w = shapefile.Writer()
        w.fields = list(r.fields)
        w.field (aep, 'C', '40')
        i=1
    
        for rec in r.records():
         rec.append(i)
         i+=1
         w.records.append(rec)
        w._shapes.extend(r.shapes())
        w.save('AMissl6KM')
        root.mainloop()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    475
    Rep Power
    33
    The Tkinter portion of the program does not run until all other statements are processed because the root.mainloop() statement is last. You possibly want to put the file write in the Enter() function if you want to wait until Return is pressed to write to the file (note that the Python Style Guide says that function names are lower case only).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Thanks, now I understand what I did wrong. I will spend some time reading the python style sheet. Here is the corrected code, it works now.


    Code:
     
    import shapefile
    from tkinter import filedialog 
    import tkinter as tk
    root=tk.Tk()
     
    class application:
        def __init__(self,window):
            """ Initalize the Application """
            self.myentrybox = tk.Entry(window)
            self.myentrybox.pack()
            self.myentrybox.insert(0,"some default value")
            self.myentrybox.bind("<Return>",self.enter)
    
        def enter(self,event):
            """ Someone Pressed Enter """
            print  (self.myentrybox.get())
            aep = (self.myentrybox.get())
            r = shapefile.Reader('Mississippi')
            w = shapefile.Writer()
            w.fields = list(r.fields)
            w.field (aep, 'C', '40')
             
            i=1
            
            for rec in r.records():
             rec.append(i)
             i+=1
              
             w.records.append(rec)
             
             w._shapes.extend(r.shapes())   
             
            w.save('AMissl6KM')
            
    myapp = application(root)
     
    root.mainloop()

IMN logo majestic logo threadwatch logo seochat tools logo