画布背景图像不适合整个窗口

2024-06-26 17:54:29 发布

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

我是TkinterGUI新手,尝试使用canvas为顶级窗口创建背景。我尝试通过expand=TRUE使画布与整个窗口的大小相同。 但是,它不能很好地工作,并且图像的大小与窗口的大小不一样。有人能帮我解决那个问题吗? 顶级窗口是overviewWindow,它位于函数createOverviewWindow中。 这是我的代码: 没有输出图像,因为很遗憾我无法添加图像。我得到的窗口在左上角有背景图像,但尺寸较小

    from tkinter import *
from tkinter import ttk, font, messagebox
from PIL import ImageTk, Image
import os


root = Tk()
root.title("Decoder of ultrasound images to detect colon tumors")
# Adding window icon
root.iconbitmap('afekaImage.ico')

rootWidth, rootHeight = 600, 600

screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()

topLeftPosition = (screenWidth / 2 - rootWidth / 2, screenHeight / 2 - rootHeight / 2)

# Configure window size
root.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')


# open doc file
def openDocFile():
    os.startfile("FinalProject.docx")


# adjusting background image to fit window
def adjustBackgroundImage(event):
    label = event.widget
    # avoid garbage collection option 1
    # global resizedBackgroundImage, newBackgroundImage
    # ----------
    width = event.width
    height = event.height
    resizedBackgroundImage = copyImage.resize((width, height))
    newBackgroundImage = ImageTk.PhotoImage(resizedBackgroundImage)
    label.config(image=newBackgroundImage)
    # avoid garbage collection option 2
    label.image = newBackgroundImage
    # ----------


def createUserManualWindow(button_userManual):
    # global image1
    userManualWindow = Toplevel(root)
    userManualWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
    userManualWindow.iconbitmap('afekaImage.ico')
    image1 = ImageTk.PhotoImage(Image.open('background.jpg'))
    label1 = ttk.Label(userManualWindow, image=image1)
    label1.pack()
    label1.bind('<Configure>', adjustBackgroundImage)
    label1.pack(fill=BOTH, expand=YES)

    def activateButtonUserManual():
        button_userManual.configure(state="normal")
        userManualWindow.destroy()

    button_userManual.configure(state="disabled")
    button_exit_userManualWindow = Button(userManualWindow, text="Exit", font=fontStyle,
                                          command=lambda: [userManualWindow.destroy(), activateButtonUserManual()])
    button_exit_userManualWindow.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
    # will occurs only when esc pressed
    userManualWindow.protocol("WM_DELETE_WINDOW", activateButtonUserManual)
    # ----------


def createOverviewWindow(button_overview):
    global image1, canvas
    overviewWindow = Toplevel(root)
    overviewWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
    overviewWindow.iconbitmap('afekaImage.ico')
    canvas = Canvas(overviewWindow, width=600, height=600)
    canvas.pack(fill=BOTH, expand=TRUE)
    image1 = ImageTk.PhotoImage(Image.open('background.jpg'))
    canvas.create_image(0, 0, image=image1, anchor='nw')


image = Image.open('background.jpg')
copyImage = image.copy()
backgroundImage = ImageTk.PhotoImage(image)
label = ttk.Label(root, image=backgroundImage)
label.bind('<Configure>', adjustBackgroundImage)
label.pack(fill=BOTH, expand=YES)

fontStyle = font.Font(family="Segoe Script", size=10, weight=font.BOLD)

# Create buttons
button_userManual = Button(root, text="USER MANUAL", command=lambda: createUserManualWindow(button_userManual), font=fontStyle)
button_userManual.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)

button_overview = Button(root, text="OVERVIEW", command=lambda: createOverviewWindow(button_overview), font=fontStyle)
button_overview.place(relx=0.4, rely=0.4, relwidth=0.2, relheight=0.1)

button_openDocFile = Button(root, text="DOC FILE", font=fontStyle, command=openDocFile)
button_openDocFile.place(relx=0.4, rely=0.6, relwidth=0.2, relheight=0.1)

button_quit = Button(root, text="Exit", font=fontStyle, command=root.destroy)
button_quit.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)

root.mainloop()


Tags: imagebuttonrootlabelintcanvasfontimagetk
1条回答
网友
1楼 · 发布于 2024-06-26 17:54:29

你放的图像对于画布来说太大了。这就像将8K图像放入720p显示器。这根本不合适。因此,解决方案是

a)调整图像大小,使其适合画布(我看到您有PIL模块,因此它应该很快)

b)更改画布大小,使其适合图像

相关问题 更多 >