return from the GUI?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtorppa
    New Member
    • Jun 2009
    • 3

    return from the GUI?

    I'm a very beginner in using Tkinter, so this is fortunately an easy question to most: how do I return values from GUI created with Tkinter?
    I have lots of input data given in the GUI, but I don't know how I can close the root window and return the given parameters to the main Python code to go on with the routine.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    I use a class. You could also have the GUI run the second progam and pass the variables to the program/function. This is an ugly example that was sitting on my computer.
    Code:
    import Tkinter
    
    class EntryTest:
       """ shows using the same StringVar in the second list box
           and in the entry box
       """
       def __init__(self):
          self.top = Tkinter.Tk()
          self.top.title("Test of Entry")
          self.top.geometry("200x125+10+10")
    
          self.str_1 = Tkinter.StringVar()
          label_lit = Tkinter.StringVar()
    
          label_1 = Tkinter.Label(self.top, textvariable = label_lit )
          label_1.pack()
          label_lit.set( "Test of Label")
    
          label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )
          label_2.pack()
    
          entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
          entry_1.pack()
          self.str_1.set( "Entry Initial Value" )
    
          ##---  Delete the contents of an entry widget
          ## entry.delete(0,END)
    
          print_button = Tkinter.Button(self.top, text='PRINT CONTENTS',
                         command=self.getit, bg='blue', fg='white' )
          print_button.pack(fill=Tkinter.X, expand=1)
    
          exit_button= Tkinter.Button(self.top, text='EXIT',
                       command=self.top.quit, bg='red', fg='white' )
          exit_button.pack(fill=Tkinter.X, expand=1)
    
          entry_1.focus_set()
          self.top.mainloop()
    
       ##-----------------------------------------------------------------
       def getit(self) :
          print "getit: variable passed =", self.str_1.get()
    
    
    ##===============================================================
    if "__main__" == __name__  :
       ET=EntryTest()
       print "\n Under __main__ =", ET.str_1.get()

    Comment

    Working...