Using tkinter, create a widget called Guess Game that, when passed a number and
ID: 3667148 • Letter: U
Question
Using tkinter, create a widget called Guess Game that, when passed a number and packed, creates a gui that allows the user guess the number. The guessing game widget can be created either by creating a widget and packing it (in which case a default Tk window is created): Or by creating a parent window and adding the widget to it: It should create two separate windows. One has hidden number 7, the other 3. When created, the game remembers the secret number. When the user types a number and clicks the Guess button, one of the following three showinfo windows is shown depending on whether the guessed number is too low, too high, or just right. The user should be able to repeatedly guess until the main window is closed: Implementation details: Don't forget your imports GuessGame should inherit from FrameExplanation / Answer
from tkinter import * import random class Application(Frame): """ GUI for Guess My Number """ def __init__(self, master): """ Constructor """ super(Application, self).__init__(master) self.the_number = random.randint(1, 100) self.tries = 1 self.grid() self.create_widgets() def create_widgets(self): """ Create the widgets """ #Labels Label(self, text = "I'm thinking of a number between 1 and 100.").grid(row = 0, column = 0, sticky = W) Label(self, text = "Try to guess it in as few attempts as possible.").grid(row = 1, column = 0, sticky = W) #Text entry self.guess_ent = Entry(self) self.guess_ent.grid(row = 2, column = 0, sticky = W) #Submit button Button(self, text = "Submit", command = self.guess_num).grid(row = 2, column = 0, sticky = N) #Text self.guess_txt = Text(self, width = 40, height = 3, wrap = WORD) self.guess_txt.grid(row = 3, column = 0, columnspan = 2) def guess_num(self): """ The guessing method""" guess = int(self.guess_ent.get()) message = "" # guessing loop if guess > self.the_number: self.guess_txt.delete(0.0, END) self.guess_txt.insert(0.0, "Lower...") self.tries += 1 elif guessRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.