patternpythonMinor
Alerting users with a pop-up window
Viewed 0 times
withwindowusersalertingpop
Problem
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.
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)?
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)?
Solution
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.Context
StackExchange Code Review Q#82532, answer score: 5
Revisions (0)
No revisions yet.