I am working with classes for the first time in Tkinter. I have no experience working with classes before this, although I have had experience with basic coding syntax before.

I am trying to make a function plotter.

If I have a class PlotApp. This is my the class that is used in

root = Tk()
app = PlotApp(root)

The class PlotApp has this

FunctionFrameInstance = FunctionFrame(self)

        ButtonFrameInstance = ButtonFrame(self)

The FunctionFrame class has a text Entry box.
The ButtonFrame has a "Add Function" button.

FunctionFrame has a text input box
ButtonFrame has a button

How can I do something basic like print what the user has entered in the inpux box when the button is clicked (just using the print command for IDLE).

P.S I have only just started learning Python and I have only just been introduced to Tkinter and classes, so my understanding is very lacking.

Edited 7 Years Ago by marshall31415: n/a

A simple example using ".get()". The print button calls the getit function when it is pushed. Note the use of "self" to create an object variable that belongs to that class object, so it can be accessed anywhere within the class as well as accessing it as a member of that class instance, in this case the __main__ print statement. Also note that label_2 and entry_1 both share the same StringVar. Two good references

import Tkinter

class EntryTest:
   def __init__(self):
      self.top = Tkinter.Tk()

      self.str_1 = Tkinter.StringVar()
      label_lit = Tkinter.StringVar()

      label_1 = Tkinter.Label(self.top, textvariable = label_lit )
      label_lit.set( "Test of Label")
      label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )

      entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
      self.str_1.set( "Entry Initial Value" )

      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)


   def getit(self) :
      print "getit: variable passed is", self.str_1.get()

if "__main__" == __name__  :
   print "under __main__ =", ET.str_1.get()

Edited 7 Years Ago by woooee: n/a

This article has been dead for over six months. Start a new discussion instead.