Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tkinter和python';str';对象没有属性';tk';_Python_Python 3.x_Tkinter_Python 3.9 - Fatal编程技术网

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

Tkinter和python';str';对象没有属性';tk';,python,python-3.x,tkinter,python-3.9,Python,Python 3.x,Tkinter,Python 3.9,我想用Python和Tkinter创建一个小小的数字猜谜游戏,我遇到了一个错误,游戏只接受第一次按下按钮,但是如果你再次按下它,它会显示一个错误。我将告诉你如何处理代码和回溯错误。我试着评论一切 #import modules that are needed from tkinter import * import random #define the random number used randInt = random.randint(1,10) #create basic tkint

我想用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()

原因是,当您按下按钮时,您已将变量
gusticeEntry
从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()
#导入所需的模块
从tkinter进口*
随机输入
#定义使用的随机数
randInt=random.randInt(1,10)
#创建基本tkinter窗口
r=Tk()
#定义窗口标题
r、 标题(“猜我的号码”)
#定义窗口大小
r、 几何图形(“600x200”)
#定义checkGuess函数,该函数检查提供的猜测是否为随机数
def checkGuess():
#导入变量
全局随机输入
#试着把猜测变成一个整数
尝试:
guessEntry1=Entry.get(guessEntry)
guessEntry1=int(guessEntry1)
#如果不可能,则将其打印到控制台并结束该功能
除值错误外:
打印(“提供的输入不是整数,请尝试另一个”)
返回
#检查猜测是否为数字
如果randInt==guessEntry1:
Guess CorrectLabel=标签(r,text=“Guess correct,创建新编号”)
猜一猜标签。包()
randInt=random.randInt(1,10)
#检查猜测是否小于数字
elif randIntguessEntry1:
tooLowLabel=标签(r,text=“猜测太低”)
tooLowLabel.pack()
打印(randInt)
#函数退出应用程序
def quitApp():
r、 退出
#制作一个标题标签
titleLabel=标签(r,text=“猜测1到10之间的随机数”)
#在屏幕上显示标签
titleLabel.pack()
#制作一个输入小部件来输入猜测
猜测条目=条目(r)
#在屏幕上显示输入小部件
guessEntry.pack()
#制作一个按钮来检查用户给出的条目
checkButton=按钮(r,text=“Check Guess”,command=checkGuess)
#在屏幕上显示按钮
checkButton.pack()
#制作一个按钮以退出应用程序
exitButton=按钮(r,text=“退出”,command=quitApp)
#在屏幕上显示按钮
exitButton.pack()
#在屏幕上显示窗口
如果名称=“\uuuuu main\uuuuuuuu”:
r、 mainloop()

谢谢,我会尽量避免再次出现这种错误。我只是给变量起个更好的名字
#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()