Last active
May 29, 2019 18:17
-
-
Save ppizarror/f78cf46fd3650181f7947fd4fdd3f490 to your computer and use it in GitHub Desktop.
Code that opens a external program (.exe) using pygame-menu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
""" | |
The MIT License (MIT) | |
Copyright 2017-2019 Pablo Pizarro R. @ppizarror | |
Permission is hereby granted, free of charge, to any person obtaining a | |
copy of this software and associated documentation files (the "Software"), | |
to deal in the Software without restriction, including without limitation | |
the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
and/or sell copies of the Software, and to permit persons to whom the Software | |
is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
""" | |
# Import pygame and libraries | |
from random import randrange | |
import datetime | |
import os | |
import pygame | |
from pygame.locals import * | |
import subprocess | |
# Import pygameMenu | |
import pygameMenu | |
from pygameMenu.locals import * | |
# ----------------------------------------------------------------------------- | |
# Constants and global variables | |
ABOUT = ['PygameMenu {0}'.format(pygameMenu.__version__), | |
'Author: {0}'.format(pygameMenu.__author__), | |
PYGAMEMENU_TEXT_NEWLINE, | |
'Email: {0}'.format(pygameMenu.__email__)] | |
COLOR_BLUE = (12, 12, 200) | |
COLOR_BACKGROUND = [128, 0, 128] | |
COLOR_WHITE = (255, 255, 255) | |
FPS = 60 | |
H_SIZE = 600 # Height of window size | |
HELP = ['Press ESC to enable/disable Menu', | |
'Press ENTER to access a Sub-Menu or use an option', | |
'Press UP/DOWN to move through Menu', | |
'Press LEFT/RIGHT to move through Selectors'] | |
W_SIZE = 800 # Width of window size | |
# ----------------------------------------------------------------------------- | |
# Init pygame | |
pygame.init() | |
os.environ['SDL_VIDEO_CENTERED'] = '1' | |
# Write help message on console | |
for m in HELP: | |
print(m) | |
# Create window | |
surface = pygame.display.set_mode((W_SIZE, H_SIZE)) | |
pygame.display.set_caption('PygameMenu example') | |
# Main timer and game clock | |
clock = pygame.time.Clock() | |
timer = [0.0] | |
dt = 1.0 / FPS | |
timer_font = pygame.font.Font(pygameMenu.fonts.FONT_NEVIS, 100) | |
# ----------------------------------------------------------------------------- | |
def mainmenu_background(): | |
""" | |
Background color of the main menu, on this function user can plot | |
images, play sounds, etc. | |
""" | |
surface.fill((40, 0, 40)) | |
def open_notepad(): | |
""" | |
Opens notepad file | |
""" | |
p = subprocess.Popen([r"C:\Windows\system32\notepad.exe"]) | |
# p = subprocess.Popen([r"C:\path_to_your_program\theprogram.exe", "you", "can", "pass", "more", "arguments"]) | |
return p.wait() # wait for notepad to exit | |
# ----------------------------------------------------------------------------- | |
# Main menu, pauses execution of the application | |
menu = pygameMenu.Menu(surface, | |
bgfun=mainmenu_background, | |
enabled=False, | |
font=pygameMenu.fonts.FONT_NEVIS, | |
menu_alpha=90, | |
onclose=PYGAME_MENU_CLOSE, | |
title='Main Menu', | |
title_offsety=5, | |
window_height=H_SIZE, | |
window_width=W_SIZE | |
) | |
menu.add_option('Open Notepad', open_notepad) # Add timer submenu | |
menu.add_option('Exit', PYGAME_MENU_EXIT) # Add exit function | |
menu.enable() | |
# ----------------------------------------------------------------------------- | |
# Main loop | |
while True: | |
# Tick clock | |
clock.tick(60) | |
timer[0] += dt | |
# Paint background | |
surface.fill(COLOR_BACKGROUND) | |
# Execute main from principal menu if is enabled | |
events = pygame.event.get() | |
menu.mainloop(events) | |
# Flip surface | |
pygame.display.flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment