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
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)
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:
By Steven D’Aprano via Discussions on 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.
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.")