Python Tkinter 创建 360x270 窗口并添加图片和文字
以下是使用 Python Tkinter 创建 360x270 窗口并添加图片和文字的示例代码:
import tkinter as tk
root = tk.Tk()
root.geometry('360x270')
# 加载图片
image_path = 'image.jpg'
image = tk.PhotoImage(file=image_path)
# 显示图片
label_image = tk.Label(root, image=image)
label_image.pack()
# 添加文字
label_temp = tk.Label(root, text='温度', font=('Arial', 16))
label_temp.place(x=55, y=62)
label_weather = tk.Label(root, text='天气', font=('Arial', 16))
label_weather.place(x=55, y=105)
root.mainloop()
代码解释:
- 导入
tkinter库,创建Tk对象,并设置窗口大小为 360x270。 - 使用
PhotoImage类加载图片,并使用Label类显示图片。 - 使用
Label类添加 '温度' 和 '天气' 文字,并使用place方法指定文字位置。 - 使用
root.mainloop()运行主循环,显示窗口。
通过以上代码,我们可以创建一个包含图片和文字的 360x270 窗口。
原文地址: https://www.cveoy.top/t/topic/nflq 著作权归作者所有。请勿转载和采集!