import random, easygui as eg
def main(low=1, high=100):
"""Guess My Number
The computer picks a random number between low and high
The player tries to guess it and the computer lets
the player know if the guess is too high, too low
or right on the money
"""
print("Welcome to 'Guess My Number'!")
print("I'm thinking of a number between {} and {}.".format(low, high))
print("Try to guess it in as few attempts as possible.")
the_number = random.randint(low, high)
tries = 0
while True:
guess = ask_number("Enter your guess:", low, high)
if guess == the_number:
print("You're right on the money!")
break
elif guess > the_number:
print("Too high!")
else:
print("Too low!")
tries += 1
print("You guessed it! The number was {}.".format(the_number))
print("And it only took you {} tries!".format(tries))
input("Press the enter key to exit.")
def ask_number(question, low, high):
"""Get the user to input a number in the appropriate range."""
while True:
try:
response = int(input(question))
except ValueError:
print ("Not an integer")
else:
if response in range(low, high+1):
return response
print ("Out of range")
if __name__ == "__main__":
main()
嘿家伙我试图让以下代码在一个GUI中使用easyGUI工作,但我不太确定如何做到这一点我试图获取输入和返回文本或其他gui将工作,我打开建议
答案 0 :(得分:0)
from Tkinter import *
from random import *
################################################################################
# Application ##################################################################
################################################################################
class Application(Tk):
def __init__(self):
Tk.__init__(self); self.resizable(0, 0)
self.geometry('{}x{}'.format(640, 480))
self.button = Button(text = 'Check', command = self.checknum)
self.button.place_configure(x = 170, y = 260, width = 300)
self.value = IntVar(); self.number = Entry(textvariable = self.value)
self.number.place_configure(x = 170, y = 220, width = 300)
self.store = randint(1, 100)
self.state, self.tries = 1, 0
self.label_1 = Label(text = 'Tries: {}'.format(self.tries))
self.label_1.place_configure(x = 10, y = 10) # place label
self.label_2 = Label(text = 'Messages: ')
self.label_2.place_configure(x = 10, y = 40)
self.mainloop()
def checknum(self):
if self.state:
if self.value.get() > self.store:
self.tries += 1
self.label_1.configure(text = 'Tries: {}'.format(self.tries))
self.label_2.configure(text = 'Messages: {}'.format('Lower'))
if self.value.get() < self.store:
self.tries += 1
self.label_1.configure(text = 'Tries: {}'.format(self.tries))
self.label_2.configure(text = 'Messages: {}'.format('Higher'))
if self.value.get() == self.store:
self.tries += 1; self.state = 0
self.label_1.configure(text = 'Tries: {}'.format(self.tries))
self.label_2.configure(text = 'Messages: {}'.format('You won'))
################################################################################
# Run ##########################################################################
################################################################################
app = Application()
################################################################################
################################################################################
################################################################################