[Tutor] passing values to a callback

Brett Wunderlich brettlea at mac.com
Sat Jul 13 17:08:37 CEST 2013


Greetings pythonistas,

I have been exploring the use of Tkinter trying to write a fairly simple program which will accept data from a GUI with 3 Entry fields that is commanded by a Button. Once the button is pressed the values in the fields should result in a calculation - the values should all be numeric (I'll work on catching errors later) but I can not figure out how to pass those values to the callback function for the button. Here is my code so far:

from Tkinter import *

class App:

    def __init__( self, master ):

        frame = Frame( master )
        
        frame.grid()
        
        entry1 = Entry( frame, bg = "white", width = 3 ).grid(row = 0, column = 0 )
        Label( frame, text = "X^2 +" ).grid( row = 0, column = 1 )
        entry2 = Entry( frame, bg = "white", width = 3 ).grid(row = 0, column = 2 )
        Label( frame, text = "X +" ).grid( row = 0, column = 3 )
        entry3 = Entry( frame, bg = "white", width = 3 ).grid(row = 0, column = 4 )
        Label( frame, text = "= 0" ).grid( row = 0, column = 5 )
        
        calcbutton = Button( text = "How many Real roots?", command = self.calculate )
        calcbutton.grid( row = 1, column = 0 )

    def calculate( self ):
    	print 1
        
root = Tk()

root.geometry( '250x100+450+70' )

app = App(root)

root.mainloop()

Now the above code runs, but I need to figure out how to get the contents of the fields entry1, entry2 and entry3 to the calculate method. Or maybe I am going about this all wrong and there is a better way to do it. Any help would be appreciated.

Brett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130713/16fb648d/attachment.html>


More information about the Tutor mailing list