第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

python canvas在循環(huán)中創(chuàng)建圖像

python canvas在循環(huán)中創(chuàng)建圖像

一只萌萌小番薯 2021-06-09 18:16:27
您好,我最近開(kāi)始學(xué)習(xí) tkinter 并決定學(xué)習(xí)國(guó)際象棋棋盤游戲。下面是我的代碼:import tkinter as tkclass GameBoard(tk.Frame):    def __init__(self, parent, rows=8, columns=8, size=70, color1="white", color2="blue"):        '''size is the size of a square, in pixels'''        self.rows = rows        self.columns = columns        self.size = size        self.color1 = color1        self.color2 = color2        self.pieces = {}        canvas_width = columns * size        canvas_height = rows * size        tk.Frame.__init__(self, parent)        self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,                                width=canvas_width, height=canvas_height, background="bisque")        self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)root = tk.Tk()board = GameBoard(root)board.pack(side="top", fill="both", expand="true", padx=4, pady=4)black_rook_l = tk.PhotoImage(file=black_rook_img)black_rook_l = black_rook_l.subsample(2, 2)board.addpiece("black_rook_l", black_rook_l, 0,0)上面的代碼我是將一塊(黑車)添加到板上,它按預(yù)期工作。下面是輔助函數(shù):def addpiece(self, name, image, row=0, column=0):    '''Add a piece to the playing board'''    self.canvas.create_image(0,0, image=image, tags=(name, "piece"), anchor="c")    self.placepiece(name, row, column)def placepiece(self, name, row, column):    '''Place a piece at the given row/column'''    self.pieces[name] = (row, column)    x0 = (column * self.size) + int(self.size/2)    y0 = (row * self.size) + int(self.size/2)    # print(name, x0, y0)    self.canvas.coords(name, x0, y0)但是當(dāng)我嘗試在 for 循環(huán)的幫助下放置 pawn 時(shí)出現(xiàn)問(wèn)題。下面是代碼:for i in range(8):    bname = tk.PhotoImage(file=black_pawn_img)    bname = bname.subsample(2, 2)    board.addpiece("black_pawn_"+str(i), bname, 1,i)root.mainloop()它只放置最后一個(gè) Pawn 塊。請(qǐng)建議/幫助我理解這個(gè)問(wèn)題。
查看完整描述

1 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

垃圾收集器正在銷毀 python 圖像對(duì)象。您需要保存對(duì)圖像的引用。第一次通過(guò)循環(huán),bname保存對(duì)創(chuàng)建的第一個(gè)圖像的引用。在下一次迭代中,bname修改為引用第二個(gè)圖像。因此,第一張圖像不再有參考。


一種簡(jiǎn)單的方法是在創(chuàng)建它們的代碼塊中跟蹤它們:


images = []

for i in range(8):

    bname = tk.PhotoImage(file=black_pawn_img)

    bname = bname.subsample(2, 2)

    board.addpiece("black_pawn_"+str(i), bname, 1,i)

    images.append(bname)

另一種方法是addpiece保存它們:


class GameBoard(...):

    def __init__(...):

        ...

        self.images = []

        ...

    def addpiece(self, name, image, row=0, column=0):

        ...

        self.images.append(image)

        ...


查看完整回答
反對(duì) 回復(fù) 2021-06-22
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)