Python Forum

Full Version: Problem with tkinter and string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:

from tkinter import *
import random
from tkinter.messagebox import*
def checking():
outPut=10
overLap=Label(text=outPut)
overLap.place(x=200,y=100)
stringForChecking=str(inputForLetters.get())#ERROR
print(stringForChecking)
answer=str(word.get())#ERROR
if stringForChecking in answer:
overLap=Label(text=outPut)
overLap.place(x=100,y=100)
else:
outPut=outPut-1
overLap=Label(text=outPut)
overLap.place(x=100,y=100)
return()
turn=0
# izbor rijeci
words=['pas','mačka']
#odabir random rijeci
word=(random.choice(words))
#imenovanje varijeble za skrivanje rijeci
wordHidden=''
#for petlja za dodavanje u wordHidden
for char in word:
wordHidden=wordHidden+'_'
wordHidden=wordHidden+' '

#GUI
#stavljane prozora
t=Tk()
t.config(bg='white',width=500,height=200)
#label za pogadanje rijeci
wordToGuess=Label(t,text=wordHidden)
wordToGuess.place(x=60,y=15)
#entry za pogadanje slova
inputForLetters=Entry()
inputForLetters.place(x=20,y=60)
#label za broj preostalih pokusaja
inputGuesses=Label(t,text="Preostali pukusaji: ")
inputGuesses.place(x=80,y=100)
#label za broj preostalih rijeci za pogodit
GuessesLeft=Label(t,text="Broj preostalih rijeci: ")
GuessesLeft.place(x=80,y=150)
#gumb za pozivanje funkcije
if turn<10:
btnForSubmiting=Button(t, text='Pogodi', command=checking)
btnForSubmiting.place(x=10,y=100)
else:
showinfo('GAME OVER')
mainloop()

Error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Sava\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\Sava\Desktop\PyGame\igra2.py", line 10, in checking
answer=str(word.get())#ERROR
AttributeError: 'str' object has no attribute 'get'
hello,
please insert your code with the tools. It's the one with the python symbol blue amd yellow, two to the left is the output. It will make it readable.
change:
stringForChecking=str(inputForLetters.get())#ERROR
to this:
stringForChecking= inputForLetters.get()
all Entry widgets return strings
from tkinter import *
import random
from tkinter.messagebox import*
def checking():
    outPut=10
    overLap=Label(text=outPut)
    overLap.place(x=200,y=100)
    stringForChecking=inputForLetters.get()#ERROR
    print(stringForChecking)
    answer=word.get()#ERROR
    if stringForChecking in answer:
        overLap=Label(text=outPut)
        overLap.place(x=100,y=100)
    else:
        outPut=outPut-1
        overLap=Label(text=outPut)
        overLap.place(x=100,y=100)
    return()
turn=0 
# izbor rijeci
words=['pas','mačka']
#odabir random rijeci
word=(random.choice(words))
#imenovanje varijeble za skrivanje rijeci
wordHidden=''
#for petlja za dodavanje u wordHidden
for char in word:
    wordHidden=wordHidden+'_'
    wordHidden=wordHidden+' '

#GUI
#stavljane prozora
t=Tk()
t.config(bg='white',width=500,height=200)
#label za pogadanje rijeci
wordToGuess=Label(t,text=wordHidden)
wordToGuess.place(x=60,y=15)
#entry za pogadanje slova
inputForLetters=Entry()
inputForLetters.place(x=20,y=60)
#label za broj preostalih pokusaja
inputGuesses=Label(t,text="Preostali pukusaji: ")
inputGuesses.place(x=80,y=100)
#label za broj preostalih rijeci za pogodit
GuessesLeft=Label(t,text="Broj preostalih rijeci: ")
GuessesLeft.place(x=80,y=150)
#gumb za pozivanje fnkcije
if turn<10:
    btnForSubmiting=Button(t, text='Pogodi', command=checking)
    btnForSubmiting.place(x=10,y=100)
else:
    showinfo('GAME OVER')
mainloop()
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Sava\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:\Users\Sava\Desktop\PyGame\igra2.py", line 10, in checking answer=word.get()#ERROR AttributeError: 'str' object has no attribute 'get'
Still outputs the error
word wont have a get method because its a string chosen at random from the list of strings in words
words=['pas','mačka']
word=(random.choice(words))
I want to make programm that chooses one random word from list of words. For the lenght of the choosen word, programm will print "_" symbols. Person has to insert a letter in input bar to guess what word programm choosed. Person has 10 tries to guess the word and after every wrong guess, number of tries lowers for one and prints as a label. I get a problem as soon as I insert letter and click "Pogodi" button. You said that word wont have get method if random choosed from list of words. Hoe can I fix the problem?
Use word directly it is not a variable with a pointer to a Entry like inputForLetters, it is a string.