Python Forum
Problem with tkinter and string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with tkinter and string
#1
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'
Reply
#2
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
Reply
#3
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
Reply
#4
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))
Reply
#5
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?
Reply
#6
Use word directly it is not a variable with a pointer to a Entry like inputForLetters, it is a string.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,547 Feb-17-2022, 07:02 AM
Last Post: pymn
  Python3 tkinter radiobutton problem Nick_tkinter 14 5,834 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,486 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,165 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,440 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  Tkinter problem DPaul 6 4,049 May-28-2020, 03:40 PM
Last Post: DPaul
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,523 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,540 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [Tkinter] Tkinter problem catlessness 1 2,013 Jan-15-2020, 05:17 AM
Last Post: Larz60+
  Problem with Submit button Tkinter Reldaing 2 3,611 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020