0

I have used tkinter to create an asking question messagebox and there are yes and no to choose.

I would like to know how to perform clicking 5 times no then it will show error.

I tried using while loop but after clicking no one time it didn't show error messagebox.

Below is my code:

from tkinter import Tk, Button
from tkinter import messagebox

count = 5

def love():
    ok = messagebox.showwarning(title='WARNING', message='HAHA')
    if (ok == "ok"):
            while count:
               answer = messagebox.askquestion(title='name', message='Who?')

               if(answer == 'yes'):
                    messagebox.showwarning(title='WARNING', message='HAHAAH')
                    break
               else:
                    count-=1
                    messagebox.showwarning(title='WARNING', message='Are you sure')
                    continue
window = Tk()
window.geometry("200x100")
button = Button(window, command=love, text='Click me')
button.pack()
window.mainloop()
B34061186
  • 13
  • 2
  • You don't check if `count` has reached `0` anywhere in your code. – Selcuk Jan 15 '23 at 23:54
  • 1
    count is initialized outside the scope of the method. If you want to use a global variable in a function, you must redeclare it in the function with global keyword, See https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – Galo do Leste Jan 16 '23 at 00:06

1 Answers1

0

couple of things to do (as per the comments to the question).

  • put an if statement into the else block to check if count==0.
  • move count=5 into the function to that it is scoped correctly.

if you are going to use the method with a while loop, the something like this works:

from tkinter import Tk, Button
from tkinter import messagebox




def love():
    count = 5

    ok = messagebox.showwarning(title='WARNING', message='Your Mac has been hacked! HAHA! Answer a few questions to get your Mac back!')
    if (ok == "ok"):
        answer = messagebox.askquestion(title='Who are you?', message='Are you a human?')
        while count:
            if(answer == 'yes'):
                messagebox.showwarning(title='WARNING', message='Your Mac has been hacked! HAHA! Answer a few questions to get your Mac back!')
                break
            else:
                count-=1
                messagebox.showwarning(title='WARNING', message='Are you sure')
                if count == 0:
                    print('you clicked no 5 times !!!')
                continue
        
    
window = Tk()
window.geometry("200x100")
button = Button(window, command=love, text='Click me')
button.pack()
window.mainloop()

Additional comment:

The general code does not actually ask 5 yes or no questions (it shows the ok button 5 times after the first no is clicked), but the above sets you on the correct track...

D.L
  • 4,339
  • 5
  • 22
  • 45
  • 1
    Thank u for your answer, it works. and also thank you for your additional comment, I have edited the code to make sure it asks 5 times yes or no question. – B34061186 Jan 16 '23 at 14:40