Tkinter 和 python 'str' 对象没有属性 'tk'

Tkinter and python 'str' object has no attribute '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'

原因是当您按下按钮时,您将变量 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()