为什么这个pysimplegui不能正常工作

2024-09-28 21:04:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我运行这段代码时,python应用程序会打开,但它只显示灰色和绿色。我在开始时添加的布局都不起作用。我是一个初学者,这是我第一次使用PYSIMPLEGUI,我不知道问题的根源,希望你们能帮我解决它。当您键入答案时,如果您提交了解决问题的代码和其他代码,就像您提交了解决问题的整个代码,然后解释问题是什么,我们将不胜感激


Tags: thekeyimageeventforcloseifpng
1条回答
网友
1楼 · 发布于 2024-09-28 21:04:14

长代码和无关键问题可能会阻止人们帮助您

你的代码中有很多问题,可能因为没有真正运行你的代码而遗漏了一些东西

  1. 如果所有长行都被拆分,则可能会得到错误的python语句
  2. ^不带选项的{}元素enable_events=True在单击时不会生成事件
  3. 对于大多数For-your-while循环,您不能在窗口关闭后循环并再次读取它
    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()

也许像这样

    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. 不应该在函数Restart中调用函数Restart,而且看起来这不是必需的,而且长/短时间后可能会出现内存不足的情况

相关问题 更多 >