0

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

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

1 回答 1

0

两件事情,

  1. 您需要使用compound=tk.BOTTOM,以便图像将低于您的文本。

  2. 如果您的图像太大,您需要调整它的大小,以免它不会将您的文本“推”出屏幕顶部。

尝试这个:

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。要安装枕头,请按照这些说明进行操作。如何在 Python 3.5 上安装 Pillow?

随时向我寻求有关此方面的更多帮助。它对我有用,我很想知道它是否对你有用!

于 2019-10-07T15:33:56.617 回答