adds replay to guess the number gui

This commit is contained in:
waldek 2022-05-02 16:49:16 +02:00
parent d4c452f6c7
commit 5368c5023f
1 changed files with 76 additions and 0 deletions

View File

@ -326,6 +326,82 @@ if __name__ == "__main__":
```
</details>
<details>
<summary>With replay function</summary>
```python
import tkinter as tk
import random
class Application(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("hello world")
self.geometry("500x400")
self.resizable(0, 0)
self.header = tk.Label(text="I have a number in mind...")
self.header.grid(column=0, row=0, sticky=tk.W)
self.question = tk.Label(self, text="What's your guess?")
self.question.grid(column=0, row=1, sticky=tk.W)
self.input = tk.Entry(self)
self.input.grid(column=1, row=1, sticky=tk.W)
self.button = tk.Button(self, text="check", command=self.clicked)
self.button.grid(column=0, row=2, sticky=tk.W)
self.output = tk.Label(self, text="...")
self.output.grid(column=1, row=2, sticky=tk.W)
self.init_new_game()
def _is_entry_digit(self):
number = self.input.get()
if number.isdigit():
number = int(number)
return number
def compare_numbers(self):
number = self._is_entry_digit()
if not number:
msg = "numbers please..."
else:
if number < self.number:
msg = "my number is bigger"
elif number > self.number:
msg = "my number is smaller"
elif number == self.number:
msg = "bingo!"
self.won = True
self.button.config(text="play again!")
self.output.config(text=msg)
def init_new_game(self):
self.won = False
self.number = random.randint(0, 100)
print(self.number)
self.button.config(text="check")
self.output.config(text="")
self.input.delete(0, 'end')
def clicked(self):
if self.won:
self.init_new_game()
else:
self.compare_numbers()
if __name__ == "__main__":
app = Application()
app.mainloop()
```
</detail>
## MVC design pattern
A simple console only MVC.