我试图做一个猜谜游戏的案例研究,但没有用
固定缩进 将breezypythongui.py放入同一文件夹,但仍然没有显示
import random
from breezypythongui import EasyFrame
class GuessingGame(EasyFrame):
"""playing a guessing game with a user."""
def __init__(self):
"""setting up the window,widgets and data."""
EasyFrame.__init__(self, title = "Guessing Game")
# Initialize the instance variables for the data
self.myNumber = random.randint(1,100)
self.count = 0
# Create and add widgets to the window
greeting = "Guess a number between 1 and 100."
self.hintLabel = self.addLabel(text = greeting,
row = 0, column = 0,
sticky = "NSEW",
columnspan = 2)
self.addLabel(text = "Your guess", row = 1, column = 0)
self.guessField = self.addIntegerField(0, row = 1, column = 1)
# Buttons have no command attributes yet
self.nextButton = self.addButton(text = "Next",row = 2,
column = 0,
command = self.next)
self.newButton = self.addButton(text = "New game",
row = 2,column = 1,
command = self.reset)
def nextGuess(self):
"""Processes the user's next guess."""
self.count += 1
guess = self.guessField.getNumber()
if guess == self.myNumber:
self.hintLabel["text"] = "You've guessed it in " + \
str(self.count) + " attempts!"
self.nextButton["state"] = "disabled"
elif guess < self.myNumber:
self.hintLabel["text"] = "Sorry, too small!"
else:
self.hintLabel["text"] = "Sorry, too large!"
def newGame(self):
"""Resets the data and GUI to their original states."""
self.myNumber = random.randint(1, 100)
self.count = 0
greeting = ("Guess a number between 1 and 100.")
self.hintLabel["text"] = greeting
self.guessField.setNumber(0)
self.nextButton["state"] = "normal"
def main():
"""Instantiate and pop up the window."""
GuessingGame().mainloop()
if __name__=="__main__":
main()
我希望程序告诉我即时消息是否接近正确的数字或两个低位
答案 0 :(得分:0)
我无法运行它,但是在if __name__=="__main__":
中看到错误的缩进。删除缩进。
def main():
"""Instantiate and pop up the window."""
GuessingGame().mainloop()
if __name__=="__main__":
main()