I was working on a space shooter game when I can across this problem:
File "C:\Users\willi\PycharmProjects\Pygame Testing\main.py", line 15, in <module>
icon = pygame.image.load("Images", "boss.png")
FileNotFoundError: No file 'Images' found in working directory 'C:\Users\willi\PycharmProjects\Pygame Testing'.
I am working in PyCharm where i created a directory called "Images" and I put all my icons and enemies. I wanted to put the boss as the icon but it didn't work. Please help me :(
There are two things to note here:
You do not need to specify the first parameter to be "Images". Just the path to the file will do (make sure that the path is relative to your current directory.) You can read up more about the pygame.image.load function here
PyGame sometimes tends to misbehave with normal variables. It is always a good idea to make them global in the main scope.
pygame.image.load takes in a filename as the first argument
'Images' is not a filename. Use icon = pygame.image.load("Images/boss.png")
(Read the docs here)
Related
I am trying to learn how to add images to tkinter (using a lot of youtube codemy videos). I have a kingdom management game I have been writing where I have been able to successfully add images as labels and as buttons and have them update as you progress through seasons of time in game.
However, when I started a new py file to work on a separate part of the code, suddenly Pycharm could not find any of the images and I got a:
"tkinter.TclError: couldn't open "summer.png": no such file or directory".
So, I copied my code that worked in the main instance in a separate py file and it still does not work and I get the same error. So, for whatever reason, in one file the same code is able to be used and it finds the png but in a second instance (with the "same" code) it cannot. Also, hovering over the image path allows me to see the image in Pycharm so I know it is still there (the images are in the same folder as my python project). I am wondering if there is something simple I am not seeing. All the below image paths work in my main program but it fails when I attempt to replicate it.
from tkinter import *
import pickle
import random
from pathlib import Path
root = Tk()
root.title("The Crumbling Kingdom")
root.geometry("1300x800")
bgsummer = PhotoImage(file="summer.png")
worldmap = PhotoImage(file="map1.png")
bgspring = PhotoImage(file="spring.png")
bgwinter = PhotoImage(file="winter.png")
bgfall = PhotoImage(file="fall.png")
worldmap = PhotoImage(file="map1.png")
table = PhotoImage(file="table.png")
side = PhotoImage(file="side.png")
backgroundimage = bgspring
my_background=Label(root, image= backgroundimage)
my_background.place(x=0,y=0, relwidth = 1, relheight =1)
I am having trouble searching for the method to assign to a Push Button the ability to choose a directory. I found this how to have a directory dialog in Pyqt, but I am still unsure about the method.
For example, I have a push button called new_directory and I have this code self.new_directory.clicked.connect(self.pick_new). What do I need to put into the function pick_new so that when new directory is clicked I can choose a new directory and have this stored in a variable?
Thanks!
I think this might help you.
With this you can get the directory.
def pick_new():
dialog = QtGui.QFileDialog()
folder_path = dialog.getExistingDirectory(None, "Select Folder")
return folder_path
I am making a program to manage a database and i need a nice way of choosing the save locations of files within the program. I was wondering if it is possible to open windows explore from my program, select a folder to save a file in, enter the file name and return the file path as a string to the main program.
Look up the Tkinter options in tkFileDialog
I don't think you necessarily need to put together a fully working GUI, you can simply call the method and select a folder location and then use that selected location in your code.
A simplistic example would be:
import Tkinter, tkFileDialog
root = Tkinter.Tk()
x = tkFileDialog.askopenfilename() # Can pass optional arguments for this...
root.destroy()
Is there a way to add an icon to a Gtk.ToolButton (Gtk3 using PyGi for Python) to add to the GTK+3 toolbar ?
Below is my code:
self.addfile = Gtk.ToolButton()
self.addfile.set_label("Add File")
self.addfileimg = Gtk.Image()
self.addfileimg.show()
self.addfileimg.set_from_file(self.get_resource("img/file.png"))
self.addfile.set_icon_widget(self.addfileimg)
self.addfile.connect("clicked", self.on_open_file)
Note: The get_resource() method digs into the local working folder for the resource path and this method is assumed working in this context.
I tried the code written above using PyGi. The image file is valid and everything is working but the image doesn't appear.
There is a method of the GtkToolButton class named set_icon_widget, taking a GtkWidget instance as its operand. GtkImage is a container that holds an image. Thus:
myToolButton.set_icon_widget(myImageWidget)
Whenever i try and run my pygame program in which I'm trying to load some images I always get plain "pygame.error" and nothing else.
I have imported pygame aswell as pygame.locals. At first i thought that it was because it couldnt find my files but whenever I change the name of a file to something thats incorrect it says "couldnt open image" so i guess that it really does find the file but fails when trying to load it. the startup() function is the first thing that is run from main. and i've also tried to do a simple grass=pygame.image.load('grass.png') with the grass.png file in the same directory as the python file
thankful for all the help i can get
..... line 41, in startup
surroundingsdict = {'gräs': pygame.image.load(os.path.join('images', 'grass.png')).convert_alpha(),
pygame.error
def startup():
pygame.init()
p1=Player.Player(int(random.random()*mapstorlek),int(random.random()*mapstorlek))
vh=ViewHandler.ViewHandler()
surroundingssdict = {'grass': pygame.image.load(os.path.join('images','grass.png')).convert(),
'mount': pygame.image.load(os.path.join('images', 'mount.png').convert)()}
playerdict={'p1': pygame.image.load(os.path.join('images','player.png')).convert()}
pygame.error is usually accompanied by a message with more details about what went wrong, like the 'couldn't open image' you get when you try a non-existant image. Are you sure you get no more info? Can't be sure what's wrong, but I'll throw out two suggestions:
Do you call pygame.display.set_mode to set a display surface before loading the images? You might be doing that in your ViewHandler class, but if not the convert() will fail since it changes the pixel format of the surface to match the display, which it of course can't do if there is no display active.
Another possibility is that you don't have full image support in pygame and can't load .png. Have you tried loading other formats? Try calling pygame.image.get_extended() to see if your pygame is built with extended image support. Even if it returns True there is no guarantee that you can load .png, but at least it's more likely to be something else.