10.2
#Guess number in graphic
import random
from tkinter import *
class Application(Frame):
the_number=0
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
#create instruction label
self.inst_lbl=Label(self, text = "Try to guess number from 1 to 100")
self.inst_lbl.grid(row=0, column = 0, columnspan = 2, sticky = W)
# create newgame button
self.newgame_bttn = Button(self, text = " new game ", command = self.new_game)
self.newgame_bttn.grid(row = 1, column = 0, columnspan=3, sticky = W)
#create label for guess
self.guess_lbl=Label(self, text = "Your guess: ")
self.guess_lbl.grid(row=2, column = 0, columnspan = 1, sticky = W)
# create entry widget to accept attempt
self.gu_ent= Entry(self)
self.gu_ent.grid(row=2, column=1, columnspan=1, sticky = W)
# create submit button
self.submit_bttn = Button(self, text = "Submit", command = self.game)
self.submit_bttn.grid(row = 2, column = 3, sticky = W)
# create text widget to display message
self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def print_text(self, message=''):
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, message)
def new_game(self):
Application.the_number=random.randint(1, 100)
self.print_text('New game is started')
def check(self):
try:
r=int(self.gu_ent.get())
except:
self.print_text('Wrong input. Try again.')
r="WRONG"
return r
def game(self):
if self.check()=="WRONG":
self.print_text('Wrong input. Try again.')
else:
contents=int(self.gu_ent.get())
self.print_text('Try to guess number from 1 to 100')
if int(contents) not in range(0,101,1) :
self.print_text('Wrong number. Try again.')
else:
if contents<Application.the_number:
self.print_text('Higher')
elif contents>Application.the_number:
self.print_text('Lower')
elif contents==Application.the_number:
self.print_text('You have guessed it! It was ' + str(contents)+ \
' Tap a "New game" button to start a new game')
# main
root = Tk()
root.title("Guess number")
root.geometry("500x250")
app = Application(root)
root.mainloop()
import random
from tkinter import *
class Application(Frame):
the_number=0
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
#create instruction label
self.inst_lbl=Label(self, text = "Try to guess number from 1 to 100")
self.inst_lbl.grid(row=0, column = 0, columnspan = 2, sticky = W)
# create newgame button
self.newgame_bttn = Button(self, text = " new game ", command = self.new_game)
self.newgame_bttn.grid(row = 1, column = 0, columnspan=3, sticky = W)
#create label for guess
self.guess_lbl=Label(self, text = "Your guess: ")
self.guess_lbl.grid(row=2, column = 0, columnspan = 1, sticky = W)
# create entry widget to accept attempt
self.gu_ent= Entry(self)
self.gu_ent.grid(row=2, column=1, columnspan=1, sticky = W)
# create submit button
self.submit_bttn = Button(self, text = "Submit", command = self.game)
self.submit_bttn.grid(row = 2, column = 3, sticky = W)
# create text widget to display message
self.secret_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.secret_txt.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def print_text(self, message=''):
self.secret_txt.delete(0.0, END)
self.secret_txt.insert(0.0, message)
def new_game(self):
Application.the_number=random.randint(1, 100)
self.print_text('New game is started')
def check(self):
try:
r=int(self.gu_ent.get())
except:
self.print_text('Wrong input. Try again.')
r="WRONG"
return r
def game(self):
if self.check()=="WRONG":
self.print_text('Wrong input. Try again.')
else:
contents=int(self.gu_ent.get())
self.print_text('Try to guess number from 1 to 100')
if int(contents) not in range(0,101,1) :
self.print_text('Wrong number. Try again.')
else:
if contents<Application.the_number:
self.print_text('Higher')
elif contents>Application.the_number:
self.print_text('Lower')
elif contents==Application.the_number:
self.print_text('You have guessed it! It was ' + str(contents)+ \
' Tap a "New game" button to start a new game')
# main
root = Tk()
root.title("Guess number")
root.geometry("500x250")
app = Application(root)
root.mainloop()
Почему 9 главу не сделала ?
ОтветитьУдалитьпотому что запуталась с наследованием
Удалить