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

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

為什么我不能在 tkinter 畫布中繪制 numpy 矩陣?

為什么我不能在 tkinter 畫布中繪制 numpy 矩陣?

寶慕林4294392 2023-05-09 15:11:38
我正在嘗試使用 numpy、matplotlib 和 tkinter 創(chuàng)建生命游戲。我打算首先詢問用戶游戲板上的行數(shù)和列數(shù),以及一個(gè)單元格存活的概率。輸入這些信息后,用戶將按下“生成”按鈕開始游戲。但是,無論何時(shí)按下按鈕,都會(huì)彈出錯(cuò)誤:“AttributeError: 'numpy.ndarray' object has no attribute 'set_canvas'”。我不知道為什么 FigureCanvasTkAgg 不能繪制一個(gè) numpy 矩陣。有沒有什么方法可以在 tkinter 窗口中繪制一個(gè) numpy 矩陣?這是我的代碼:from tkinter import *import numpy as npimport matplotlib.pyplot as pltfrom board import Boardfrom PIL import Image, ImageTkfrom matplotlib.backends.backend_tkagg import (    FigureCanvasTkAgg, NavigationToolbar2Tk)def num():    n1 = int(t1.get())    n2 = int(t2.get())    n3 = int(t3.get()) / 100.00    initBoard = np.zeros((n1, n2))    for row in range(0,n1):            for column in range(0,n2):                initBoard[row][column] = np.random.choice(np.arange(0, 2), p = [1 - n3, n3])    #game_board = Board(n1, n2, initBoard)    canvas = FigureCanvasTkAgg(initBoard, master=root)  # A tk.DrawingArea.    canvas.draw()    canvas.get_tk_widget().grid(row = 4, column = 0)    root.quit()root = Tk()root.title('Game of Life')root.geometry('800x600')Label(root, text="How many rows?: ").grid(row = 0)Label(root, text="How many columns?: ").grid(row = 1)Label(root, text="Probability of spawn (between 0 and 100): ").grid(row = 2)t1 = Entry(root)t2 = Entry(root)t3 = Entry(root)t1.grid(row = 0, column = 1)t2.grid(row = 1, column = 1)t3.grid(row = 2, column = 1)Button(root, text = 'Generate', command = num).grid(row = 3, column = 1, sticky = W, pady = 4)fig= plt.figure()canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.canvas.draw()canvas.get_tk_widget().grid(row = 4, column = 0)mainloop()
查看完整描述

1 回答

?
鴻蒙傳說

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

您的代碼中至少有兩個(gè)問題:


您沒有使用FigureCanvasTkAgg正確的方法,此類創(chuàng)建了一個(gè)包含 matplotlib 圖的畫布,您可以將其集成到您的 tkinter 應(yīng)用程序中。然而,這不是用這個(gè)類來繪制你的情節(jié),而是用類AxesSubplot(即 中的子圖Figure)。以下是如何創(chuàng)建 matplotlib 圖并將其集成到 tkinter 應(yīng)用程序中:


fig = plt.figure()  # matplotlib figure

ax = fig.subplot_add(111)  # subplot inside figure

canvas = FigureCanvasTkAgg(fig, master=root)  # integrate the figure in a tkinter widget

canvas.get_tk_widget().grid(row = 4, column = 0)  # display figure in tkinter app

現(xiàn)在,如果你想在圖中繪制一些東西,只需使用ax.plot(...)(或任何其他繪圖函數(shù))。在你的例子中,因?yàn)槟阆腼@示一個(gè)二維數(shù)組,所以你可能想使用ax.imshow(initBoard).


你退出root,num()這也會(huì)破壞包含你的圖形的窗口。我寧愿做的是創(chuàng)建一個(gè)Toplevel向用戶詢問參數(shù)的問題,并關(guān)閉這個(gè)頂層,而不是num().


完整代碼:


from tkinter import *  # not a good idea in my opinion, can lead to accidental 

                       # naming conflicts, "import tkinter as tk" is a better alternative

import numpy as np

import matplotlib.pyplot as plt

#~from board import Board

from PIL import Image, ImageTk

from matplotlib.backends.backend_tkagg import (

    FigureCanvasTkAgg, NavigationToolbar2Tk)


def num():

    n1 = int(t1.get())

    n2 = int(t2.get())

    n3 = int(t3.get()) / 100.00

    top.destroy()  # close dialog

    initBoard = np.zeros((n1, n2))

    for row in range(0, n1):

        for column in range(0, n2):

            initBoard[row][column] = np.random.choice(np.arange(0, 2), p=[1 - n3, n3])

    #game_board = Board(n1, n2, initBoard)

    ax.imshow(initBoard)  # draw board

    canvas.draw_idle()  # update matplotlib figure


root = Tk()

root.title('Game of Life')

root.geometry('800x600')

# create matplotlib figure

fig = plt.figure()

ax = fig.add_subplot(111)  # create axis

ax.axis('off')

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.

canvas.get_tk_widget().grid(row=4, column=0)


# toplevel to choose parameters

top = Toplevel(root)

Label(top, text="How many rows?: ").grid(row=0)

Label(top, text="How many columns?: ").grid(row=1)

Label(top, text="Probability of spawn (between 0 and 100): ").grid(row=2)


t1 = Entry(top)

t2 = Entry(top)

t3 = Entry(top)


t1.grid(row=0, column=1)

t2.grid(row=1, column=1)

t3.grid(row=2, column=1)


Button(top, text='Generate', command=num).grid(row=3, column=1, sticky=W, pady=4)


mainloop()



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

添加回答

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