-1
import PySimpleGUI as psg
import random

psg.theme('Dark2')   # Add a touch of color
# All the stuff inside your window.

def Hero():#Defining a Function for Hero

    image_joker = ('/Users/arnav/Downloads/JokerHeadshot.png')#Makes a variable for 
image type as png
    image_scarecrow = ('/Users/arnav/Downloads/scarecrowheadshot.png')#Makes a 
variable for image type as png
    image_bane = '/Users/arnav/Downloads/Baneheadshot.png'#Makes a variable for image 
type as png
    image_hulk = '/Users/arnav/Downloads/Hulkheadshot.png'#Makes a variable for image 
type as png
    image_pacman = '/Users/arnav/Downloads/PacmanHeadshot.png'#Makes a variable for 
image type as png
    image_mario = '/Users/arnav/Downloads/MarioHeadshot.png'#Makes a variable for 
image type as png
    image_random = '/Users/arnav/Downloads/Random.png'#Makes a variable for image 
type as png

    ImageButton = lambda image_filename, key:psg.Button('', 
image_filename=image_filename, image_size=(125, 125), image_subsample=2, 
border_width=0, 
key=key)#https://www.programcreek.com/python/example/116002/PySimpleGUI.Button

    layout = [ #Making a layout for our Hero Gui selection
        [psg.Text("Choose your Hero")],
        [ImageButton(image_joker, key = 'Joker'), ImageButton(image_scarecrow, key = 
'Scarecrow'), ImageButton(image_bane, key = 'Bane'), ImageButton(image_hulk, key = 
'Hulk'), ImageButton(image_pacman, key = 'Pacman'), ImageButton(image_mario, key = 
'Mario'), ImageButton(image_random, key = 'Random')],#Image Buttons for Heros
        [psg.Button("Exit")]#Button option to exit code
]
    window =psg.Window("Dungeons & Dragons-Hero", layout)#Makes a window with 
appropriate features for Hero selection.

    fileopener = open("D&D.txt", 'a')

    while True:#True loop
        event, values = window.read()#Reads the window and its values for further use
        if event ==psg.WIN_CLOSED or event == "Exit":#If the window is closed or 
"Exit" button is pressed
            break#The code will stop
        elif event == "Joker":#Else if the "Joker" button is pressed
            psg.Popup("You chose Joker to mentally destroy your foes!")#Display this 
message
            fileopener.write("Joker,")#Write this into the file
            window.close()#Close the window
        elif event == "Scarecrow":#Else if the "Scarecrow" button is pressed
            psg.Popup("You chose Scarecrow to spread fear amongst all!")#Display this 
message
            fileopener.write("Scarecrow,")#Write this into the file
            window.close()#Close the window
        elif event == "Bane":#Else if the "Bane" button is pressed
            psg.Popup("You chose Bane to brutally bash your enemies!")#Display this 
message
            fileopener.write("Bane,")#Write this into the file
            window.close()#Close the window
        elif event == "Hulk":#Else if the "Hulk" button is pressed
            psg.Popup("You chose Hulk to ruthlessly hammer your rivals!")#Display 
this message
            fileopener.write("Hulk,")#Write this into the file
            window.close()#Close the window
        elif event == "Pacman":#Else if the "Skeleton" button is pressed
            psg.Popup("You chose Pacman, ghost hunter and honorary member of the 
ghostbusters")#Display this message
            fileopener.write("Pacman,")#Write this into the file
            window.close()#Close the window
        elif event == "Mario":#Else if the "Zombie" button is pressed
            psg.Popup("You chose Mario save the 8 bit princess!")#Display this 
message
            fileopener.write("Mario,")#Write this into the file
            window.close()#Close the window
        elif event == "Random":#Else if the "Random" button is pressed
            hero_list = ["Joker,", "Scarecrow,", "Bane,", "Hulk,", "Mario,", 
"Random,"]#List of options for Random Heros
            selected = random.choice(hero_list)#Variable for what the random Enemy is
            psg.Popup("You Hero is", selected, "Your foes shall bow down before 
you!")#Display this message
            fileopener.write(selected)#Write the random hero into the file
            window.close()#Close the window        
       
        window.close()#Close all windows for Enemies function
        fileopener.close()#Close the file
Hero()#Calls the Enemies function    

def Enemies():#Function for Enemies

    image_batman = '/Users/arnav/Downloads/Batman.png'#Makes a variable for image 
type as png
    image_Scarecrow = '/Users/arnav/Downloads/thanosheadshot.png'#Makes a variable 
for image type as png
    image_Bane = 'Bane.png'#Makes a variable for image type as png
    image_Hulk = 'Hulk.png'#Makes a variable for image type as png
    image_skeleton = 'Skeleton.png'#Makes a variable for image type as png
    image_zombie = 'Zombie.png'#Makes a variable for image type as png
    image_random = 'Random.png'#Makes a variable for image type as png

    ImageButton = lambda image_filename, key:sg.Button('', 
image_filename=image_filename, image_size=(125, 125), image_subsample=2, 
border_width=0, 
key=key)#https://www.programcreek.com/python/example/116002/PySimpleGUI.Button


    layout = [#Making a layout for our Enemies Gui selection
        [sg.Text("Choose your Enemy")],
        [ImageButton(image_batman, key = 'Joker'), ImageButton(image_Bane, key = 
'Bane'), ImageButton(image_Scarecrow, key = 'Scarecrow'), ImageButton(image_Hulk, key 
= 'Hulk'), ImageButton(image_skeleton, key = 'Skeleton'), ImageButton(image_zombie, 
key = 'Zombie'), ImageButton(image_random, key = 'Random')],# Image Buttons for 
Enemies
        [sg.Button("Exit")]#Button option to exit code
    ]
    window =psg.Window("Dungeons & Dragons-Enemies", layout)#Makes a window with 
appropriate features for Enemies selection.

    f = open("D&D.txt", 'a')#Opens D&D.txt file for appending

    while True:#True loop
        event, values = window.read()#Reads the window and its values for further use
        if event ==psg.WIN_CLOSED or event == "Exit":#If the window is closed or 
 "Exit" button is pressed
            break#The code will stop
        elif event == "Joker":#Else if the "Joker" button is pressed
            psg.Popup("You chose Joker to brutally bash your foes!")#Display this 
message
            f.write("Joker,")#Write this into the file
            window.close()#Close the window
        elif event == "Scarecrow":#Else if the "Scarecrow" button is pressed
            psg.Popup("You chose Scarecrow to loot from the most secure of 
places!")#Display this message
            f.write("Scarecrow,")#Write this into the file
            window.close()#Close the window
        elif event == "Bane":#Else if the "Bane" button is pressed
            psg.Popup("You chose Bane to push into others territories!")#Display this 
message
            f.write("Bane,")#Write this into the file
            window.close()#Close the window
        elif event == "Hulk":#Else if the "Hulk" button is pressed
            psg.Popup("You chose Hulk to cause mischeivery and cast 
nightmares!")#Display this message
            f.write("Hulk,")#Write this into the file
            window.close()#Close the window
        elif event == "Skeleton":#Else if the "Skeleton" button is pressed
            psg.Popup("You chose Skeleton to attack the enemy and never 
die!")#Display this message
            f.write("Skeleton,")#Write this into the file
            window.close()#Close the window
        elif event == "Zombie":#Else if the "Zombie" button is pressed
            psg.Popup("You chose Zombie to twist the enemy onto your side!")#Display 
this message
            f.write("Zombie,")#Write this into the file
            window.close()#Close the window
        elif event == "Random":#Else if the "Random" button is pressed
            hero_list = ["Joker,", "Scarecrow,", "Bane,", "Hulk,", "Skeleton,", 
"Skeleton,"]#List of options for Random Enemies
            selected = random.choice(hero_list)#Variable for what the random Enemy is
            psg.Popup("You Enemy is", selected, "Your foes shall bow down before 
you!")#Display this message
            f.write(selected)#Write the random enemy into the file
            window.close()#Close the window        
       
        window.close()#Close all windows for Enemies function
        f.close()#Close the file
Enemies()#Calls the Enemies function

def levels():#Function for Levels

    image_level1 = 'Level1.png'#Makes a variable for image type as png
    image_level2 = 'Level2.png'#Makes a variable for image type as png
    image_level3 = 'Level3.png'#Makes a variable for image type as png
    image_random = 'Random.png'#Makes a variable for image type as png

    ImageButton = lambda image_filename, key:sg.Button('', 
image_filename=image_filename, image_size=(125, 125), image_subsample=2, 
border_width=0, 
key=key)#https://www.programcreek.com/python/example/116002/PySimpleGUI.Button

    layout = [#Making a layout for our Levels Gui selection
        [sg.Text("Choose a level")],
        [ImageButton(image_level1, key = 'Level 1'), ImageButton(image_level2, key = 
'Level 2'), ImageButton(image_level3, key = 'Level 3'), ImageButton(image_random, key 
= 'Random')],#Image Buttons for Levels
        [sg.Button("Exit")]#Button option to exit code
    ]
    window =psg.Window("Battle of the ages-Levels", layout)#Makes a window with 
appropriate features for Level selection.

    f = open("D&D.txt", 'a')#Opens D&D.txt file for appending

    while True:#True loop
        event, values = window.read()#Reads the window and its values for further use
        if event ==psg.WIN_CLOSED or event == "Exit":#If the window is closed or 
"Exit" button is pressed
            break#The code will stop
        elif event == "Level 1":#Else if the "Level 1" button is pressed
            psg.Popup("You selected Level 1")#Display this message
            f.write("Level 1")#Write this into the file
            window.close()#Close the window
        elif event == "Level 2":#Else if the "Level 2" button is pressed
            psg.Popup("You selected Level 2")#Display this message
            f.write("Level 2")#Write this into the file
            window.close()#Close the window
        elif event == "Level 3":#Else if the "Level 3" button is pressed
            psg.Popup("You selected Level 3") #Display this message
            f.write("Level 3")#Write this into the file
            window.close()#Close the window
        elif event == "Random":#Else if the "Random" button is pressed
            level_list = ["Level 1", "Level 2", "Level 3"]#List of options for Random 
levels
            selected = random.choice(level_list)#Variable for what the random Level 
is
            psg.Popup("You Level is", selected, "Go forth if you DARE!")#Display this 
message
            f.write(selected)#Write the random level into the file
            window.close()#Close the window        
       
    window.close()#Close all windows for levels function
    f.close()#Close the file
levels()#Calls the levels function    

def Restart():#Function for Restart
    layout = [#Making a kayout for our Restart Gui selection
        [sg.Text("Would you like to Restart or Quit?")],
        [sg.Button("Restart"),psg.Button("Quit")]#Buttons for Restart or Continue
    ]
    window =psg.Window("Battle of the ages-Restart or Continue", layout)#Makes a 
window with appropriate features for Restart selection.
    f = open("D&D.txt", 'a')#Opens D&D.txt file for appending

    while True:#True loop
        event, values = window.read()#Reads the window and its values for further use
        if event ==psg.WIN_CLOSED:#If the window is closed or "Exit" button is 
pressed
            break#The code will stop
        elif event == "Restart":#Else if the "Restart" button is pressed
            f.writelines("\n")#Prints a line in the file
            Hero()#Calls all function from start to end again.
            Enemies()#Calls all function from start to end again.
            NPC()#Calls all function from start to end again.
            levels()#Calls all function from start to end again.
            Restart()#Calls all function from start to end again.
            window.close()#Close the window
        elif event == "Quit":#Else if the "Quit" button is pressed
            break#The code will stop

    window.close()#Close all windows for Restart function
    f.close()#Close the file
Restart()#Calls the Restart function

When I run this code, the python app opens but it just shows grey and green. None of the layout I added at the beginning works. I am a beginner coder and it is my first time using PYSIMPLEGUI, I do not know the origin of the problem and was hoping you guys could help me solve it. When you type the answer, it would be greatly appreciated if you submitted the resolves issue with the rest of the code so like you submit the entire code with the resolved issue and then explain what the issue is.

1 Answers1

0

Long code and without key issue may stop people to help you !

There're lot of issues in your code, maybe miss something for not really running your code.

  1. All long lines splitted, it may get wrong python statements.
  2. psg.Image element without option enable_events=True will not generate event when clicked.
  3. For most for your while loops, you cannot loop it and read it again after window closed.
    window =psg.Window("Dungeons & Dragons-Hero", layout)

    fileopener = open("D&D.txt", 'a')

    while True:
        event, values = window.read()
        if event == psg.WIN_CLOSED or event == "Exit":
            break
        elif event == "Joker":
            psg.Popup("You chose Joker to mentally destroy your foes!")
            fileopener.write("Joker,")
            window.close()
        elif event == "Scarecrow":
            psg.Popup("You chose Scarecrow to spread fear amongst all!")
            fileopener.write("Scarecrow,")
            window.close()
...
        window.close()
        fileopener.close()

maybe like this

    window =psg.Window("Dungeons & Dragons-Hero", layout, enable_close_attempted_event=True)

    fileopener = open("D&D.txt", 'a')

    while True:
        event, values = window.read()
        if event == "Joker":
            psg.Popup("You chose Joker to mentally destroy your foes!")
            fileopener.write("Joker,")
            break
        elif event == "Scarecrow":
            psg.Popup("You chose Scarecrow to spread fear amongst all!")
            fileopener.write("Scarecrow,")
            break
...
    window.close()
    fileopener.close()
  1. Should not call function Restart in your function Restart, and it looks like it is not necessary, also recursive maybe out of memory after long/short time.
Jason Yang
  • 11,284
  • 2
  • 9
  • 23