Tkinter 图片和几何创建

huangapple go评论73阅读模式
英文:

Tkinter images and geometry creation

问题

以下是您提供的代码的翻译部分:

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

app = Tk()
app.title("欢迎")
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="欢迎来到我们的星系",
               font=("Times New Roman", 24))

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

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

app.mainloop()

然而,当我运行它时,我希望星星位于背景图像之上。有解决方案吗?谢谢。

英文:

I've tried to do a star fractal drawing a star using tkinter and putting an image as a background.

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()

That's the code

However when i run it i want the star to be upon the background image. Any solutions for this ? Thanks

答案1

得分: 1

你需要将图像创建为画布对象,而不是标签。

例如,这将图像添加到画布的左上角,并在其上绘制:

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

You need to create the image as a canvas object rather than as a label.

For example, this add the image to the top-left corner of the canvas, with the drawing on top of it:

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

答案2

得分: 0

你明确地错过了@BryanOakley提供的那一行代码(+1),但总体上你有太多冗余的代码(例如,两个根窗口,两个不同的几何管理器):

import tkinter as tk
from PIL import ImageTk, Image

canvas_width = 800
canvas_height = 800

root = tk.Tk()
root.title("Welcome")

img = Image.open(r'C:\Users\Stefa\Downloads\galaxy.jpeg')
bg = ImageTk.PhotoImage(img)

label = tk.Label(root, text="WELCOME TO OUR GALAXY", font=("Times New Roman", 24))
label.pack(pady=50)

can_widgt = tk.Canvas(root, width=canvas_width, height=canvas_height)
can_widgt.pack()

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

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

root.mainloop()
英文:

You're specifically missing the line of code that @BryanOakley provides (+1) but in general you have too much redundant code (e.g. two roots, two different geomentry managers):

import tkinter as tk
from PIL import ImageTk, Image

canvas_width = 800
canvas_height = 800

root = tk.Tk()
root.title("Welcome")

img = Image.open(r'C:\Users\Stefa\Downloads\galaxy.jpeg')
bg = ImageTk.PhotoImage(img)

label = tk.Label(root, text="WELCOME TO OUR GALAXY", font=("Times New Roman", 24))
label.pack(pady=50)

can_widgt = tk.Canvas(root, width=canvas_width, height=canvas_height)
can_widgt.pack()

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

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

root.mainloop()

huangapple
  • 本文由 发表于 2023年2月18日 01:32:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定