我怎么把句子放在最上面?

2024-09-19 03:51:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把句子(欢迎货币转换器)放在最上面,但没有成功。你知道吗

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

Tags: importtkintermyas货币windowcurrencytk
1条回答
网友
1楼 · 发布于 2024-09-19 03:51:34

两件事

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

在这里,我使用枕头导入图像,调整大小,然后把它传递给ImageTk。要安装枕头,请遵循以下说明。How to install Pillow on Python 3.5?

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

相关问题 更多 >