Skip to content

Instantly share code, notes, and snippets.

@ppizarror
Created May 29, 2019 20:03
Show Gist options
  • Save ppizarror/eceeeca337d9cd010cfb52a4b7c8a4af to your computer and use it in GitHub Desktop.
Save ppizarror/eceeeca337d9cd010cfb52a4b7c8a4af to your computer and use it in GitHub Desktop.
Call command using pygame-menu, in this code we call a python script using py command
# 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_python_app():
"""
Opens python app file
"""
# You can use stdout or stderr for catching process standard output
subprocess.call([r"py", "open_file.py"])
# -----------------------------------------------------------------------------
# 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 Python App', open_python_app) # 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