-3

I am currently writing a hang man game. Using Tkinter for GUI.

How can I get a string from a function:

def startgame():
     player21abel=Label(text="    Guess a Letter     ",fg="black").place(x=10,y=60)
     mbutton=Button(text="  Press to summit   ",command=guess).place(x=220,y=100)
     player2=StringVar()
     player2input=Entry(textvariable=player2).place(x=220,y=56)
     test=""
     uetext=utext.get()


def guess():
     test=player2.get()
     test=""
     player2=StringVar
     print (test)

I get the error:

line 16, in guess
test=player2.get()
UnboundLocalError: local variable 'player2' referenced before assignment

I want to get the text box input from player2input and process it on function guess. But it does not recognise it as a string??

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
Angus
  • 17

2 Answers2

0

player2 doesn't exist ing the guess namespace. The typical way to share data between functions is to use a class:

class App(object):
    def __init__(self):
         self.master = Tk()
    def startgame():
         player21abel=Label(self.master, text="    Guess a Letter     ",fg="black")
         player2label.place(x=10,y=60)
         mbutton=Button(self.master, text="  Press to summit   ",command=self.guess)
         mbutton.place(x=220,y=100)
         self.player2=StringVar()
         player2input=Entry(self.master, textvariable=self.player2)
         player2input.place(x=220,y=56)
         test=""
         uetext=utext.get()


    def guess():
         test=self.player2.get()
         test=""
         self.player2=StringVar
         print (test)

Then you'd work with it as follows:

app = App()
app.startgame()

Note that you had a number of other bugs as well -- You weren't passing a parent widget to your label/entry/button and you should generally craeate a widget and then use it's geometry manager in a separate line. Otherwise your references will all be None. e.g.

foo = button(master, ...).grid(...)  # Wrong: foo is None!!!

foo = button(master, ...)
foo.grid(...)  # right, foo is a tkinter Widget.
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Sorry. I don't really get what you mean by working with these commands: – Angus Oct 04 '14 at 09:40
  • app = App() app.startgame(). In the upcoming code, if I would like to like a button in to a function, do I replace : mbutton=Button(text="Press to start game",command=startgame).place(x=123,y=100) – Angus Oct 04 '14 at 09:41
0

You must pass it to the function as a parameter.

To do this in tkinter take a look this answer: https://stackoverflow.com/a/6921225/1165441

All you need to do is use a lambda that calls your function with the parameter you want.

You will need to rearrange your code a bit, but the command attribute will look like this:

command=lambda: guess(paramYouWantToPass)

Edit: I would say that the answer posted by @mgilson is a more correct way to solve your problem, but understanding how to pass parameters to tkinter command callbacks using lambdas is something you should understand as well.

Community
  • 1
  • 1
edhedges
  • 2,722
  • 2
  • 28
  • 61