# Return variable using tktinter

**URL:** <https://discuss.python.org/t/return-variable-using-tktinter/14596>\
**Category:** Python Help\
**Tags:** help\
**Created:** [March 25, 2022, 6:24pm UTC](https://discuss.python.org/t/return-variable-using-tktinter/14596 "2022-03-25T18:24:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goose](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/goose/32/6210_2.png) [@Goose](https://discuss.python.org/u/Goose)\
**Post date:** [March 25, 2022, 6:24pm UTC](https://discuss.python.org/t/return-variable-using-tktinter/14596/1 "2022-03-25T18:24:18Z")

</div>

im using tkinter to ask you a question and then return the answer on the click of a button  
the gui is inside a funtion to i can call the gui with whatever question i want, but im struggling to return the answer in the `command=" "` in tkinter

is there an easy way to do this in one line

example:

```auto
Tk.Label(tk, text=questions[current_question]).place(x=0, y=0)

answer_entry = Tk.Entry(tk)
answer_entry.place(x=0, y=20)

submit = Tk.Button(tk, text="Submit", command=return answer_entry.get())

```

Full code:

```auto
import tkinter as Tk

current_question = 0
questions = ["What is the capital of France? ", "What is the capital of Germany? ", "What is the capital of Italy? "]
answers = ["Paris", "Berlin", "Rome"]

def ask_question(current_question):
	#defining stuff
	tk = Tk.Tk()
	tk.title("Question")
	tk.geometry("300x200")

	#main GUI
	Tk.Label(tk, text=questions[current_question]).place(x=0, y=0)
	
	answer_entry = Tk.Entry(tk)
	answer_entry.place(x=0, y=20)

	submit = Tk.Button(tk, text="Submit", command="")
	tk.mainloop()

ask_question(0)

```

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 25, 2022, 9:29pm UTC](https://discuss.python.org/t/return-variable-using-tktinter/14596/2 "2022-03-25T21:29:18Z")

</div>

Why does it have to be in one line? Are you playing code-golf (shortest code with fewest lines wins), or maybe the Enter key on your keyboard is broken?

You could study the source code for the tkinter.simpledialog functions to see how they do it:

[https://docs.python.org/3/library/dialog.html](https://docs.python.org/3/library/dialog.html)

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [March 25, 2022, 11:54pm UTC](https://discuss.python.org/t/return-variable-using-tktinter/14596/3 "2022-03-25T23:54:38Z")

</div>

By Steven D’Aprano via Discussions on [Python.org](http://Python.org) at 25Mar2022 21:40:

> Why does it have to be in one line? Are you playing code-golf (shortest  
> code with fewest lines wins), or maybe the Enter key on your keyboard  
> is broken?

I expect that what the OP actually wants is a convenent was to use a  
TkInter dialogue to read a value in the same way normal procedural code  
would use `input()` to read from a terminal i.e. conveniently.

```
value = some_tk_dialogue_thing("prompt string")

```

The whole make-a-GUI then run-a-mainloop thing gets in the way of that  
formulation.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [March 26, 2022, 12:49am UTC](https://discuss.python.org/t/return-variable-using-tktinter/14596/4 "2022-03-26T00:49:02Z")

</div>

If you put the “make-a-gui then run-a-mainloop” thing inside a function, then it doesn’t matter whether the solution is one line or a million lines, you can just call the function to get a result:

```
from tkinter.simpledialog import askstring
askstring("Open the secret door", "Say 'Friend' and enter.")

```

Functions are good like that 🙂
