繁体   English   中英

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

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

我是 tkinter GUI 的新手,并尝试使用 canvas 为顶级 window 制作背景。 我试图通过 expand=TRUE 使 canvas 的大小与整个 window 的大小相同。 但是,它不能很好地工作,并且图像的大小不是 window。 谁能帮我解决这个问题? 顶层window是overviewWindow,在function createOverviewWindow中。 这是我的代码:没有 output 图像,因为不幸的是我无法添加图像。 我得到的 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()

您放置的图像对于 canvas 来说太大了。 这就像将 8K 图像放入 720p 显示器。 它根本不适合。 所以解决方案是

a)调整图像大小,使其适合 canvas(我看到你有 PIL 模块,所以应该很快)

b) 更改 canvas 尺寸使其适合图像

暂无
暂无

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

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