首页 > 解决方案 > Tkinter 和 python 'str' 对象没有属性 'tk'

问题描述

我想用 Python 和 Tkinter 创建一个小数字猜谜游戏,但我遇到了错误,游戏只接受第一个按钮按下,但如果你再次尝试按下它,它会显示错误。我将告诉你代码和回溯错误。我试图评论一切。

#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()

这是第二次按钮按下后的回溯错误:

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'

标签: pythonpython-3.xtkinterpython-3.9

解决方案


原因是当您按下按钮时,您已将变量guessEntry从 Entry 更改为 int ,这样您将在第二次按下后得到错误。

试试下面的代码:

#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()

推荐阅读