如何将句子置顶?

How do I place sentence on the top?

我正在尝试将句子(欢迎货币转换器)放在顶部,但无法成功。

import tkinter as tk

my_window = tk.Tk()
photo = tk.PhotoImage(file='currency conventer.png')
background_window = tk.Label(my_window,
                          text='Welcome\nCurrency Converter',
                          image=photo,
                          compound=tk.CENTER,
                          font=('Calibri',20,'bold italic'),
                          fg='black')
background_window.pack()
my_window.mainloop()

两件事,

  1. 您需要使用 compound=tk.BOTTOM 以便图片位于您的文字下方。

  2. 如果您的图片太大,您需要调整它的大小以使其不会"push"您的文字超出屏幕顶部。

试试这个:

import tkinter as tk
from PIL import Image, ImageTk

my_window=tk.Tk()
image = Image.open('currency conventer.png')
image = image.resize((250, 250), Image.ANTIALIAS) # resize image to that it fits within the window. If the image is too big, it will push your new label off the top of the screen
photo=ImageTk.PhotoImage(image)
background_window=tk.Label(my_window,
                          text='Welcome\nCurrency Converter',
                          image=photo,
                          compound=tk.BOTTOM, # put the image below where the label will be
                          font=('Calibri',20,'bold italic'),
                          fg='black')
background_window.place(x=0,y=1000)
background_window.pack()
my_window.mainloop()

这里我使用 Pillow 导入图像,调整大小,然后将其传递给 ImageTk。要安装枕头,请按照这些说明进行操作。 How to install Pillow on Python 3.5?

关于这方面的更多帮助,请随时向我咨询。它对我有用,我很想知道它是否对你有用!