def showCustomAnswerDialog(theWindow, theText): oDialogBackground = pygwidgets.Image(theWindow, (35, 450), 'images/dialog.png') oPromptDisplayText = pygwidgets.DisplayText(theWindow, (0, 480), theText, width=WINDOW_WIDTH, justified='center', fontSize=36) oUserInputText = pygwidgets.InputText(theWindow, (200, 550), '', fontSize=36, initialFocus=True) oNoButton = pygwidgets.CustomButton(theWindow, (65, 595), 'images/noThanksNormal.png', over='images/noThanksOver.png', down='images/noThanksDown.png', disabled='images/noThanksDisabled.png') oYesButton = pygwidgets.CustomButton(theWindow, (330, 595), 'images/addNormal.png', over='images/addOver.png', down='images/addDown.png', disabled='images/addDisabled.png') userAnswer = pyghelpers.customAnswerDialog(theWindow, oDialogBackground, oPromptDisplayText, oUserInputText, oYesButton, oNoButton) return userAnswer
def showCustomAnswerDialog(theWindow, theText): oDialogBackground = pygwidgets.Image(theWindow, (60, 120), 'images/dialog.png') oPromptDisplayText = pygwidgets.DisplayText(theWindow, (0, 170), theText, \ width=WINDOW_WIDTH, justified='center', fontSize=36) oUserInputText = pygwidgets.InputText(theWindow, (225, 220), '', fontSize=36, initialFocus=True) oNoButton = pygwidgets.CustomButton(theWindow, (105, 265), \ 'images/cancelNormal.png',\ over='images/cancelOver.png',\ down='images/cancelDown.png',\ disabled='images/cancelDisabled.png') oYesButton = pygwidgets.CustomButton(theWindow, (375, 265), \ 'images/okNormal.png',\ over='images/okOver.png',\ down='images/okDown.png',\ disabled='images/okDisabled.png') choiceAsBoolean, userAnswer = pyghelpers.customAnswerDialog(theWindow, oDialogBackground, \ oPromptDisplayText, oUserInputText, oYesButton, oNoButton) return choiceAsBoolean, userAnswer
oRunButton.disable() oRoundsDisplay = pygwidgets.DisplayText(window, (315, 460), '0', fontName='monospaces', fontSize=28, width=70, justified='right') oOutOfDisplay = pygwidgets.DisplayText(window, (390, 460), 'out of', fontName='monospaces', fontSize=28) oMaxRoundsInput = pygwidgets.InputText(window, (454, 460), str(nRoundsToRun), fontName='monospaces', fontSize=28, width=70, initialFocus=True) oDiceImage = pygwidgets.Image(window, (28, 15), 'images/twoDice.png') imagesDict = { 1: 'images/dice1.png', 2: 'images/dice2.png', 3: 'images/dice3.png', 4: 'images/dice4.png', 5: 'images/dice5.png', 6: 'images/dice6.png' } oDie1 = pygwidgets.ImageCollection(window, (630, 15), imagesDict, 1) oDie2 = pygwidgets.ImageCollection(window, (715, 15), imagesDict, 1)
def textAnswerDialog(theWindow, theRect, prompt, trueButtonText='OK',\ falseButtonText='Cancel', backgroundColor=DIALOG_BACKGROUND_COLOR): """Puts up a text-based two-button answerable modal dialog (typically Yes/No or OK/Cancel) Parameters: | theWindow - the window to draw in | theRect - the rectangle of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | trueButtonText - text on the True button (defaults to 'OK') | falseButtonText - text on the False button (defaults to 'Cancel') | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) Returns: | trueOrFalse - True means true button was pressed, False means false button was pressed | userText - if above is True, then this contains the text that the user typed. """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] frameRect = pygame.Rect(dialogLeft + 1, dialogTop + 1, dialogWidth - 2, dialogHeight - 2) INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center') inputWidth = dialogWidth - (2 * INSET) inputText = pygwidgets.InputText(theWindow, (dialogLeft + INSET, dialogTop + 80), width=inputWidth, initialFocus=True) falseButton = pygwidgets.TextButton(theWindow, (0, 0), falseButtonText) trueButton = pygwidgets.TextButton(theWindow, (0, 0), trueButtonText) trueButtonRect = trueButton.getRect() trueButtonHeight = trueButtonRect[3] trueButtonWidth = trueButtonRect[2] # get width xPos = dialogLeft + dialogWidth - trueButtonWidth - INSET buttonsY = dialogTop + dialogHeight - trueButtonHeight - 20 falseButton.setLoc((dialogLeft + INSET, buttonsY)) trueButton.setLoc((xPos, buttonsY)) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if inputText.handleEvent(event): theAnswer = inputText.getValue() return True, theAnswer if trueButton.handleEvent(event): theAnswer = inputText.getValue() return True, theAnswer if falseButton.handleEvent(event): return False, None # 8 - Do any "per frame" actions # 9 - Clear the screen area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, theRect, 1) # 10 - Draw the screen elements promptText.draw() inputText.draw() falseButton.draw() trueButton.draw() # 11 - Update the screen pygame.display.update()
window = pygame.display.set_mode([WINDOW_WIDTH, WINDOW_HEIGHT]) clock = pygame.time.Clock() # create a clock object oTest = Test() # 4 - Load assets: image(s), sounds, etc. oBackgroundImage = pygwidgets.Image(window, (0, 0), 'images/background.jpg') oDisplayTextTitle = pygwidgets.DisplayText(window, (0, 20), 'pygwidgets example by Irv Kalb', fontSize=36, width=640, textColor=BLACK, justified='center') oInputTextA = pygwidgets.InputText(window, (20, 100), 'Default input text', textColor=WHITE, backgroundColor=BLACK, fontSize=24, width=250) oInputTextB = pygwidgets.InputText( window, (20, 200), initialFocus=True, textColor=(0, 0, 255), fontSize=28) # add: , mask='*' for passwords oDisplayTextA = pygwidgets.DisplayText(window, (20, 300), 'Here is some display text', fontSize=24, textColor=WHITE, justified='center') oDisplayTextB = pygwidgets.DisplayText(window, (20, 400), 'Here is some display text',
def textAnswerDialog(theWindow, theRect, prompt, okButtonText='OK', cancelButtonText='Cancel', backgroundColor=DIALOG_BACKGROUND_COLOR, promptTextColor=DIALOG_BLACK, inputTextColor=DIALOG_BLACK): """A function that puts up a text-based two-button answerable modal dialog (typically OK/Cancel) Parameters: | theWindow - the window to draw in | theRect - the rectangle (or tuple) of the dialog box in the application window | prompt - prompt (title) string to be displayed in the dialog box Optional keyword parameters: | okButtonText - text on the OK button (defaults to 'OK') | cancelButtonText - text on the Cancel button (defaults to 'Cancel') | backgroundColor - rgb background color for the dialog box (defaults to (0, 200, 200)) | promptTextColor - rgb color of the prompt text (defaults to black) | inputTextColor - rgb color of the input text (defaults to black) Returns: | userAnswer - If user presses OK, returns the text the user typed. Otherwise returns None """ dialogLeft = theRect[0] dialogTop = theRect[1] dialogWidth = theRect[2] dialogHeight = theRect[3] INSET = 30 # inset buttons from the edges of the dialog box promptText = pygwidgets.DisplayText(theWindow, (dialogLeft, dialogTop + 30), prompt, fontSize=24, width=dialogWidth, justified='center', textColor=promptTextColor) inputWidth = dialogWidth - (2 * INSET) inputText = pygwidgets.InputText(theWindow, (dialogLeft + INSET, dialogTop + 80), width=inputWidth, initialFocus=True, textColor=inputTextColor) cancelButton = pygwidgets.TextButton(theWindow, (0, 0), cancelButtonText) okButton = pygwidgets.TextButton(theWindow, (0, 0), okButtonText) okButtonRect = okButton.getRect() okButtonHeight = okButtonRect[3] okButtonWidth = okButtonRect[2] # get width xPos = dialogLeft + dialogWidth - okButtonWidth - INSET buttonsY = dialogTop + dialogHeight - okButtonHeight - 20 cancelButton.setLoc((dialogLeft + INSET, buttonsY)) okButton.setLoc((xPos, buttonsY)) # 6 - Loop forever while True: # 7 - Check for and handle events for event in pygame.event.get(): if (event.type == QUIT) or \ ((event.type == KEYDOWN) and (event.key == K_ESCAPE)): pygame.quit() sys.exit() if inputText.handleEvent(event) or okButton.handleEvent(event): theAnswer = inputText.getValue() return theAnswer if cancelButton.handleEvent(event): return None # 8 - Do any "per frame" actions # 9 - Clear the window area before drawing it again pygame.draw.rect(theWindow, backgroundColor, theRect) pygame.draw.rect(theWindow, DIALOG_BLACK, theRect, 1) # 10 - Draw the window elements promptText.draw() inputText.draw() cancelButton.draw() okButton.draw() # 11 - Update the window pygame.display.update()