Tragg Thorsgard
Tragg Thorsgard

Reputation: 1

tkinter, button returns variable

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.geometry('800x600+100+100')
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:
    root.update_idletasks()
    root.update()
    submit.wait_variable(textboxentry)   
## 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!

Upvotes: 0

Views: 2192

Answers (2)

furas
furas

Reputation: 143017

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 
    print(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)
textbox_entry.pack()

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

root.mainloop()

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.textbox_entry.pack()

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

    def run(self):
        self.root.mainloop()

    def inputs(self, text):
        # do something with text 
        print(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()
app.run()

Upvotes: 0

Nae
Nae

Reputation: 15345

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
    lblTextVar.set(obj.get())

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

txt = tk.Entry(root)
txt.pack()

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

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

root.mainloop()

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.

Upvotes: 1

Related Questions