如何将文本添加到图像顶部?

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

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

huangapple
  • 本文由 发表于 2023年5月13日 18:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242301.html
匿名

发表评论

匿名网友

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

确定