要将“返回”按钮变成图标并移到右上角,可以采取以下步骤:

  1. 首先,在导入的模块中,添加一个新的图标资源,用于表示返回按钮的图标。可以通过在文件夹中添加一个名为"return_icon.png"的图标文件来实现。

  2. 创建一个自定义的IconButton类,继承自tk.Button类。在构造函数中,通过配置text属性为空字符串,compound属性为tk.LEFT,使按钮只显示图标而不显示文本。

  3. 在scan函数中,创建一个新的Toplevel窗口,并设置其标题为"扫描病毒",大小为400x300。

  4. 使用PhotoImage加载"return_icon.png"图标文件,并将其赋值给return_icon变量。

  5. 创建一个IconButton对象return_button,将其父窗口设置为scan_window,指定图标为return_icon,命令为scan_window.destroy,背景颜色为scan_window的背景颜色,高亮厚度为0,边框宽度为0。

  6. 使用place方法将return_button放置在相对于父窗口右上角的位置。可以使用relx=1和y=20来实现。

  7. 将scan_button的y坐标修改为240,使其不被窗口栏遮挡。

完整代码如下所示:

import tkinter as tk
from tkinter import PhotoImage

class IconButton(tk.Button):
    def __init__(self, master=None, **kwargs):
        super().__init__(master, **kwargs)
        self.config(text="", compound=tk.LEFT)

def scan():
    # 创建新窗口
    scan_window = tk.Toplevel(window)
    scan_window.title("扫描病毒")
    scan_window.geometry("400x300")  # 修改为400x300

    # 添加返回按钮
    return_icon = PhotoImage(file="return_icon.png")
    return_button = IconButton(scan_window, image=return_icon, command=scan_window.destroy, bg=scan_window.cget('bg'), highlightthickness=0, bd=0)
    return_button.place(relx=1, y=20, anchor="ne")

    # 扫描操作的代码
    print("正在扫描...")
    # result = pyclamav.scan_file("file_to_scan.txt")
    # if result == "OK":
    #     print("扫描完成,未发现病毒")
    # else:
    #     print("扫描完成,发现病毒")

# 创建主窗口
window = tk.Tk()
window.title("杀毒软件")
window.geometry("400x300")

# 创建背景图片
background_image = PhotoImage(file="background.png")
background_label = tk.Label(window, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# 创建图标
scan_icon = PhotoImage(file="scan_icon.png")
scan_button = IconButton(window, image=scan_icon, command=scan, bg=window.cget('bg'), highlightthickness=0, bd=0)
scan_button.place(x=10, y=240)

# 运行主循环
window.mainloop()
``
让返回变成图标并且移到右上角并且让窗口栏不遮挡返回:import tkinter as tkfrom tkinter import PhotoImageclass IconButtontkButton def __init__self master=None kwargs super__init__master kwargs selfconfigtext= com

原文地址: https://www.cveoy.top/t/topic/irxm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录