ich möchte ein Bild in einem Canvas anzeigen. Das klappt gut. Wenn ich aber die gleichen Programzeilen über eine Funktion aufrufe, wird das Bild nicht angezeigt. Wenn aber ein Programmfehler (hier x=y) auftritt, wird das Bild angezeigt.
canvas.create_text oder canvas.create_line funktionieren einwandfrei.
Code: Alles auswählen
# Import the required Libraries
from tkinter import *
from PIL import Image, ImageTk
def formanz(canvas, nkorr):
img=Image.open("bild.jpg")
img=img.resize((100,180), Image.LANCZOS)
img=ImageTk.PhotoImage(img)
canvas.create_image(50+nkorr, 50, anchor="nw", image=img)
canvas.create_text(5+nkorr,15, text="Text",anchor="w",fill="blue")
#x=y
# Create an instance of tkinter frame
root = Tk()
# Set the geometry of tkinter frame
root.title("Formulargenerator")
root.geometry("704x400+20-20")
fr=Frame(root)
fr.place(x =1, y =1, width=700, height=390)
# Create a canvas widget
canvas= Canvas(fr, width=700, height=390,bg="beige")
canvas.place(x =2, y =30)
# Load an image
img=Image.open("bild.jpg")
img=img.resize((100,180), Image.LANCZOS)
img=ImageTk.PhotoImage(img)
# Add image to the Canvas Items
canvas.create_image(50, 50, anchor="nw", image=img)
canvas.create_text(5,15, text="Text",anchor="w",fill="blue")
formanz(canvas,300)
root.mainloop()