简体   繁体   English

Canvas 背景图片不适合整个 window tkinter python

[英]Canvas background image doesnt fit the entire window tkinter python

I'm new with tkinter GUI and trying to make a background to a top-level window by using canvas.我是 tkinter GUI 的新手,并尝试使用 canvas 为顶级 window 制作背景。 I have tried to make the canvas the same size as the entire window by expand=TRUE.我试图通过 expand=TRUE 使 canvas 的大小与整个 window 的大小相同。 However, it doesn't work well and the image is not sized as the window.但是,它不能很好地工作,并且图像的大小不是 window。 can anyone help me fix that problem?谁能帮我解决这个问题? The top-level window is overviewWindow, which is in the function createOverviewWindow.顶层window是overviewWindow,在function createOverviewWindow中。 This is my code: There is no output image because unfortunately I cant add images.这是我的代码:没有 output 图像,因为不幸的是我无法添加图像。 The window I got has the background image in the top left corner but small sized我得到的 window 在左上角有背景图像,但尺寸很小

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

The image you put was too big for the canvas.您放置的图像对于 canvas 来说太大了。 It's like putting a 8K image into a 720p monitor.这就像将 8K 图像放入 720p 显示器。 It simply does not fit.它根本不适合。 So the solutions are所以解决方案是

a) resize the image so it fits in the canvas (I see you have the PIL module so it should be quick) a)调整图像大小,使其适合 canvas(我看到你有 PIL 模块,所以应该很快)

b) Change the canvas size so it fits the image b) 更改 canvas 尺寸使其适合图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM