English 中文(简体)
Error在试图展示含有由另一个模块在Stattkinter和Avhury中制作的形象的植被时
原标题:Error when trying to display a widget containing an image that is created by another module in customtkinter, Python

I m trying to display an a widget containing an image in the window created by my main script, by calling a function from another module. But I m getting the error:

•tkinter.TclError:形象“pyimage1”

这种傲慢只会产生含有图像的植被。

主要内容。 创建窗口和从模块中接通功能的文字。

from customtkinter import *
import image_script

window = CTk()
window.geometry("1400x1000")


image_script.call_image()


window.mainloop()

含有产生和显示植被功能的图像。

from customtkinter import *
from PIL import Image


def call_image():
    from main import window
    image_example = CTkImage(Image.open("images/icon_example.png"), size=(150, 100))
    image_label = CTkLabel(window, image=image_example)
    image_label.place(relx=0.5, rely=2, anchor="center")
问题回答

只有更新的定制模块支持直接从图像道路上打开形象。 但是,这一更新的版本与金星标准图书馆同时出现。 需要安装。

与安装假装同时出现的定制器模块是旧版本。 它可以从这条道路上直接开放。 它只能处理照片。 此外,旧版本的用意是将图像添加到纽顿和所有地方。

为了处理除纽托克以外的光电,你不需要定制器模块。 你们能够用警棍做。 消费物价指数(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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签