title: Can a tkinter button return a value from an entry on-click?
body: <p>I'm doing an extended project as one of my qualifications in my current College and I chose to write a python Strategy/RPG game. As a result, I ended up with the highest level of Python knowledge (Surpassing my Computing Teacher who only ever uses the basics... and used Tkinter only once a few years ago. Every one else who has decided to make a program, are either coding in Lua, Java, C++, HTML/CSS/Java-Script or, those who are coding in python, they are only using the basics learned from our teacher.)
  I say "Highest level of Python knowledge" but really it isn't that high... I only know a little beyond the basics.
  As a result, a forum post is the best place I can turn to for help.</p>
  
  <p>So in my game I defined this function:</p>
  
  <pre><code>#"Given_String" is the question that one would want to ask. (With the answer being an integer between 1 and "Choice_Range" (inclusive)
  def Value_Error(Given_String,Error_Message,Choice_Range):
      while True:
          try:
              Temp=int(input(Given_String))
              if Temp&lt;1 or Temp&gt;Choice_Range:
                  print(Error_Message)
              else:
                  break
          except ValueError:
              print(Error_Message)
      return Temp
  </code></pre>
  
  <p>I then wanted to add tkinter to my code, because the game would have to be in a separate window, and not in the console. As a result, I had to change this code so that it displays the "Given_Message" and the "Error_Message" in a tkinter window, and uses the value that has been typed into an entry box when defining "Temp".</p>
  
  <p>I wrote this code to make this work: (Or at least most of it)</p>
  
  <pre><code>#This code is stored in a different file for neatness and hence I had to import "sys" to avoid circular imports.
  #This code is made to be flexible so that I can later re-use it when necessary.
  #This code starts with the function all the way at the bottom. The rest are made to add flexibility and to structure the algorithm.
  #This code hasn't been fully run (Because of the Error crashing the Python Shell) so it can contain other Run-time Errors that I'm not aware of yet.
  
  import sys
  def Generate_Window(Window_Name,X_Parameter=5,Y_Parameter=50):
      Temp=sys.modules['tkinter'].Tk()
      Temp.title(Window_Name)
      Temp.geometry(str(X_Parameter)+"x"+str(Y_Parameter))
      return Temp
  
  def Generate_Button(Master,Text="Submit"):
      Temp=sys.modules["tkinter"].Button(Master,text=Text)
      return Temp
  
  def Generate_Entry(Master):
      Temp=sys.modules["tkinter"].Entry(Master)
      return Temp
  
  def Generate_Label(Master,Given_String):
      Temp=sys.modules["tkinter"].Label(Master,text=Given_String)
      return Temp
  
  def Com_Get_Entry(Given_Window,Given_Entry):
      Temp=Given_Entry.get()
      Given_Window.destroy()
      return Temp
  
  def Com_Confirm(Given_Window):
      Given_Window.destroy()
  
  def Generate_Entry_Box(Given_String):
      Entry_Window=Generate_Window("Entry",X_Parameter=300)
      Entry_Label=Generate_Label(Entry_Window,Given_String)
      Entry_Entry=Generate_Entry(Entry_Window)
      Entry_Button=Generate_Button(Entry_Window)
      Entry_Button.configure(command=lambda:Com_Get_Entry(Entry_Window,Entry_Entry))
      Entry_Label.grid(row=0,columnspan=2)
      Entry_Entry.grid(row=1,column=0)
      Entry_Button.grid(row=1,column=1)
  
  def Generate_Alert_Message(Given_String):
      Alert_Window=Generate_Window("Alert",X_Parameter=300)
      Alert_Label=Generate_Label(Alert_Window,Given_String)
      Alert_Button=Generate_Button(Alert_Window,Text="OK")
      Alert_Button.configure(command=lambda:Com_Confirm(Alert_Window))
      Alert_Label.grid(row=0,columnspan=2)
      Alert_Button.grid(row=1,column=1)
  
  def Get_Interger_Input_In_Range(Given_String,Error_Message,Choice_Range):
      while True:
          try:
              Returned_Value=int(Generate_Entry_Box(Given_String))
              if Returned_Value&lt;1 or Returned_Value&gt;Choice_Range:
                  Generate_Alert_Message(Error_Message)
              else:
                  break
          except ValueError:
              Generate_Alert_Message(Error_Message)
      return Temp
  </code></pre>
  
  <p>I already included in my code all that I was struggling with and that I could find an answer to.
  I.E: On-click, do a certain action with given parameters.
  One thing I could not find, is how to return the entered value to the original (Get_Interger_Input_In_Range()) function after the button has been clicked.
  What I mean is something like this:</p>
  
  <pre><code>def Function1(GivenParameter1,GivenParameter2):
      Temp=Function2(GivenParameter1)
      Temp+=GiverParameter2   #random action
      return Temp
  
  def Function2(GivenParameter):
      Button=Button(Master,command=Function3).grid()
      Entry=Entry(Master).grid()
  
  def Function3():
      Temp=Entry.get()
      return Temp
  </code></pre>
  
  <p>In Function1 I want Temp to equal the entered value from Function2.
  Is there any way to do this without using classes? (I'm not too familiar with classes yet)
  Is there any way to do this at all?
  I haven't seen anyone give the answer I was looking for...
  Because even if they said to use classes... I still didn't know how to return it (Explanation just below)</p>
  
  <pre><code>#The following code was written quickly for purposes of explaining what I mean. It doesn't actually work... (It seems that the button command is being called automatically...)
  
  from tkinter import *
  class Return_Value_In_Entry():
      def __init__(self):
          self.Master=Tk()
          self.Entry=Entry(self.Master)
          self.Button=Button(self.Master,text="Submit",command=self.Return())
      def Return(self):
          self.TempVar=self.Entry.get()
          return self.TempVar
  </code></pre>
  
  <p>The way I see it, the Return() function would return the value to the button and not the function/assignment that called the class ... Which is the same problem I'm having with my code.</p>
  
  <p>If you read this all then I really appreciate it. I hope someone can answer my question and tell me (if it's impossible otherwise) how to use classes to solve my "Little" yet large problem.</p>