簡體   English   中英

圖像未在 tkinter canvas 上繪制

[英]Image not getting drawn on tkinter canvas

我想在綠色 canvas 上顯示一個背包圖像。canvas 的高度和寬度為 250X250 像素。

在此處輸入圖像描述

圖像的大小為 260X280 像素。

在此處輸入圖像描述

當我嘗試執行下面的代碼時,我得到 output,如上面的屏幕截圖1所示。 代碼文件和圖像文件的位置相同。

from tkinter.font import BOLD
from tkinter import *
import tkinter
from PIL import Image, ImageTk

root = Tk()

def draw():
    global canvas
    root.geometry('1080x720')
    root.state('zoomed')

    canvas = Canvas(root,bg='black',highlightthickness=0)
    canvas.pack(fill=tkinter.BOTH, expand=True)

    sw = root.winfo_screenwidth()
    sh = root.winfo_screenheight()
    canvas.create_line(int(sw*0.0000),int(sh*0.1736),int(sw*0.6510),int(sh*0.1736),fill='white')
    canvas.create_line(int(sw*0.6510),int(sh*0.0000),int(sw*0.6510),int(sh*1.0000),fill='white')
    canvas.create_line(int(sw*0.6510),int(sh*0.1157),int(sw*1.0000),int(sh*0.1157),fill='white')
    canvas.create_line(int(sw*0.6510),int(sh*0.8101),int(sw*1.0000),int(sh*0.8101),fill='white')

    UI_frame1 = Frame(canvas,bg='black',width=int(sw*0.6510),height=int(sh*0.1580))
    canvas.create_window(0,0, anchor=NW,window=UI_frame1)
    
    N = Label(UI_frame1,text='N',bg ='black',fg='white',font=(12))
    N.grid(row=0,column=0, padx=139,pady=22)
    weights = Label(UI_frame1,text='Weights',bg ='black',fg='white',font=(12))
    weights.grid(row=0,column=1,padx=140,pady=22)
    val = Label(UI_frame1,text='Values',bg ='black',fg='white',font=(12))
    val.grid(row=0,column=2,padx=140,pady=22)
    
    n = Entry(UI_frame1,bg='white',width=4,font=(12))
    n.grid(row=1,column=0,padx=50,pady=17)
    value_arr = Entry(UI_frame1,bg='white',font=(12))
    value_arr.grid(row=1,column=1,padx=50,pady=17)
    weight_arr = Entry(UI_frame1,bg='white',font=(12))
    weight_arr.grid(row=1,column=2,padx=50,pady=17)

    Label(canvas,text='i',bg='black',fg='white',font=(14)).place(x=150,y=185)
    i = Label(canvas,text="  i  ",bg='white',font=(12)).place(x=175,y=185)
    Label(canvas,text='j',bg='black',fg='white',font=(14)).place(x=525,y=185)
    j = Label(canvas,text="  j  ",bg='white',font=(12)).place(x=550,y=185)


    table = Canvas(canvas,bg='red',width=600,height=450)
    table.place(x=75,y=300)

    w = int(600/8)
    h = int(450/5)
    x=0
    y=0
    for r in range(5):
        for c in range(8):
            table.create_rectangle(x,y,x+w,y+h)
            x+=w
        y+=h
        x=0
    
    UI_frame2 = Frame(canvas,bg='blue',width=250,height=250)
    canvas.create_window(835,630, anchor=CENTER,window=UI_frame2)

    image_c = Canvas(UI_frame2,bg='green',highlightthickness=0,width=250,height=250)
    image_c.grid(row=0,column=0,padx=0,pady=0)

    photo = ImageTk.PhotoImage(file ='knapsack.png')
    image_c.create_image(835,630,image=photo,anchor=NW)


draw()
root.mainloop()

我想用背包的單個圖像覆蓋整個綠色 canvas。 運行 GUI 時我沒有任何錯誤。 任何人都可以幫助我,我將非常感激。

有兩個問題:

第一的:

您在 position (835,630)中顯示,但 canvas 僅顯示(250, 250) -(左上角為(0,0) ) - 因此photo就位,您可以看到。

第二:

PhotoImage()中存在錯誤,當將圖像分配給局部變量時,它會從 memory 中刪除圖像。 解決方案之一是使用global photo將其分配給全局變量。

def draw():
    global canvas
    global photo   # use global variable
    
    #... code ...

    photo = ImageTk.PhotoImage(file='knapsack.png')
    image_c.create_image(0, 0,image=photo, anchor=NW)   # position (0,0)

Doc(在 archive.org 上): PhotoImage - 請參閱文檔底部的Note

在此處輸入圖像描述

其他問題可能是圖像在 object 附近有較大的(透明)邊距,它可能會在與您預期不同的地方顯示 object。
屏幕截圖顯示 position (0,0)中的photo ,但 object 位於綠色 canvas 的中心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM