吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1433|回复: 7
收起左侧

[求助] Python中如何给GUI窗口增加背景图片

[复制链接]
liuliang0906 发表于 2025-4-24 10:30
各位大佬好,如何才能正确设置背景图片,并在图片上显示控件。
尝试过background使用"#00000000" "transparent",结果显示为黑色背景。
修改背景在框架的位置,要么就是背景图片在最顶层,所有控件都不显示;要么就是图片加载成功但是不显示

t1.png t2.png

[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import fitz  
from PIL import Image
import io
import img2pdf  
import threading
import os  

# 自定义样式
def set_style():
    style = ttk.Style()
    
    # 基础样式
    style.configure("TButton", 
        padding=8,
        font=("Segoe UI", 9),
        foreground="black",
        background="#1976D2",
        relief="flat",
        borderwidth=2)
    style.map("TButton",
        background=[("active", "#1565C0"), ("pressed", "#0D47A1")],
        foreground=[("disabled", "#888888")])

    # 输入框样式
    style.configure("TEntry",
        padding=10,
        font=("Segoe UI", 9),
        fieldbackground="#F5F5F5",
        bordercolor="#BDBDBD",
        relief="solid")
    style.map("TEntry",
        fieldbackground=[("focus", "#E3F2FD")])

    # 标签样式
    style.configure("TLabel",
        font=("Segoe UI", 9),
        background="#FFFFFF",
        padding=5)

    # 框架样式
    style.configure("TLabelframe",
        font=("Segoe UI", 9, "bold"),
        borderwidth=2,
        relief="groove")
    style.configure("TLabelframe.Label",
        font=("Segoe UI", 9))

def select_input_file():
    file_path = filedialog.askopenfilename(
        title="选择PDF文件",
        filetypes=[("PDF文件", "*.pdf"), ("所有文件", "*.*")]
    )
    if file_path:
        input_file_entry.delete(0, tk.END)
        input_file_entry.insert(0, file_path)

# 创建主窗口
root = tk.Tk()
root.title("扫描文件压缩工具")
root.geometry("560x480")
root.configure(bg="#FFFFFF")
root.minsize(560, 480)

# 设置样式
set_style()

# 创建控件框架
main_frame = ttk.Frame(root, padding=24)
main_frame.grid(row=0, column=0, sticky='nsew')

# 输入文件组
input_frame = ttk.LabelFrame(main_frame, text=" PDF文件 ", padding=12)
input_frame.grid(row=0, column=0, columnspan=3, sticky='ew', pady=8)

ttk.Label(input_frame, text="文件路径:").grid(row=0, column=0, sticky='w')
input_file_entry = ttk.Entry(input_frame, width=40)
input_file_entry.grid(row=0, column=1, padx=8, sticky='ew')
ttk.Button(input_frame, text="选    择", command=select_input_file).grid(row=0, column=2, padx=8)


# 配置网格布局
main_frame.columnconfigure(1, weight=1)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

root.mainloop()

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ylhtzj 发表于 2025-4-24 10:39
感谢卤煮,代码借了
307921917 发表于 2025-4-24 10:57
本帖最后由 307921917 于 2025-4-24 10:59 编辑

[Python] 纯文本查看 复制代码
from tkinter import Tk, Label, Button, Entry
from PIL import Image, ImageTk
import os

# 获取当前脚本所在目录
current_dir = os.path.dirname(os.path.abspath(__file__))

def on_button_click():
    user_input = entry.get()
    print(f"用户输入:{user_input}")

# 创建主窗口
window = Tk()
window.title("带有背景图片的窗口")
window.geometry("400x300")

# 加载背景图片
background_image_path = os.path.join(current_dir, "111.jpg")  # 替换为你的图片路径
try:
    image = Image.open(background_image_path)
    background_image = ImageTk.PhotoImage(image.resize((400, 300)))  # 调整图片大小以适应窗口

    # 创建标签并设置背景图片
    background_label = Label(window, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    # 在窗口上添加其他控件
    entry = Entry(window)
    entry.pack(pady=20)

    button = Button(window, text="点击我", command=on_button_click)
    button.pack(pady=5)

except Exception as e:
    print(f"加载图片失败:{e}")
    # 如果图片加载失败,设置一个简单的纯色背景
    window.configure(bg="lightblue")

# 进入主循环
window.mainloop()



 楼主| liuliang0906 发表于 2025-4-24 11:22
307921917 发表于 2025-4-24 10:57
[mw_shl_code=python,true]from tkinter import Tk, Label, Button, Entry
from PIL import Image, ImageT ...

非常感谢!  刚才尝试用Canvas也可以实现
请问Button 按钮可以设置成透明吗?
wapjsx 发表于 2025-4-24 13:56
liuliang0906 发表于 2025-4-24 11:22
非常感谢!  刚才尝试用Canvas也可以实现
请问Button 按钮可以设置成透明吗?

可以参考下面网址。
https://blog.csdn.net/weixin_62651706/article/details/125911027
space218 发表于 2025-4-24 13:57
这个有啥用?
307921917 发表于 2025-4-24 14:08
liuliang0906 发表于 2025-4-24 11:22
非常感谢!  刚才尝试用Canvas也可以实现
请问Button 按钮可以设置成透明吗?

[Python] 纯文本查看 复制代码
from tkinter import Tk, Button
from PIL import Image, ImageTk
import os

# 获取当前脚本所在目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 创建主窗口
window = Tk()
window.title("透明按钮示例")
window.geometry("400x300")

# 加载背景图片
background_image_path = os.path.join(current_dir, "111.jpg")  # 替换为你的图片路径
try:
    background_image = Image.open(background_image_path)
    background_image = ImageTk.PhotoImage(background_image.resize((400, 300)))
    background_label = Label(window, image=background_image)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

except Exception as e:
    print(f"加载图片失败:{e}")
    window.configure(bg="lightblue")

# 加载按钮图片
button_image_path = os.path.join(current_dir, "button.png")  # 替换为你的按钮图片路径
try:
    button_image = Image.open(button_image_path)
    button_image = ImageTk.PhotoImage(button_image.resize((100, 30)))  # 调整按钮图片大小

    # 创建按钮,使用图片作为按钮的显示内容
    button = Button(
        window,
        image=button_image,
        borderwidth=0,
        highlightthickness=0,
        activebackground=window.cget("bg")  # 按钮激活时的背景颜色
    )
    button.image = button_image  # 防止图片被垃圾回收
    button.pack(pady=20)

except Exception as e:
    print(f"加载按钮图片失败:{e}")

    # 如果按钮图片加载失败,创建一个普通按钮
    button = Button(
        window,
        text="点击我",
        bg=window.cget("bg"),
        activebackground=window.cget("bg"),
        borderwidth=0,
        highlightthickness=0
    )
    button.pack(pady=20)

# 进入主循环
window.mainloop()
 楼主| liuliang0906 发表于 2025-4-24 14:36
307921917 发表于 2025-4-24 14:08
[mw_shl_code=python,true]from tkinter import Tk, Button
from PIL import Image, ImageTk
import os ...

懂了        button图片不存在就直接透明啦     感谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-8-12 18:08

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表