diff --git a/learning_python3.md b/learning_python3.md index 2fbb604..72bd79e 100644 --- a/learning_python3.md +++ b/learning_python3.md @@ -2676,7 +2676,7 @@ for question in data["results"]: choices.append(question["correct_answer"]) choices.extend(question["incorrect_answers"]) for choice in enumerate(choices): - print(html.unescape("\t {} {}".format(*choice)))``` + print(html.unescape("\t {} {}".format(*choice))) ``` You can get some *inspiration* from a small project I did for an other course. @@ -2917,42 +2917,48 @@ if __name__ == "__main__": ## Tkinter guess the number ```python - +import time from tkinter import * import random class MainWindow(Frame): - def __init__(self): + def __init__(self): Frame.__init__(self, master=None, bg="white") MyPanel = PanedWindow.__init__(self) MyNumber = random.randint(0, 100) #Label - MyLabel = Label(MyPanel, text="I have a number in mind...", bg= "blue") - MyLabel.pack(fill="x", ipadx=25, ipady=20) + self.MyLabel = Label(MyPanel, text="I have a number in mind...", bg= "blue") + self.MyLabel.pack(fill="x", ipadx=25, ipady=20) #TextBox MyEntry = Entry(MyPanel) MyEntry.place(x=200,y=90) #Bouton - MyButton = Button(MyPanel, text="I'm clickable!", command=lambda : self.ButtonEnable(MyLabel,MyEntry, MyNumber)) + MyButton = Button(MyPanel, text="I'm clickable!", command=lambda : self.ButtonEnable(MyEntry, MyNumber)) MyButton.place(x=10, y=90) - self.pack(fill=BOTH,expand=True) + self.pack(fill=BOTH,expand=True) - def ButtonEnable(self, MyLabel, MyEntry, MyNumber): + def ButtonEnable(self, MyEntry, MyNumber): if self.IsCorrect(MyEntry.get()): number = int(MyEntry.get()) - if number > MyNumber: - MyLabel.config(text="My number is smaller") - elif number < MyNumber: - MyLabel.config(text="My number is bigger") + if number != MyNumber: + self.GameOver(number, MyNumber) else: - MyLabel.config(text="You WIN!") + self.Win() else: - MyLabel.config(text="I need numbers!") + self.MyLabel.config(text="I need numbers!") + def GameOver(self, number, MyNumber): + if number > MyNumber: + self.MyLabel.config(text="My number is smaller") + else: + self.MyLabel.config(text="My number is bigger") + + def Win(self): + self.MyLabel.config(text="You WIN!") def IsCorrect(self, MyEntry): x = str(MyEntry) @@ -2964,8 +2970,8 @@ class MainWindow(Frame): if __name__ == "__main__": root = Tk() - root.title("Guess the number") - root.geometry("500x300") + root.title("Guess the number") + root.geometry("500x300") win = MainWindow() root.mainloop()