Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
努力在python中打开图像_Python_Image_Tkinter_Label - Fatal编程技术网

努力在python中打开图像

努力在python中打开图像,python,image,tkinter,label,Python,Image,Tkinter,Label,我尝试用python打开一个图像,代码如下: from tkinter import * from PIL import ImageTk, Image main = Tk() main.geometry("500x500") main.title("Geometry Helper") main.config(bg="Light Green") img = PhotoImage(Image.open("C:\\Users\\Le

我尝试用python打开一个图像,代码如下:

from tkinter import *
from PIL import ImageTk, Image
main = Tk()
main.geometry("500x500")
main.title("Geometry Helper")
main.config(bg="Light Green")
img = PhotoImage(Image.open("C:\\Users\\Lenovo User\\Downloads\\Geommy Dash.PNG"))
Yoeshabite = Label(main, image=img)
label1 = Label(main, text="Geometry Dash Helper", font=" Verdana 12", bg="Light Green").place(x=155,y=130)
label2 = Label(main, text="An app designed by MellowMoex", bg="Light Green").place(x=155,y=150)
eeeeeEe = Button(main, text="Search", bg="Dark Green", command="Searchj").place(x=155,y=170)
eeeeEEe = Button(main, text="Level requests", bg="Green").place(x=206,y=170)
main.mainloop()
但它标记了这个错误:

回溯(最近一次呼叫最后一次):
文件“c:\Users\Lenovo User\OneDrive-Colchester皇家文法学校\Documents\geometr dash.py”,第8行,在
Yoeshabite=标签(主,图像=img)
文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9\u 3.9.1520.0\u x64\uuuuuqbz5n2kfra8p0\lib\tkinter\\uuuuuu init\uuuuuu.py”,第3148行,在uuuuu init中__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9\u 3.9.1520.0\u x64\uuuuuuqbz5n2kfra8p0\lib\tkinter\\uuuuuu init\uuuuuuuuu.py”,第2572行,在uuuuu init中__
自我呼叫(
TypeError:\uuuuu str\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

将图像放在使用文件的同一目录中
img=PhotoImage(file='geomy Dash.PNG')
如果不起作用,请尝试使用GIF格式的图像instaead

根据帖子,您需要使用
place
方法

  • 而且,正如所评论的,它应该是
    img=ImageTk.PhotoImage(…)
    ,而不是
    img=PhotoImage(…)
我将您的图像替换为来自的
'chelsea.png'
图像(将图像放置在与Python脚本相同的文件夹中)


下面是一个应该可以工作的代码示例:

from tkinter import *
from PIL import ImageTk, Image

main = Tk()
main.geometry("500x500")
main.title("Geometry Helper")
main.config(bg="Light Green")

#img = PhotoImage(Image.open("C:\\Users\\Lenovo User\\Downloads\\Geommy Dash.PNG"))
img = ImageTk.PhotoImage(Image.open('chelsea.png'))
Yoeshabite = Label(main, image=img)

# https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter
Yoeshabite.place(x=0, y=0, relwidth=1, relheight=1)
Yoeshabite.image = img  # if you are doing this inside a function, make sure you keep a reference.

label1 = Label(main, text="Geometry Dash Helper", font=" Verdana 12", bg="Light Green").place(x=155,y=130)
label2 = Label(main, text="An app designed by MellowMoex", bg="Light Green").place(x=155,y=150)
eeeeeEe = Button(main, text="Search", bg="Dark Green", command="Searchj").place(x=155,y=170)
eeeeEEe = Button(main, text="Level requests", bg="Green").place(x=206,y=170)
main.mainloop()

以下是输出:


img=PhotoImage(…)
更改为
img=ImageTk.PhotoImage(…)
无法正常工作,您是否给了您一个错误?您还使用了
utton(…,command=“Searchj”)
。这是不正确的,因为
命令
参数只能是可调用函数/lambdaTheLizzard是正确的。这应该可以工作
img=ImageTk.PhotoImage(Image.open('name.png'))
from tkinter import *
from PIL import ImageTk, Image

main = Tk()
main.geometry("500x500")
main.title("Geometry Helper")
main.config(bg="Light Green")

#img = PhotoImage(Image.open("C:\\Users\\Lenovo User\\Downloads\\Geommy Dash.PNG"))
img = ImageTk.PhotoImage(Image.open('chelsea.png'))
Yoeshabite = Label(main, image=img)

# https://stackoverflow.com/questions/10158552/how-to-use-an-image-for-the-background-in-tkinter
Yoeshabite.place(x=0, y=0, relwidth=1, relheight=1)
Yoeshabite.image = img  # if you are doing this inside a function, make sure you keep a reference.

label1 = Label(main, text="Geometry Dash Helper", font=" Verdana 12", bg="Light Green").place(x=155,y=130)
label2 = Label(main, text="An app designed by MellowMoex", bg="Light Green").place(x=155,y=150)
eeeeeEe = Button(main, text="Search", bg="Dark Green", command="Searchj").place(x=155,y=170)
eeeeEEe = Button(main, text="Level requests", bg="Green").place(x=206,y=170)
main.mainloop()