如何将句子放在最上方?

时间:2019-10-07 14:37:43

标签: python tkinter photoimage

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

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 个答案:

答案 0 :(得分: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。要安装枕头,请遵循以下说明。 How to install Pillow on Python 3.5?

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