繁体   English   中英

image =具有变量的ImageTk.PhotoImage

[英]image = ImageTk.PhotoImage with variables

所以基本上,我想用image = ImageTk.PhotoImage()打开一个Image,并且在

image = ImageTk.PhotoImage(file="C:/Users/timol/PycharmProjects/Test1/haha.jpg")

在代码的前面,我将路径保存在一个变量中

def click():
    file = askopenfilename(initialdir='C:/Users/%s')
    directory = os.path.split(file)[0]
    print(directory)

现在,我想使用保存在“目录”中的路径放入image = ImageTk.PhotoImage(file= "directory")但这给了我很多错误。

如果您需要知道,我想这样做,因为我希望以后的用户能够上载图像并在程序中显示/使用。

这是完整的代码:

`from tkinter import *
from PIL import ImageTk, Image, ImageDraw
import PIL
import os
from tkinter import filedialog

root = Tk()
root.title("Zahlenerfassung")
root.geometry("500x400")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
def create(): command=os.system("start "+"pepsi.txt")
#def click():
    # Get the file
   # file = askopenfilename(initialdir='C:/Users/%s')
    # Split the filepath to get the directory
#  directory = os.path.split(file)[0]
#   print(directory)

def click():
     image_file_location = filedialog.askopenfilename(initialdir="C:/Users/%s")
     image = ImageTk.PhotoImage(file=image_file_location)
     canvas.create_image(50, 50, image=image, anchor=NW)

button1 = Button(topFrame, text="Bild auswerten", fg="red")
button2 = Button(bottomFrame, text="Erstelle ein Bild", fg="blue", command=lambda: create())
button3 = Button(bottomFrame, text="Lade dein Bild hoch", fg="blue", command=lambda: click())

canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)

one = Label(root, text="", fg="white")
one.pack(fill=BOTH, expand=TRUE)
root.mainloop()

您可以尝试以下方法:

from tkinter import *
from PIL import ImageTk, Image
import os
from tkinter import filedialog

root = Tk()
root.title("Zahlenerfassung")
root.geometry("500x400")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
def create():
    command=os.system("start "+"pepsi.txt")

def click():
     image_file_location = filedialog.askopenfilename(initialdir="C:/Users/%s")
     img = Image.open(image_file_location)
     canvas.image = ImageTk.PhotoImage(img.resize((200, 200), Image.ANTIALIAS))     #  this line is for resizing the image to fit canvas size
     # canvas.image = ImageTk.PhotoImage(img)   #  you have to use this line instead of the upper line if you don't want resizing
     canvas.create_image(0, 0, image=canvas.image, anchor='nw')

button1 = Button(topFrame, text="Bild auswerten", fg="red")
button2 = Button(bottomFrame, text="Erstelle ein Bild", fg="blue", command=lambda: create())
button3 = Button(bottomFrame, text="Lade dein Bild hoch", fg="blue", command=lambda: click())

canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)

button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)

one = Label(root, text="", fg="white")
one.pack(fill=BOTH, expand=TRUE)
root.mainloop()

我使用了askopenfile()及其name属性来获取文件名。 PhotoImage接受类型为Image.open()的图像文件。 我已经测试了此代码,并且图像显示在画布窗口内。

from tkinter import filedialog, Tk, Canvas, NW, NO, NONE, mainloop
import os
from PIL import ImageTk, Image


canvas = Canvas(width=200, height=200)
canvas.pack(expand=NO, fill=NONE)
image_file_loc = filedialog.askopenfile(initialdir=r'C:\Users')
file_path = image_file_loc.name
image = Image.open(file_path)
tk_image = ImageTk.PhotoImage(image)
canvas.create_image(50, 50, image=tk_image, anchor=NW)
mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM