-2

I wanted to store data from Entry to an variable. So I did this:

main = Tk()

main.title("Players Names")

var1 = StringVar()
var2 = StringVar()

Label(main, text="Player One: ").pack(side=LEFT)
e1 = Entry(main, textvariable=var1).pack(side=LEFT)
Label(main, text="Player Two: ").pack(side=LEFT)
e2 = Entry(main, textvariable=var2).pack(side=LEFT)

def enter():
    player1 = var1.get()
    player2 = var2.get()


def printIt():
    print player1
    print player2

Button(main, width=15, text="ENTER", command=enter).pack(side=LEFT)
Button(main, width=15, text="PRINT", command=printIt).pack(side=LEFT)

main.mainloop()

When I tried it...

...it printed nothing. What shall I do (except by putting the printIt function in enter function. I know this; i just want to store the data)?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

1

player1 and player2 are local to the enter() function and therefore cannot be accessed outside of it. While you could make them global variables that can be referenced by any and all functions, globals are generally considered to be poor way to do things for a variety of reasons. (See Global Variables Are Bad for a specifics about why it's probably a good idea to avoid them.)

One way to sidestep having to do that in Tkinter applications, is make your program more object-oriented and define the main window to be a subclass of its Tkinter.Frame widget. This is fairly easy to do and then you can then add as many additional attributes to it as needed and reference them from any of its methods via self.xxx without polluting the global namespace with a bunch of "stuff".

Here's what I mean (note I remove your e1 and e2 variables because they were both being assigned None (because that's what the pack() method returns). They weren't being used anywhere else, anyway, which is probably why you never noticed.

from Tkinter import *

class App(Frame):
    def __init__(self, title, master=None):
        Frame.__init__(self, master)  # initialize base class
        self.pack()
        self.master.title(title)

        self.var1 = StringVar()
        self.var2 = StringVar()

        Label(self, text="Player One: ").pack(side=LEFT)
        Entry(self, textvariable=self.var1).pack(side=LEFT)
        Label(self, text="Player Two: ").pack(side=LEFT)
        Entry(self, textvariable=self.var2).pack(side=LEFT)

        Button(self, width=15, text="ENTER", command=self.enter).pack(side=LEFT)
        Button(self, width=15, text="PRINT", command=self.printIt).pack(side=LEFT)

    def enter(self):
        self.player1 = self.var1.get()
        self.player2 = self.var2.get()

    def printIt(self):
        print(self.player1)
        print(self.player2)

app = App("Player's Names")
app.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
0

player1 and player2 are stored exclusively in the function as local variables, which means they can only be accessed within the function.

You should instead return the results:

def enter():
    return [var1.get(), var2.get()]

def printIt():
    print enter()[0], enter()[1]

or make them global variables by declaring them outside your functions which is probably simpler:

main = Tk()

main.title("Players Names")

var1 = StringVar()
var2 = StringVar()
player1 = None               # Global variable declaration
player2 = None

Label(main, text="Player One: ").pack(side=LEFT)
e1 = Entry(main, textvariable=var1).pack(side=LEFT)
Label(main, text="Player Two: ").pack(side=LEFT)
e2 = Entry(main, textvariable=var2).pack(side=LEFT)

def enter():
    global player1, player2
    player1 = var1.get()
    player2 = var2.get()


def printIt():
    global player1, player2
    print player1
    print player2

Button(main, width=15, text="ENTER", command=enter).pack(side=LEFT)
Button(main, width=15, text="PRINT", command=printIt).pack(side=LEFT)

main.mainloop()
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38