0

I'm trying to wrap my head around this problem.

Say I have a code like this:

def get_input(data_A, data_B):
    all_data = [data_A.get(),dataB.get()]
    return(all_data)

def the_gui():
    root = Tk()
    data_A = Entry(root)
    data_B = Entry(root)
    button = Button(root, text='Submit', command=lambda: get_input(data_A, data_B))
    mainloop()

My goal is to get the value of data_A and data_B once I clicked the submit button. I tried to use global variable and everything, but I kept failing to catch the value.

The only thing that works is when I put the whole get_input() function inside the_gui() function. However, I don't think that's a good practice to implement.

Any suggestions?

4
  • The only issue of your code is missed calling layout function: grid() or pack() on the widgets. Commented Feb 7, 2020 at 3:25
  • @acw1668 thanks for the reply but I intentionally removed that part so that I could point out the issue. If you said that nothing is wrong, then how do I catch the value of all_data? I want to parse and play around with it. Commented Feb 7, 2020 at 3:35
  • I do not know why using global variables does not work in your case. You need to post the code using global variables and see what is the issues. Commented Feb 7, 2020 at 3:39
  • 1
    You cannot return a value from button command. Can't you access the values by data_A.get() directly? why do you need to "catch" it? Commented Feb 7, 2020 at 11:14

1 Answer 1

1

Here is a simple example of how you could write this to get the results you are looking for.

  1. When using global is that all your root window and related fields are in a function. So you would have to define global in both function and this is not what you want to do.

  2. Typically you will want to write the root window in the global namespace and not in a function or write it into a class so you can avoid global's all-together.

  3. button = Button(...) may not be doing what you think it is. This does not return a value from the command once clicked. Tkinter buttons do not care about anything being returned. So you have to record that value elsewhere.

  4. I am not sure how you code is working as you do not use geometry managers and mainloop() should be attached to the root window so I have added those in as well.

Example 1:

import tkinter as tk


def get_input():
    global a_and_b
    a_and_b = [data_a.get(), data_b.get()]
    # If you want to keep a running record of all values submitted
    # then you can do this instead:
    # a_and_b.append([data_a.get(), data_b.get()])

def print_a_b():
    print(a_and_b)


root = tk.Tk()
a_and_b = []

data_a = tk.Entry(root)
data_b = tk.Entry(root)
data_a.pack()
data_b.pack()

tk.Button(root, text='Submit', command=get_input).pack()
tk.Button(root, text='Print A/B List', command=print_a_b).pack()
root.mainloop()

Example 2 using OOP:

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.a_and_b = []
        self.data_a = tk.Entry(self)
        self.data_b = tk.Entry(self)
        self.data_a.pack()
        self.data_b.pack()

        tk.Button(self, text='Submit', command=self.get_input).pack()
        tk.Button(self, text='Print A/B List', command=self.print_a_b).pack()

    def get_input(self):
        self.a_and_b = [self.data_a.get(), self.data_b.get()]

    def print_a_b(self):
        print(self.a_and_b)


if __name__ == '__main__':
    App().mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your explanation. Your example just gave me an idea to find the fix for my issue!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.