Advertisement
jimkilled

graphics v2

Dec 8th, 2020 (edited)
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.21 KB | None | 0 0
  1. from tkinter import *
  2. from math import *
  3.  
  4.  
  5. def initialize_window(window, title, size=(1024, 720), sizable=(False, False)):
  6.     '''
  7.    Устанавливает заголовок окна, размеры по осям X, Y и устанавливает возможность изменения по осям X, Y
  8.    args - window (Tk), title (str), size (tuple(int, int)), sizable (tuple(bool, bool))
  9.    return - None
  10.    '''
  11.     window.title(title)
  12.     window.geometry(f'{size[0]}x{size[1]}')
  13.     window.resizable(sizable[0], sizable[1])
  14.  
  15.  
  16. def initialize_widgets(window, start, size=(1024, 720)):
  17.     global canvas
  18.     canvas = Canvas(window, bg='#fff', width=size[0], height=size[1])
  19.     canvas.place(x=start[0], y=start[1])
  20.  
  21.     text = Label(window, text='y = ', font=('Arial', 13, 'bold'))
  22.     text.place(x=1130, y=347)
  23.  
  24.     global ent_func
  25.     ent_func = Entry(window, width='20')
  26.     ent_func.place(x=1168, y=350)
  27.  
  28.     but_func = Button(window, text='Сгенерировать', command=get_and_draw_graphic)
  29.     but_func.place(x=1175, y=374)
  30.  
  31.     but_erase_last_func = Button(window, text='Стереть', command=erase_graphic)
  32.     but_erase_last_func.place(x=1175, y=400)
  33.  
  34.     but_erase_all = Button(window, text='Стереть всё', command=erase_graphics)
  35.     but_erase_all.place(x=1175, y=426)
  36.  
  37.     but_hide_graph = Button(window, text='Скрыть', command=hide_graphic)
  38.     but_hide_graph.place(x=1175, y=452)
  39.  
  40.     global func_listbox
  41.     func_listbox = Listbox(window, width=30, height=20, selectmode=SINGLE)
  42.     func_listbox.place(x=1145, y=20)
  43.  
  44.     scroll = Scrollbar(window, command=func_listbox.yview, orient=VERTICAL)
  45.     scroll.place(x=1329, y=20)
  46.  
  47.     func_listbox.config(yscrollcommand=scroll.set)
  48.  
  49.     window.bind('<Return>', enter_function)
  50.  
  51.  
  52. def create_graphic_grid(delay, size=(1024, 720)):
  53.  
  54.     width = size[0]
  55.     height = size[1]
  56.  
  57.     middle_cord_x = (width)//2
  58.     middle_cord_y = (height)//2
  59.  
  60.     # Созадние осей графика
  61.     canvas.create_line(middle_cord_x, height, middle_cord_x, 0, width=3, arrow=LAST)
  62.     canvas.create_line(0, middle_cord_y, width, middle_cord_y, width=3, arrow=LAST)
  63.  
  64.     start_x = middle_cord_x % delay
  65.     start_y = middle_cord_y % delay
  66.  
  67.     # Создание сетки графика
  68.     # for i in range(start_x, width, delay):
  69.     #     canvas.create_line(i, y0, i, height, fill='grey')
  70.     # for i in range(start_y, height, delay):
  71.     #     canvas.create_line(x0, i, width, i, fill='grey')
  72.  
  73.     # Создание сетки графика, меток и цифр
  74.     for i in range(start_x, width, delay):
  75.         canvas.create_line(i, 0, i, height, fill='grey')
  76.         canvas.create_line(i, middle_cord_y-5, i, middle_cord_y+6, width=3)
  77.         text = (middle_cord_x - i) // delay
  78.         canvas.create_text(i-5, middle_cord_y+10, text=-text, fill='#f10', font=('Arial', 8, 'bold'))
  79.     for i in range(start_y, height, delay):
  80.         canvas.create_line(0, i, width, i, fill='grey')
  81.         canvas.create_line(middle_cord_x-5, i, middle_cord_x+6, i, width=3)
  82.         text = (middle_cord_y - i) // delay
  83.         if text == 0:
  84.             continue
  85.         canvas.create_text(middle_cord_x-10, i, text=text, fill='#f10', font=('Arial', 8, 'bold'))
  86.  
  87.     global params
  88.     params = (middle_cord_x, middle_cord_y, delay)
  89.  
  90. def enter_function(event):
  91.     get_and_draw_graphic()
  92.  
  93.  
  94. def hide_graphic():
  95.     selection = func_listbox.curselection()
  96.     if len(selection) == 1:
  97.         index_func = selection[0]
  98.         func = func_listbox.get(selection[0]).split()[2]
  99.  
  100.     if func in hided_graphics:
  101.         show_graphic(func, index_func)
  102.         return
  103.  
  104.     hided_graphics.append(func)
  105.     # Стерание графика выбранного графика
  106.     for line in graphics[func]:
  107.         canvas.delete(line)
  108.  
  109.     graphics.pop(func)
  110.     func_listbox.delete(index_func)
  111.     func_listbox.insert(index_func,' y = ' + func + ' СКРЫТ')
  112.  
  113. def show_graphic(func, index_func):
  114.     hided_graphics.remove(func)
  115.     create_graphic(func, *params)
  116.     func_listbox.delete(index_func)
  117.     func_listbox.insert(index_func,' y = ' + func)
  118.  
  119. def get_and_draw_graphic():
  120.     func = ent_func.get()
  121.     if func in functions:
  122.         return
  123.     functions.append(func)
  124.     create_graphic(func, *params)  # params = (middle_cord_x, middle_cord_y, delay)
  125.     update_listbox('ADD')
  126.  
  127.  
  128. # Стерает выбранный график или последний, если график не выбран
  129. def erase_graphic():
  130.     global functions
  131.     selection = func_listbox.curselection()
  132.  
  133.     if len(selection) == 0:
  134.         func = functions[-1]
  135.         functions.pop()
  136.         index_func = len(functions)
  137.     else:
  138.         index_func = selection[0]
  139.         func = func_listbox.get(selection[0]).split()[2]
  140.         functions.remove(func)
  141.  
  142.     for line in graphics[func]:
  143.         canvas.delete(line)
  144.     graphics.pop(func)
  145.     update_listbox('DELETEFUNC', index_func)
  146.  
  147. # def erase_last_graphic():
  148. #     last_func = functions[len(functions)-1]
  149. #     functions.pop()
  150. #     for line in graphics[last_func]:
  151. #         canvas.delete(line)
  152. #     graphics.pop(last_func)
  153. #     update_listbox('DELETELAST')
  154.  
  155.  
  156. def erase_graphics():
  157.     global graphics, functions
  158.     for function in graphics:
  159.         for line in graphics[function]:
  160.             canvas.delete(line)
  161.     hided_graphics = []
  162.     functions = []
  163.     graphics = {}
  164.     update_listbox('DELETEALL')
  165.  
  166.  
  167. def create_graphic(func, middle_cord_x, middle_cord_y, delay):
  168.     graphic = []
  169.     for x in range(-10000, 10000):
  170.         try:
  171.             last_x = (x - 1)/100
  172.             last_y = set_y_func_value(last_x, func)
  173.             x /= 100                                   # Уменьшение x для получения более точного рисунка графика
  174.             y = set_y_func_value(x, func)
  175.             last_x, last_y = set_coordinat(last_x, last_y, middle_cord_x, middle_cord_y, delay)
  176.             # x = middle_cord_x + x*delay
  177.             # y = middle_cord_y - (y*delay)
  178.             x, y = set_coordinat(x, y, middle_cord_x, middle_cord_y, delay)
  179.             graphic.append(canvas.create_line(last_x, last_y, x, y, width=3))
  180.         except:
  181.             continue
  182.     graphics[func] = graphic
  183.  
  184.  
  185. def set_y_func_value(x, func):
  186.     y = eval(func)
  187.     return y
  188.  
  189.  
  190. def set_coordinat(x, y, middle_cord_x, middle_cord_y, delay):
  191.     x = middle_cord_x + x*delay
  192.     y = middle_cord_y - (y*delay)
  193.     return x, y
  194.  
  195.  
  196. def update_listbox(action, index=None):
  197.     if action == 'DELETEALL':
  198.         func_listbox.delete(0, func_listbox.size())
  199.     elif action == 'DELETEFUNC':
  200.         func_listbox.delete(index)
  201.     elif action == 'ADD':
  202.         func_listbox.insert(len(functions)-1, ' y = ' + str(functions[-1]) )
  203.  
  204.  
  205. def main():
  206.     global size
  207.     global graphics, functions, hided_graphics
  208.     graphics = {}
  209.     functions = []
  210.     hided_graphics =[]
  211.  
  212.     size_of_window = (1400, 1000)
  213.     root = Tk()
  214.     start = (10, 10)
  215.     initialize_window(root, 'Graphics', size_of_window)
  216.     initialize_widgets(root, start)
  217.  
  218.     delay = 40
  219.     create_graphic_grid(delay)
  220.     root.mainloop()
  221.  
  222.  
  223. if __name__ == "__main__":
  224.     main()
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement