Tkinter entry getting text entered by user
Question: I am very new to Tkinter ( I find it very difficult to learn). I have a python
script working based on user input. I would like to wrap a GUI around it and
eventually put it on web. In any case for user input I would like to get this
from the GUI with a combination of Entry widgets and some buttons. First thing
is I was reading and some people mentioned to use a class so I have the
following. I have a few questions

  1. I would like to check to see if indeed the users entered a value before he hits the GO button. How do I do this?
  2. I would like the value entered to be made accessible by the rest of the program in the main body. How do I do this?

Thanks,

    
    
    from Tkinter import *
    
    class MainWindow():
    
        def get_fc(self):
            a = self.fc_gui.get()
            return a
    
        def __init__(self, master):
            self.master = master
            self.master.title('TEST')
    
            self.fc_gui = DoubleVar(self.master, value = 500.00)
    
            self.fclabel1 = Label(self.master, text = 'Please Enter a value', fg = 'black', bg = 'yellow')
            self.fclabel1.grid(row = 0, column = 0)
    
            self.fcedit1 = Entry(self.master, textvariable = self.fc_gui, bd = 5  )
            self.fcedit1.grid(row = 1, column = 0)
    
            fcbutton1 = Button(self.master, text='GO', command = self.get_fc)
            fcbutton1.grid(row = 1, column = 1)
    
    master = Tk()
    MainWindow(master)
    master.mainloop()
    


Answer:   1. It doesn't make sense to `return` to a `Button`. The `Button` can't do anything with the value. Instead, save the value as an instance variable.
  2. You don't have a `mainloop()`.
  3. You can't really check if the user entered a value _before_ they hit "Go" - at the start of the program, of course they haven't entered anything yet. If you needed to track the contents of this field, there are ways to do that, but it's not necessary for a simple validation. Just check the value when they hit the button.



    
    
    from Tkinter import *
    
    class MainWindow():
    
        def get_fc(self):
            a = self.fc_gui.get()
            if a: # this block will execute if a has content
                self.a = a # save it for future use
    
        def __init__(self, master):
            self.master = master
            self.master.title('TEST')
    
            self.fc_gui = DoubleVar(self.master, value = 500.00)
    
            self.fclabel1 = Label(self.master, text='Please Enter a value',
                                  fg = 'black', bg = 'yellow')
            self.fclabel1.grid(row = 0, column = 0)
    
            self.fcedit1 = Entry(self.master, textvariable = self.fc_gui, bd = 5  )
            self.fcedit1.grid(row = 1, column = 0)
    
            fcbutton1 = Button(self.master, text='GO', command = self.get_fc)
            fcbutton1.grid(row = 1, column = 1)
    
    master = Tk()
    MainWindow(master)
    master.mainloop() # don't forget mainloop()
    

