python-3.x Tkinter图像和几何图形创建

hfyxw5xn  于 2023-02-20  发布在  Python
关注(0)|答案(1)|浏览(136)

我试过用tkinter画一个星星,并把一张图片作为背景。

from tkinter import *
from PIL import ImageTk, Image  
import tkinter as tk

app = Tk()
app.title("Welcome")
img =Image.open('C:\\Users\\Stefa\\Downloads\\galaxy.jpeg')
bg = ImageTk.PhotoImage(img)
canvas_width=800
canvas_height=800
master = tk.Tk()

label = Label(app, image=bg)
label.place(x = 0,y = 0)
label2 = Label(app, text = "WELCOME TO OUR GALAXY",
               font=("Times New Roman", 24))

label2.pack(pady = 50)
app.geometry(f"{canvas_width}x{canvas_height}")
can_widgt=Canvas(app, width=canvas_width, height= canvas_height)
can_widgt.pack()

points=[200,20,80,396,380,156,20,156,320,396]
can_widgt.create_polygon(points, outline='red',fill='cyan', width=6)

app.mainloop()

这就是密码
然而,当我运行它,我希望星星是在背景图像。有什么解决办法吗?谢谢

vq8itlhq

vq8itlhq1#

您需要将图像创建为画布对象,而不是标签。
例如,这会将图像添加到画布的左上角,图形位于其上:

can_widgt.create_image(0, 0, anchor="nw", image=bg)

相关问题