Advertisement
jimkilled

GRAPHICS V6 main.py

Dec 16th, 2020 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.69 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import ttk
  3. from graphics import *
  4. from math import *
  5.  
  6.  
  7. def initialize_window(window, title, size=(1024, 720), sizable=(False, False)):
  8.     '''
  9.    Устанавливает заголовок окна, размеры по осям X, Y и устанавливает возможность изменения по осям X, Y
  10.    args - window (Tk), title (str), size (tuple(int, int)), sizable (tuple(bool, bool))
  11.    return - None
  12.    '''
  13.     window.title(title)
  14.     window.geometry(f'{size[0]}x{size[1]}')
  15.     window.resizable(sizable[0], sizable[1])
  16.  
  17.  
  18. def initialize_widgets(window, start, size=(1024, 720)):
  19.     MARK = Label(window, text='By George Golubev Lyceum №8. 2020', font=('Arial', 15, 'bold'))
  20.     MARK.place(x=1020, y=835)
  21.     global canvas
  22.     canvas = Canvas(window, bg='#fff', width=size[0], height=size[1])
  23.     canvas.place(x=start[0], y=start[1])
  24.  
  25.     text = Label(window, text='y = ', font=('Arial', 13, 'bold'))
  26.     text.place(x=1060, y=355)
  27.  
  28.     but_func = Button(window, text='Сгенерировать', command=get_choosen_graphic_n_draw)
  29.     but_func.place(x=1165, y=380)
  30.  
  31.     but_erase_last_func = Button(window, width=11, text='Стереть', command=erase_graphic)
  32.     but_erase_last_func.place(x=1295, y=40)
  33.  
  34.     but_erase_all = Button(window, width=11, text='Стереть всё', command=erase_graphics)
  35.     but_erase_all.place(x=1295, y=70)
  36.  
  37.     but_hide_graph = Button(window, width=11, text='Скрыть', command=hideshow_graphic)
  38.     but_hide_graph.place(x=1295, y=100)
  39.  
  40.     global func_listbox
  41.     func_listbox = Listbox(window, width=25, height=20, selectmode=EXTENDED)
  42.     func_listbox.place(x=1135, 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.     global func_choose, ent_first_coef, ent_second_coef
  52.     ent_first_coef = Entry(window, width=3)
  53.     ent_first_coef.place(x=1095, y=357)
  54.  
  55.     text = Label(window, text='*', font=('Arial', 13, 'bold'))
  56.     text.place(x=1120, y=357)
  57.  
  58.     functions = [
  59.                 'acos(x)', 'acosh(x)', 'asin(x)', 'asinh(x)', 'atan(x)', 'atan2(x,x)', 'atanh(x)', 'ceil(x)',
  60.                 'cos(x)', 'cosh(x)', 'exp(x)', 'expm1(x)', 'fabs(x)', 'factorial(x)', 'log(x)', 'log10(x)',
  61.                 'log1p(x)', 'log2(x)','sin(x)','sinh(x)', 'sqrt(x)', 'tan(x)', 'tanh(x)', 'tau(x)', 'trunc(x)'
  62.                 ]
  63.  
  64.     func_choose = ttk.Combobox(window, values=functions, width=20)
  65.     func_choose.place(x=1140, y=356)
  66.  
  67.     text = Label(window, text='+', font=('Arial', 13))
  68.     text.place(x=1290, y=354)
  69.  
  70.     ent_second_coef = Entry(window, width=3)
  71.     ent_second_coef.place(x=1310, y=356)
  72.  
  73.  
  74. def enter_function(event):
  75.     get_choosen_graphic_n_draw()
  76.  
  77.  
  78. def get_choosen_graphic_n_draw():
  79.     body_func = func_choose.get()
  80.     first_coef = ent_first_coef.get()
  81.     second_coef = ent_second_coef.get()
  82.  
  83.     if first_coef != '':
  84.         first_coef += ' * '
  85.  
  86.     if second_coef != '':
  87.         if '-' not in second_coef:
  88.             second_coef = ' + ' + second_coef
  89.         else:
  90.             second_coef = ' - ' + second_coef[1:]
  91.  
  92.     func = Graphic(first_coef + body_func + second_coef)
  93.  
  94.     if func.function in graphs:
  95.         return
  96.  
  97.     functions.append(func)
  98.     graphs.append(func.function)
  99.     draw_graphic(func)  # params = (middle_cord_x, middle_cord_y, delay)
  100.     update_listbox('ADD')
  101.  
  102.  
  103. def create_graphic_grid(delay, size=(1024, 720)):
  104.  
  105.     width = size[0]
  106.     height = size[1]
  107.  
  108.     middle_cord_x = (width)//2
  109.     middle_cord_y = (height)//2
  110.  
  111.     # Созадние осей графика
  112.     canvas.create_line(middle_cord_x, height, middle_cord_x, 0, width=3, arrow=LAST)
  113.     canvas.create_line(0, middle_cord_y, width, middle_cord_y, width=3, arrow=LAST)
  114.  
  115.     start_x = middle_cord_x % delay
  116.     start_y = middle_cord_y % delay
  117.  
  118.     # Создание сетки графика, меток и цифр
  119.     for i in range(start_x, width, delay):
  120.         canvas.create_line(i, 0, i, height, fill='grey')                        # Создание линий сетки по оси OY
  121.         canvas.create_line(i, middle_cord_y-5, i, middle_cord_y+6, width=3)     # Создание меток (разделителей на оси OX)
  122.         text = (middle_cord_x - i) // delay
  123.         canvas.create_text(i-5, middle_cord_y+10, text=-text, fill='#f10', font=('Arial', 8, 'bold')) # Создание цифр на разделителях оси OX
  124.  
  125.     for i in range(start_y, height, delay):
  126.         canvas.create_line(0, i, width, i, fill='grey')                         # Создание линий сетки по оси OX
  127.         canvas.create_line(middle_cord_x-5, i, middle_cord_x+6, i, width=3)     # Создание меток (разделителей на оси OY)
  128.         text = (middle_cord_y - i) // delay
  129.         if text == 0:
  130.             continue
  131.         canvas.create_text(middle_cord_x-10, i, text=text, fill='#f10', font=('Arial', 8, 'bold'))    # Создание цифр на разделителях оси OY
  132.  
  133.  
  134.     global params
  135.     params = (middle_cord_x, middle_cord_y, delay)
  136.  
  137.  
  138. def hideshow_graphic():
  139.     selection = func_listbox.curselection()
  140.     for index in selection:
  141.         func = functions[index]
  142.  
  143.         if func.hided == True:
  144.             show_graphic(func, index)
  145.         else:
  146.             hide_graphic(func, index)
  147.  
  148.  
  149. # Скрывает график удаляя линии рисунка
  150. def hide_graphic(func, index):
  151.     '''
  152.    args - func (Grpahic), index (Int)
  153.    return - None
  154.    '''
  155.     func.hided = True
  156.     delete_graphic_lines(func)
  157.     graphics.pop(func)
  158.     func_listbox.delete(index)
  159.     func_listbox.insert(index,' y = ' + func.function + ' СКРЫТ')
  160.  
  161.  
  162. # Перерисовывает скрытый график
  163. def show_graphic(func, index):
  164.     '''
  165.    args - func (Grpahic), index (Int)
  166.    return - None
  167.    '''
  168.     func.hided = False
  169.     draw_graphic(func)
  170.     func_listbox.delete(index)
  171.     func_listbox.insert(index,' y = ' + func.function)
  172.  
  173.  
  174. def delete_graphic_lines(func):
  175.     '''
  176.    args - func (Grpahic)
  177.    return - None
  178.    '''
  179.     for line in graphics[func]:
  180.         canvas.delete(line)
  181.  
  182.  
  183. # Стерает выбранный график или последний, если график не выбран
  184. def erase_graphic():
  185.     global functions
  186.     selection = func_listbox.curselection()
  187.  
  188.     if len(selection) == 0:
  189.         func = functions[-1]
  190.         functions.pop()
  191.         graphs.pop()
  192.         index_func = len(functions)
  193.         delete_graphic_lines(func)
  194.         if not func.hided:
  195.             graphics.pop(func)
  196.         update_listbox('DELETEFUNC', index_func)
  197.  
  198.     else:
  199.         deleted_index = []
  200.         for i in sorted(list(selection), reverse=True):
  201.             index_func = selection[i]
  202.             func = functions[i]
  203.  
  204.             if func.hided:
  205.                 functions.remove(func)
  206.                 graphs.remove(func.function)
  207.                 deleted_index.append(index_func)
  208.             else:
  209.                 functions.remove(func)
  210.                 graphs.remove(func.function)
  211.                 delete_graphic_lines(func)
  212.                 graphics.pop(func)
  213.                 deleted_index.append(index_func)
  214.  
  215.         deleted_index.sort(reverse=True)
  216.         for i in deleted_index:
  217.             update_listbox('DELETEFUNC', i)
  218.  
  219.  
  220. # Удаляет все графики, а также очищает массивы
  221. def erase_graphics():
  222.     global graphics, functions, graphs
  223.  
  224.     for function in graphics:
  225.         delete_graphic_lines(function)
  226.  
  227.     graphs = []
  228.     functions = []
  229.     graphics = {}
  230.     update_listbox('DELETEALL')
  231.  
  232.  
  233. def draw_graphic(func):
  234.     '''
  235.    Рисует график по точкам
  236.    args - func (Grpahic)
  237.    return - None
  238.    '''
  239.     graphic = []
  240.     x_dots = func.dots[0]
  241.     y_dots = func.dots[1]
  242.     for i in range(1, len(x_dots)-1):
  243.         try:
  244.             if x_dots[i] != None and x_dots[i-1] != None:
  245.                 last_x, last_y = set_coordinat(x_dots[i-1], y_dots[i-1], *params)
  246.                 x, y = set_coordinat(x_dots[i], y_dots[i], *params)
  247.                 graphic.append(canvas.create_line(last_x, last_y, x, y, width=3))
  248.         except Exception:
  249.             continue
  250.     graphics[func] = graphic
  251.  
  252.  
  253. def set_coordinat(x, y, middle_cord_x, middle_cord_y, delay):
  254.     '''
  255.    Устанавливает координаты точки на графике
  256.    args - x (int), y (int), middle_cord_x (int), middle_cord_y (int), delay(int)
  257.    return - x (int), y (int)
  258.    '''
  259.     x = middle_cord_x + x*delay
  260.     y = middle_cord_y - (y*delay)
  261.     return x, y
  262.  
  263.  
  264. def update_listbox(action, index=None):
  265.     '''
  266.    args - action (str), index (int)
  267.    return - None
  268.    '''
  269.     # Удаляет все графики
  270.     if action == 'DELETEALL':
  271.         func_listbox.delete(0, func_listbox.size())
  272.     # Удаляет выбранный график
  273.     elif action == 'DELETEFUNC':
  274.         func_listbox.delete(index)
  275.     # Добавляет графиик
  276.     elif action == 'ADD':
  277.         func_listbox.insert(len(functions)-1, ' y = ' + functions[-1].function)
  278.  
  279.  
  280. def main():
  281.     global graphics, functions, graphs
  282.     graphics = {}
  283.     functions = []
  284.     graphs = []
  285.  
  286.     size_of_window = (1400, 900)
  287.     root = Tk()
  288.     start = (10, 10)
  289.     initialize_window(root, 'Graphics', size_of_window)
  290.     initialize_widgets(root, start)
  291.  
  292.     delay = 40
  293.     create_graphic_grid(delay)
  294.     root.mainloop()
  295.  
  296.  
  297. if __name__ == "__main__":
  298.     main()
  299.  
  300. # made by Egor Golubev Lyceum №8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement