使用tkinter开发GUI添加图片遇AttributeError错误求助
Hey there! Let's break down why you're hitting that annoying error when trying to load your meme image, and fix it up quick.
The Root Cause
This error comes down to a naming conflict between two of your imports:
- You pulled in PIL's
Imageclass withfrom PIL import Image, ImageTk - Then you used
from tkinter import *, which dumps all tkinter components into your global namespace—including tkinter's ownImageclass.
So when you call Image.open(), Python is trying to use tkinter's Image (which doesn't have an open method at all) instead of the PIL Image class you actually need.
Quick Fixes
You've got two simple ways to resolve this clash:
1. Ditch the Wildcard Import (Recommended)
Wildcard imports (*) are risky because they cause exactly this kind of naming overlap. Stick with your existing import tkinter as tk and remove the from tkinter import * line entirely:
# Remove this line: from tkinter import * import tkinter as tk from PIL import Image, ImageTk
2. Alias PIL's Image
If you really want to keep from tkinter import *, give PIL's Image a unique alias so it doesn't get overwritten:
from PIL import Image as PilImage, ImageTk from tkinter import * # In your button function, use the alias: image = PilImage.open('pexels-photo-247932.jpg')
Bonus: Fix the New Window Issue
While we're polishing things up, instead of using tk.Tk() to create the meme window, use tk.Toplevel(). This creates a proper child window attached to your main app—using Tk() again spawns a separate application instance that can cause weird behavior down the line:
def button_command(): meme_window = tk.Toplevel() # Replace tk.Tk() with this meme_window.title('I Warned You') # You can remove meme_window.grid()—it's unnecessary for toplevel windows image = Image.open('pexels-photo-247932.jpg') photo = ImageTk.PhotoImage(image) label = tk.Label(meme_window, image=photo) label.image = photo label.pack() # Pack is simpler than place here, but place works too if you prefer
Full Corrected Code
Here's your full code with all fixes applied, ready to run:
# Nicolas Bart import tkinter as tk from PIL import Image, ImageTk window = tk.Tk() window.title('Bad Meme Generator') window.geometry('500x500') window.configure(bg='saddle brown') meme_label = tk.Label(window, text='PRESS BUTTON FOR BAD MEMES:', fg='blue4', bg='brown4', font=('comicsans', '20')) meme_label.grid(pady=25, padx=25, column=0, row=0) def button_command(): meme_window = tk.Toplevel() meme_window.title('I Warned You') image = Image.open('pexels-photo-247932.jpg') photo = ImageTk.PhotoImage(image) label = tk.Label(meme_window, image=photo) label.image = photo label.pack() button = tk.Button(window, text='Dont Do It!', command=button_command, padx=100, pady=75, font=('comicsans', '20'), bg='brown4', fg='blue4') button.grid(column=0, row=1) warning_label = tk.Label(window, text="Really shit tier memes incoming:", bg='brown4', fg='blue4', font=('comicsans', '20')) warning_label.grid(pady=75) window.mainloop()
Just double-check that your image file pexels-photo-247932.jpg is in the same folder as your script, and you should be generating those "bad memes" in no time!
内容的提问来源于stack exchange,提问作者Nicolas Bart

