use of numpy matrix in tkinter python 3

Solutions on MaxInterview for use of numpy matrix in tkinter python 3 by the best coders in the world

showing results for - "use of numpy matrix in tkinter python 3"
Andrea
06 Jun 2017
1from tkinter import *  # not a good idea in my opinion, can lead to accidental 
2                       # naming conflicts, "import tkinter as tk" is a better alternative
3import numpy as np
4import matplotlib.pyplot as plt
5#~from board import Board
6from PIL import Image, ImageTk
7from matplotlib.backends.backend_tkagg import (
8    FigureCanvasTkAgg, NavigationToolbar2Tk)
9
10def num():
11    n1 = int(t1.get())
12    n2 = int(t2.get())
13    n3 = int(t3.get()) / 100.00
14    top.destroy()  # close dialog
15    initBoard = np.zeros((n1, n2))
16    for row in range(0, n1):
17        for column in range(0, n2):
18            initBoard[row][column] = np.random.choice(np.arange(0, 2), p=[1 - n3, n3])
19    #game_board = Board(n1, n2, initBoard)
20    ax.imshow(initBoard)  # draw board
21    canvas.draw_idle()  # update matplotlib figure
22
23root = Tk()
24root.title('Game of Life')
25root.geometry('800x600')
26# create matplotlib figure
27fig = plt.figure()
28ax = fig.add_subplot(111)  # create axis
29ax.axis('off')
30canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
31canvas.get_tk_widget().grid(row=4, column=0)
32
33# toplevel to choose parameters
34top = Toplevel(root)
35Label(top, text="How many rows?: ").grid(row=0)
36Label(top, text="How many columns?: ").grid(row=1)
37Label(top, text="Probability of spawn (between 0 and 100): ").grid(row=2)
38
39t1 = Entry(top)
40t2 = Entry(top)
41t3 = Entry(top)
42
43t1.grid(row=0, column=1)
44t2.grid(row=1, column=1)
45t3.grid(row=2, column=1)
46
47Button(top, text='Generate', command=num).grid(row=3, column=1, sticky=W, pady=4)
48
49mainloop()