# Alerting users with a pop-up window

I'm relatively new at programming in general including Python. But I've tried to create a program in Python that alerts users through a pop up window to perform an action every 2 hours.

import time
import ctypes

count = 0

print "This break message was sent on "+time.ctime()
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerYes", "SWindowTitle", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0)

while (count < 4):
time.sleep(60*60*2)
print "This break message was sent on"+time.ctime()
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4)
if returnValue == 6:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerYes", "WindowTitle", 0x40 | 0x0)
elif returnValue == 7:
messageBox = ctypes.windll.user32.MessageBoxA
returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0)
count = count + 1

time.localtime()


It essentially calls a message box with a title and a text in it and if the user clicks one button then another one follow up message box appears. If the user clicks on the other button, then another follow up message appears. This process is repeated every 2 hours for 5 times.

How could I improve this code to be shortened? Could I use a function somehow? Or what would be a way to shorten it and avoid me repeating myself too much (if that is the case)?

• I didn't notice it on your SO question, but are you really giving the same message in every case? – RubberDuck Feb 25 '15 at 3:16
• Please fix the indentation. Also what's the point of returnValue = messageBox(None, "Text", "Title", 0x40 | 0x0) inside of those if conditions(in while loop) if you're actually ignoring them when the while loop restarts? – Ashwini Chaudhary Feb 25 '15 at 3:17
• @AshwiniChaudhary - thanks for the indentation tip. The first block of code calls the pop up window as soon as someone runs the program. Then, everything after the while loop runs every 2 hours for 5 times. Does this help clarify? – FelipeRego Feb 25 '15 at 4:24
• @RubberDuck - Yes I've also edited the texts in the code above to try to illustrate. But if you click in the button Yes then it shows one message as follow up if you click No, then it shows another message as follow up. After 2hrs, it repeats the process... – FelipeRego Feb 25 '15 at 4:26
• I was talking about the returnValue = messageBox(None, "TextRespondingToAnswerNo", "WindowTitle", 0x40 | 0x0) inside of the while loop, you're not using these returnValue anywhere. So, are those returnValue actually required? Currently you're re-calculating returnValue = messageBox(None, "TextAskingToDoSomething", "WindowTitle", 0x40 | 0x4) every time the while loop re-starts. – Ashwini Chaudhary Feb 25 '15 at 4:27

You could change count = count + 1 to count += 1 besides that, do you need to repeat messageBox = ctypes.windll.user32.MessageBoxA so much? If it doesn't change, couldn't you just have it once? You could also add variables to shorten repeated lines like making a variable called resetMsgBox and have it be: resetMsgBox = ctypes.windll.user32.MessageBoxA so it's easier to read by having messageBox = resetMsgBox every line instead, I suppose.