简体   繁体   English

Tkinter 和 python 'str' object 没有属性 'tk'

[英]Tkinter and python 'str' object has no attribute 'tk'

I want to create a little number guessing game with Python and Tkinter and I ran into error where the game would only accept the first button push but if you tried to press it again it would show you an error.我想用 Python 和 Tkinter 创建一个小数字猜谜游戏,我遇到了错误,游戏只接受第一个按钮按下,但如果你再次按下它会显示错误。 I will just how you the code and the traceback error.我将告诉你代码和回溯错误。 I tried to comment everything.我试图评论一切。

#import modules that are needed
from tkinter import *
import random


#define the random number used
randInt = random.randint(1,10)

#create basic tkinter window
r = Tk()
#define the window title
r.title(" Guess My Number")
#define window size
r.geometry("600x200")


#define the checkGuess function, which checks if the guess provided is the random number
def checkGuess():
    #import variables
    global randInt, guessEntry
    #try to make the guess a integer
    try:
        guessEntry = Entry.get(guessEntry)
        guessEntry = int(guessEntry)
    #if that is not possible, prints that to the console and ends the function
    except ValueError:
        print("Input provided is not a whole number, please try another one")
        return
    #checks if the guess is the number
    if randInt == guessEntry:
        guessCorrectLabel = Label(r, text="Guess correct, new number created")
        guessCorrectLabel.pack()
        randInt = random.randint(1,10)
    #checks if the guess is smaller than the number
    elif randInt < guessEntry:
        tooHighLabel = Label(r, text="Guess is too high")
        tooHighLabel.pack()
        print(randInt)
    elif randInt > guessEntry:
        tooLowLabel = Label(r, text="Guess is too low")
        tooLowLabel.pack()
        print(randInt)

#function to quit the app
def quitApp():
    r.quit()


#make a title label
titleLabel = Label(r, text="Guess a random Number between 1 and 10")
#show the label on the screen
titleLabel.pack()

#make a Entry widget to enter the guess
guessEntry = Entry(r)
#show the entry widget on the screen
guessEntry.pack()

#make a button to check the entry given by the user
checkButton = Button(r, text="Check Guess", command=checkGuess)
#show the button on the screen
checkButton.pack()

#make a button to exit the application
exitButton = Button(r, text="QUIT", command=quitApp)
#show the button on the screen
exitButton.pack()


#show the window on the screen
if __name__ == "__main__":
    r.mainloop()

and here is the traceback error after a second button push:这是第二次按钮按下后的回溯错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Python\Python392\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "Z:\Coding\Projects\Pycharm\numGuesser1\main.py", line 23, in checkGuess
    guessEntry = Entry.get(guessEntry)
  File "D:\Python\Python392\lib\tkinter\__init__.py", line 3043, in get
    return self.tk.call(self._w, 'get')
AttributeError: 'str' object has no attribute 'tk'

The reason is that when you press the button you have change the variable guessEntry from Entry to int,so that you will get the error after a second push.原因是当您按下按钮时,您将变量guessEntry从Entry 更改为int,这样您将在第二次按下后得到错误。

Try the following code:试试下面的代码:

#import modules that are needed
from tkinter import *
import random


#define the random number used
randInt = random.randint(1,10)

#create basic tkinter window
r = Tk()
#define the window title
r.title(" Guess My Number")
#define window size
r.geometry("600x200")

#define the checkGuess function, which checks if the guess provided is the random number
def checkGuess():
    #import variables
    global randInt, guessEntry
    #try to make the guess a integer
    try:
        guessEntry1 = Entry.get(guessEntry)
        guessEntry1 = int(guessEntry1)
    #if that is not possible, prints that to the console and ends the function
    except ValueError:
        print("Input provided is not a whole number, please try another one")
        return
    #checks if the guess is the number
    if randInt == guessEntry1:
        guessCorrectLabel = Label(r, text="Guess correct, new number created")
        guessCorrectLabel.pack()
        randInt = random.randint(1,10)
    #checks if the guess is smaller than the number
    elif randInt < guessEntry1:
        tooHighLabel = Label(r, text="Guess is too high")
        tooHighLabel.pack()
        print(randInt)
    elif randInt > guessEntry1:
        tooLowLabel = Label(r, text="Guess is too low")
        tooLowLabel.pack()
        print(randInt)

#function to quit the app
def quitApp():
    r.quit()


#make a title label
titleLabel = Label(r, text="Guess a random Number between 1 and 10")
#show the label on the screen
titleLabel.pack()

#make a Entry widget to enter the guess
guessEntry = Entry(r)
#show the entry widget on the screen
guessEntry.pack()

#make a button to check the entry given by the user
checkButton = Button(r, text="Check Guess", command=checkGuess)
#show the button on the screen
checkButton.pack()

#make a button to exit the application
exitButton = Button(r, text="QUIT", command=quitApp)
#show the button on the screen
exitButton.pack()


#show the window on the screen
if __name__ == "__main__":
    r.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM