只有更新的定制模块支持直接从图像道路上打开形象。 但是,这一更新的版本与金星标准图书馆同时出现。 需要安装。
与安装假装同时出现的定制器模块是旧版本。 它可以从这条道路上直接开放。 它只能处理照片。 此外,旧版本的用意是将图像添加到纽顿和所有地方。
为了处理除纽托克以外的光电,你不需要定制器模块。 你们能够用警棍做。 消费物价指数(PIL)向照片转换。 图像Tk。
另一个问题: 您用了地方方法,依靠2, 您先得。 依赖相对和协调。 数值在0到1之间。 如果你提到2个,它就在窗口之外。
此外,最好不要宣布形象道路为扼杀。 由于在窗户中(如果项目在窗户中使用),路由斜坡分开。 气温和骨质中则通过斜坡(/)分离。 可以通过使用python s os模块, .path.join()方法进行治疗。
因此,该法典应当:
主编:
#main.py
from tkinter import *
from image_script import call_image
window = Tk()
window.geometry("1400x1000")
call_image(window)
window.mainloop()
#image_script.py
#image_script.py
from tkinter import *
import PIL.Image as im
import PIL.ImageTk as imgtk
import os
def call_image(window):
#get current working directory
cwd = os.getcwd()
#Assuming that the images folder is in your current working directory, get image_path
img_path = os.path.join(cwd, images , icon_example.png )
#open image with PIL.Image
img = im.open(img_path)
#Resize
img = img.resize((150,100))
#Convert into Photoimage using PIL.ImageTK
image_example = imgtk.PhotoImage(img)
#image label
image_label = Label(window, image=image_example)
#the following line is important as this function will be imported in main.py
#It prevents garbage_collection
image_label.photo = image_example
#Place image and label
image_label.place(relx=0.5, rely=0.5, anchor="center")
检查了一只好米的图像并完美地开展工作。 rel=“nofollow noreferer>> 见。
Edit:
Also you can see that I ve added window argument to the function. That is because, If the function and design-part are there in a same file: When you ask the function to place the image in window, the function will know what is window variable. Because you would have mentioned window=Tk() in the same file. But since function is written in another file, the function won t know what you mean by window. So, I ve given it as an argument. And after impoting while calling the function, I ve given this window(ie Tk()) is the argument of the function.