ostapdontstop

GSV2

Jul 3rd, 2020
1,199
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkinter as tk
  2.  
  3. import matplotlib
  4. matplotlib.use('TkAgg')
  5. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  6. from matplotlib.figure import Figure
  7. import matplotlib.pyplot as plt
  8.  
  9. import random
  10. import gen
  11.  
  12. class GenApp(tk.Frame):
  13.  
  14.     GENS = [
  15.         gen.lcg(2**32, 1664525, 1013904223, 0),
  16.         gen.mid_square(36149212),
  17.         gen.mid_multi(123456),
  18.         gen.mix_sum(1234567890) ]
  19.  
  20.     NAMES = [
  21.         'Срединные квадраты',
  22.         'Срединные произведения',
  23.         'Метод перемешивания',
  24.         'Линейно конгруэнтный' ]
  25.  
  26.     CURRENT = 0
  27.     N = 500
  28.  
  29.     STYLE = dict(marker = 'o', s = 20, alpha = 0.8)
  30.  
  31.  
  32.     def __init__(self, master, *args, **kwargs):
  33.         tk.Frame.__init__(self, master, *args, **kwargs)
  34.        
  35.         self.master = master
  36.         master.title("Графики ГПСЧ")
  37.         self.text_input = tk.StringVar()
  38.         self.text_input.set(str(self.N))
  39.  
  40.         self.createPlots();
  41.         self.createTextInput();
  42.         self.createButtons();
  43.         self.set_method()
  44.  
  45.  
  46.  
  47.     def createPlots(self):
  48.  
  49.         self.fig = Figure()
  50.         self.fig.patch.set_visible(False)
  51.         self.ax = self.fig.subplots(2,1,sharey=True, gridspec_kw={'hspace':0.4})
  52.  
  53.         for side in ['top','right', 'bottom','left']:
  54.             self.ax[0].spines[side].set_color('#ccc')
  55.             self.ax[1].spines[side].set_color('#ccc')
  56.            
  57.         self.ax[0].spines['top'].set_visible(False)
  58.         self.ax[1].spines['top'].set_visible(False)
  59.  
  60.  
  61.         self.canvas = FigureCanvasTkAgg(self.fig, master=self.master)
  62.         self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
  63.        
  64.  
  65.     def createButtons(self):
  66.         frame = tk.Frame()
  67.         frame.pack(side=tk.BOTTOM, pady=5)
  68.         for i,name in enumerate(self.NAMES):
  69.             tk.Button(frame,text=self.NAMES[i], width=22, command=lambda i=i: self.callback(set_gen = i)).grid(row=i//2, column=i%2)
  70.        
  71.     def createTextInput(self):
  72.         frame = tk.Frame()
  73.         frame.pack(side=tk.BOTTOM, pady=5)
  74.         tk.Label(frame,text='Длина').pack(side=tk.LEFT, padx=10, pady=5)
  75.         tk.Button(frame,text="OK", command=self.callback, width=4).pack(side=tk.RIGHT, padx=10, pady=5)
  76.         text = tk.Entry(frame,textvariable=self.text_input)
  77.         text.bind('<Return>', self.callback)
  78.         text.focus_set()
  79.         text.pack(side=tk.LEFT, padx=1, pady=5)
  80.  
  81.  
  82.     def callback(self, event = None, set_gen = None):
  83.         try:
  84.             self.N = int(self.text_input.get())
  85.         except ValueError:
  86.             return
  87.         if self.N < 0:
  88.             return
  89.         if set_gen != None:
  90.             self.CURRENT = int(set_gen)
  91.         self.set_method(set_gen)
  92.  
  93.  
  94.     def set_method(self, method = None):
  95.         if method != None:
  96.             self.CURRENT = method
  97.  
  98.         self.ax[0].clear()
  99.         self.ax[0].set_xlim(0,self.N)
  100.         self.ax[0].set_title('random()')
  101.         self.ax[0].scatter(range(self.N),[random.random() for i in range(self.N)],**self.STYLE)
  102.  
  103.         self.ax[1].clear()
  104.         self.ax[1].set_xlim(0,self.N)
  105.         self.ax[1].set_title(self.NAMES[self.CURRENT])
  106.         self.ax[1].scatter(range(self.N),[next(self.GENS[self.CURRENT]) for i in range(self.N)],**self.STYLE)
  107.  
  108.         self.canvas.draw()
  109.  
  110.  
  111. if __name__ == '__main__':
  112.     root = tk.Tk()
  113.     GenApp(root).pack(side="top", fill="both", expand=True)
  114.     root.mainloop()
RAW Paste Data