This is for my programming project and I'm having difficulties trying to make my code do what I want it to do (Probably because i have never used pygame or any GUI tool). Basically for my project i wanted to make a program called "Logic Gate Simulator" where the user can drag a logic gate from the side of the screen and place it into a canvas to create a circuit, now that i started coding it, that is too difficult. So now my plan is to add images of logic gates and once the user clicks a button it displays the truth table or shows the output of the logic gate clicked
I used pygame-menu to create the main menu for this, i found the code online and tweaked it for my purpose, unfortunately the code is all over the place and does not use functions as i was expecting it to. Could someone try and explain and demonstrate how to make a button that when clicked shows the truth table or be able to show text onto it.
I've tried my best trying to clean up the code but my head starts hurting when going through it all.(ignore someone the comments most of them are waffle)
import sys
sys.path.insert(0, '../../')
import os
import pygame
import pygame_menu
# -----------------------------------------------------------------------------
# Constants and global variables
# -----------------------------------------------------------------------------
FPS = 60.0
WINDOW_SIZE = (800, 600)
sound = None # type: pygame_menu.sound.Sound
surface = None # type: pygame.Surface
main_menu = None # type: pygame_menu.Menu
# -----------------------------------------------------------------------------
# Methods
# -----------------------------------------------------------------------------
def main_background():
"""
Background color of the main menu, on this function user can plot
images, play sounds, etc.
:return: None
"""
surface.fill((40, 40, 40))
def check_name_test(value):
"""
This function tests the text input widget.
:param value: The widget value
:type value: str
:return: None
"""
print('User name: {0}'.format(value))
# noinspection PyUnusedLocal
def update_menu_sound(value, enabled):
"""
Update menu sound.
:param value: Value of the selector (Label and index)
:type value: tuple
:param enabled: Parameter of the selector, (True/False)
:type enabled: bool
:return: None
"""
if enabled:
main_menu.set_sound(sound, recursive=True)
print('Menu sounds were enabled')
else:
main_menu.set_sound(None, recursive=True)
print('Menu sounds were disabled')
def main(test=False):
"""
Main program.
:param test: Indicate function is being tested
:type test: bool
:return: None
"""
# -------------------------------------------------------------------------
# Globals
# -------------------------------------------------------------------------
global main_menu
global sound
global surface
# -------------------------------------------------------------------------
# Init pygame
# -------------------------------------------------------------------------
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'
# Create pygame screen and objects
surface = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Logic Gate Simulator')
clock = pygame.time.Clock()
# -------------------------------------------------------------------------
# Set sounds
# -------------------------------------------------------------------------
sound = pygame_menu.sound.Sound()
# Load example sounds
sound.load_example_sounds()
# Disable a sound
sound.set_sound(pygame_menu.sound.SOUND_TYPE_ERROR, None)
# -------------------------------------------------------------------------
# Create menus: Settings/Options
# -------------------------------------------------------------------------
settings_menu_theme = pygame_menu.themes.THEME_DARK.copy()
settings_menu_theme.title_offset = (5, -2)
settings_menu_theme.widget_alignment = pygame_menu.locals.ALIGN_LEFT
settings_menu_theme.widget_font = pygame_menu.font.FONT_OPEN_SANS_LIGHT
settings_menu_theme.widget_font_size = 20
settings_menu = pygame_menu.Menu(
height=600,
width=800,
onclose=pygame_menu.events.DISABLE_CLOSE,
theme=settings_menu_theme,
title='Settings'
)
settings_menu.add_button('Return to main menu', pygame_menu.events.BACK,
align=pygame_menu.locals.ALIGN_CENTER)
#mode selector
#logic gate
logic_menu_theme = pygame_menu.themes.THEME_DARK.copy()
logic_menu_theme.title_offset = (5, -2)
logic_menu_theme.widget_alignment = pygame_menu.locals.ALIGN_LEFT
logic_menu_theme.widget_font = pygame_menu.font.FONT_OPEN_SANS_LIGHT
logic_menu_theme.widget_font_size = 20
logic_menu = pygame_menu.Menu(
height=600,
width=800,
onclose=pygame_menu.events.DISABLE_CLOSE,
theme=logic_menu_theme,
title='Logic Gates',
columns = 3,
rows = 3
)
logic_menu.add_image("and.png", scale=(0.25, 0.25), scale_smooth=True, align=pygame_menu.locals.ALIGN_LEFT)
#logic_menu.add_button("AND", AND(), align=pygame_menu.locals.ALIGN_LEFT)
#logic_menu.add_vertical_margin(1)
#logic_menu.add_vertical_margin(100)
#find better or image
logic_menu.add_image("or.png", scale=(0.65, 0.65), scale_smooth=True, align=pygame_menu.locals.ALIGN_LEFT)
#logic_menu.add_vertical_margin(1)
#logic_menu.add_vertical_margin(100)
logic_menu.add_image("not.png", scale=(0.25, 0.25), scale_smooth=True, align=pygame_menu.locals.ALIGN_LEFT)
logic_menu.add_image("nor.png", scale=(0.15, 0.15), scale_smooth=True, align=pygame_menu.locals.ALIGN_CENTER)
logic_menu.add_image("exnor.png", scale=(0.15, 0.15), scale_smooth=True, align=pygame_menu.locals.ALIGN_CENTER)
logic_menu.add_image("nand.png", scale=(0.15, 0.15), scale_smooth=True, align=pygame_menu.locals.ALIGN_CENTER)
logic_menu.add_button('Back', pygame_menu.events.BACK, align=pygame_menu.locals.ALIGN_RIGHT)
#update use def
#karnough maps porb spelled it wrong
#help
help_menu_theme = pygame_menu.themes.THEME_DARK.copy()
help_menu_theme.title_offset = (5, -2)
help_menu_theme.widget_alignment = pygame_menu.locals.ALIGN_LEFT
help_menu_theme.widget_font = pygame_menu.font.FONT_OPEN_SANS_LIGHT
help_menu_theme.widget_font_size = 20
help_menu = pygame_menu.Menu(
height=600,
width=800,
onclose=pygame_menu.events.DISABLE_CLOSE,
theme=help_menu_theme,
title='Help'
)
#add text
font = pygame.font.SysFont("comicsansms", 72)
text = font.render("", True, (0, 128, 0))
help_menu.add_button('Return to main menu', pygame_menu.events.BACK,
align=pygame_menu.locals.ALIGN_CENTER)
# -------------------------------------------------------------------------
# Create menus: Main menu
# -------------------------------------------------------------------------
main_menu_theme = pygame_menu.themes.THEME_DARK.copy()
main_menu_theme.widget_offset = (0, 0.09)
main_menu_theme.title_font = pygame_menu.font.FONT_COMIC_NEUE
main_menu_theme.widget_font = pygame_menu.font.FONT_COMIC_NEUE
main_menu_theme.widget_font_size = 30
main_menu = pygame_menu.Menu(
height=600,
width=800,
onclose=pygame_menu.events.EXIT, # User press ESC button
title='Main menu',
theme=main_menu_theme,
)
#menu.add_selector('Modes :', [('Logic Gates', 1), ('Karnaugh Maps', 2)], onchange=set_mode)
main_menu.add_button('Logic Gates', logic_menu)
main_menu.add_button("Help", help_menu)
main_menu.add_button('Settings', settings_menu)
main_menu.add_selector('Menu sounds ',
[('Off', False), ('On', True)],
onchange=update_menu_sound)
main_menu.add_button('Quit', pygame_menu.events.EXIT)
# -------------------------------------------------------------------------
# Main loop
# -------------------------------------------------------------------------
while True:
# Tick
clock.tick(FPS)
# Paint background
main_background()
# Main menu
main_menu.mainloop(surface, main_background, disable_loop=test, fps_limit=FPS)
# Flip surface
pygame.display.flip()
# At first loop returns
if test:
break
if __name__ == '__main__':
main()
this is the code, i tried creating a def function so that when i click it does whats inside the function but that never worked so i removed the function.
#logic_menu.add_button("AND", AND(), align=pygame_menu.locals.ALIGN_LEFT)
please ignore the "AND()", i created a def earlier on but it never worked. thats the code im trying to find a solution.