Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Unable to return value from callback function of a button in Python Tkinter
#1
Hi Team

I am trying to get the input written in an Entry box using Tkinter, But whenever I am trying to do so I am not getting the desired result..Instead a None is getting returned. Please help My code is given below
from Tkinter import *

class menu:
    def __init__(self,master):
        self.master=master
        self.var1 = StringVar()
        self.entry = Entry(self.master,textvariable=self.var1)
        self.entry.pack(side=TOP)
        self.var2 = StringVar()
        self.value=self.get_value()
        self.button = Button(self.master,text='Hit Me',command = self.get_value)
        self.button.pack(side=TOP)

    def get_value(self):
         self.value= self.var1.get()

if __name__=="__main__":
    root = Tk()
    Menu = menu(root)
    print Menu.value
    root.mainloop()
Output:
None
in this while printing Menu.value I am getting None instead off getting what I entered into Entry.Please help.Thanks in advance.
Quote
#2
1. Don't flood namespace.
from Tkinter import *
this is better.
import Tkinter as tk
Example. Every thing seems to be working find.
import Tkinter as tk

class Menu:
    def __init__(self,master):
        self.master=master
        self.var1 = tk.StringVar()
        self.entry = tk.Entry(self.master, textvariable=self.var1)
        self.entry.pack(side=tk.TOP)

        self.button = tk.Button(self.master,text='Hit Me',command = self.get_value)
        self.button.pack(side=tk.TOP)
        self.value = None

    def get_value(self):
        print(self.entry.get())
        print(self.var1.get())
        self.value = self.entry.get()
        print(self.value)

if __name__=="__main__":
    root = tk.Tk()
    menu = Menu(root)
    root.mainloop()
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote
#3
(Aug-05-2018, 12:58 PM)Windspar Wrote: 1. Don't flood namespace.
from Tkinter import *
this is better.
import Tkinter as tk
Example. Every thing seems to be working find.
import Tkinter as tk

class Menu:
    def __init__(self,master):
        self.master=master
        self.var1 = tk.StringVar()
        self.entry = tk.Entry(self.master, textvariable=self.var1)
        self.entry.pack(side=tk.TOP)

        self.button = tk.Button(self.master,text='Hit Me',command = self.get_value)
        self.button.pack(side=tk.TOP)
        self.value = None

    def get_value(self):
        print(self.entry.get())
        print(self.var1.get())
        self.value = self.entry.get()
        print(self.value)

if __name__=="__main__":
    root = tk.Tk()
    menu = Menu(root)
    root.mainloop()

Hi Windspar,
Thanks for your input.But what I want is to return the input value to a variable in the main program so that I can use t for other purpose. Actually my idea is to make one FTP GUI app so that user will provide input in GUI app & after the pressing Hit Me button the main program will be able to fetch the input details of user from this GUI. In this program if I am trying to return a value the output is coming as None. So, please tell me how I can use this input in entry box to another program? Again thank you very much for your input.
Quote
#4
You never going to be able to print anything in the main area.
Program must run first. Then when program ends. All widgets are destroyed. All this happens when mainloop is called.

Example
import Tkinter as tk

class Main:
    root = tk.Tk()
    value = "None"

class Menu:
    def __init__(self, master):
        self.master = master
        self.entry = tk.Entry(self.master)
        self.entry.pack(side=tk.TOP)
        self.button = tk.Button(master,text='Hit Me',command = self.get_value)
        self.button.pack(side=tk.TOP)
        self.button_send = tk.Button(master, text="Get Text", command=self.get_text)
        self.button_send.pack()
        self.label = tk.Label(master, text=Main.value)
        self.label.pack()

    def get_value(self):
        Main.value = self.entry.get()

    def get_text(self):
        #self.label['text'] = self.entry.get()
        self.label['text'] = Main.value


if __name__=="__main__":
    Main.menu = Menu(Main.root)
    Main.root.mainloop()
99 percent of computer problems exists between chair and keyboard.
hobby-programmer.com
Quote
#5
It has to go after the mainloop(), otherwise you print it before tkinter runs, i.e
    root.mainloop()
    print Menu.value 
Note that classes use UpperCase and variables use lower_case_with_underscores. You should also state that you are using Python2.X, which is the exception, as Python3 is what most people are using (Python2 reaches end of life in 2020).
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Problem with Submit button Tkinter Reldaing 2 130 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  Unable fetch fucntion data in class in tkinter jenkins43 2 229 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Unable to put background image on Tkinter Frame jenkins43 2 494 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  tkinter button not accessing the command when clicked jhf2 1 189 Nov-23-2019, 10:17 PM
Last Post: DT2000
  Tkinter button in for Simpsunt 3 157 Nov-23-2019, 01:41 AM
Last Post: Larz60+
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 335 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 310 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  Unable to update or refresh label text in tkinter jenkins43 3 943 Jul-24-2019, 02:09 PM
Last Post: Friend
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 515 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  GUI freezes while executing a callback funtion when a button is pressed abi17124 5 865 Jul-10-2019, 12:48 AM
Last Post: FullOfHelp

Forum Jump:


Users browsing this thread: 1 Guest(s)