英文:
How do i add text on an image, but at the top of the screen?
问题
I know about compound = 'center' but that only puts it in the middle of the screen. is there a way for me to place it at the top of the screen and then other labels below it, so that the background is acting like a background and not a label so I can place text on top?
代码:
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('heartBg.png')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound="center")
label.pack()
root.mainloop()
英文:
I know about compound = 'center' but that only puts it in the middle of the screen. is there a way for me to place it at the top of the screen and then other labels below it, so that the background is acting like a background and not a label so i can place text ontop?
the code:
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('heartBg.png')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound = "center")
label.pack()
root.mainloop()
答案1
得分: 1
除了 'center' 外,复合参数可以是 'bottom'、'top'、'right'、'left' 或 'none'。顺便说一句:compound 与图像有关,而不是文本。所以如果你想将文本置于顶部,请设置 compound=bottom。
英文:
In addition to 'center' the compound argument can be 'bottom', 'top', 'right', 'left', or 'none'. BTW: compound relates to the image, not the text. So if you want text at the top set compound=bottom
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
root = tk.Tk()
image = Image.open('heartBg.png')
tk_image = ImageTk.PhotoImage(image)
label = tk.Label(root, text='Some Plain Text', image=tk_image, compound = 'top')
label.pack()
root.mainloop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论