Home tkinter, button returns variable
Reply: 2

tkinter, button returns variable

Tragg Thorsgard
Tragg Thorsgard Published in 2017-11-15 02:33:54Z

I am trying to make a GUI text based adventure game in python. I want to be able to take text from a textinput box and store it as string variable.

I have 2 problems:

  1. Making the python wait for the submit button to be pressed, before processing the input and updating the game.
  2. Getting the text variable out of the command, I would like to not use global if possible.

Here is some of my code to better understand:

root = tk.Tk()
root.title("my game")
textbox = tk.StringVar()
textboxentry = tk.Entry(root, textvariable=textbox, bd=5, width = "40",  font=("times", 20))
textboxentry.pack(in_=bgImageLabel, side = "bottom")
def getInput():
    textboxInput = textbox.get() #gets entry
    lengthEntry = len(textbox.get())
    textboxentry.delete(0,lengthEntry) #removes entry from widget
    return textboxInput # I would like this return to work 

submit = tk.Button(root, text ="Submit", command = (textboxInput = getInput()))
##I want the command function to use command = getInput and store the return on getInput as textboxInput. This will update the wait_variable down below, and give the inputs(textboxInput) a string to work with.

submit.pack(in_=bgImageLabel, side = "bottom")

while game == True:
## I need it to wait before proceeding to this next line because i need the textboxInput from the entry widget.
    actionInput, extraInput, texts = inputs(textboxInput)

Currently I can't figure a way to use command = (textboxInput = getInput), using lambda or anything else. I just want to store the return which comes off of the Entry as a string variable that can be used by the main function. All help is appreciated!

Nae Reply to 2017-11-16 07:55:07Z

Below code processes entry widget's text when Submit button is pressed.

import tkinter as tk

root = tk.Tk()

aVarOutside = 'asd'
def btn_cmd(obj):

    #use global variable
    global aVarOutside
    #print its unmodified value
    print("aVarOutside: " + aVarOutside)
    #modify it with what's written in Entry widget
    aVarOutside = obj.get()

    #modify lblTextVar, which is essentially modifying Label's text as lblTextVar is its textvariable

    #print what's inside Entry
    print("Entry: " + obj.get())

txt = tk.Entry(root)

lblTextVar = tk.StringVar()
lbl = tk.Label(root, textvariable=lblTextVar)

btn = tk.Button(text="Submit", command=lambda obj = txt : btn_cmd(obj))


When the button is pressed:

  1. Value of a global variable, aVarOutside is printed.
  2. Value of aVarOutside is modified to the value of Entry box's (txt's) content.
  3. Value of a textvariable used by a label (lbl) is modified. Which means that the text of lbl is updated and can be seen on the GUI.
  4. Finally Entry box, txt's content is printed.
furas Reply to 2017-11-16 08:51:57Z

I think you should use inputs() inside getInputs() and then button doesn't have to return any variables - and then you can use root.mainloop() instead of while loop.

import tkinter as tk

# --- functions ---

def inputs(text):
    # do something with text 

    # and return something      
    return 'a', 'b', 'c'

def get_input():
    global action_input, extra_input, texts

    text = textbox.get()

    if text: # check if text is not empty
        textbox.set('') # remove text from entry
        #textbox_entry.delete(0, 'end') # remove text from entry
        action_input, extra_input, texts = inputs(text)

# --- main ---

root = tk.Tk()

textbox = tk.StringVar()

textbox_entry = tk.Entry(root, textvariable=textbox)

submit = tk.Button(root, text="Submit", command=get_input)


BTW: you could better organize code

  • all functions before main part (root = tk.Tk())
  • PEP8 suggests to use lower_case_names for functions and variables (instead of CamelCaseNames)

global is not prefered method but I think it is better solution than yours.

If you don't need global then you can use classes with self.

import tkinter as tk

# --- classes ---

class Game:

    def __init__(self):
        self.root = tk.Tk()

        self.textbox = tk.StringVar()

        self.textbox_entry = tk.Entry(self.root, textvariable=self.textbox)

        self.submit = tk.Button(self.root, text="Submit", command=self.get_input)

    def run(self):

    def inputs(self, text):
        # do something with text 

        # and return something      
        return 'a', 'b', 'c'

    def get_input(self):

        text = self.textbox.get()

        if text: # check if text is not empty
            self.textbox.set('') # remove text from entry
            #textbox_entry.delete(0, 'end') # remove text from entry
            self.action_input, self.extra_input, self.texts = self.inputs(text)

# --- functions ---

# empty

# --- main ---

app = Game()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324998 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO