Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import ttk
- from tkinter import colorchooser
- from graphics import *
- from math import *
- def initialize_window(window, title, size=(1024, 720), sizable=(False, False)):
- '''
- Устанавливает заголовок окна, размеры по осям X, Y и устанавливает возможность изменения по осям X, Y
- args - window (Tk), title (str), size (tuple(int, int)), sizable (tuple(bool, bool))
- return - None
- '''
- window.title(title)
- window.geometry(f'{size[0]}x{size[1]}')
- window.resizable(sizable[0], sizable[1])
- def initialize_widgets(window, start, size=(1024, 720)):
- MARK = Label(window, text='By George Golubev Lyceum №8. 2020', font=('Arial', 10, 'bold'))
- MARK.place(x=1150, y=840)
- global canvas
- canvas = Canvas(window, bg='#fff', width=size[0], height=size[1])
- canvas.place(x=start[0], y=start[1])
- text = Label(window, text='y = ', font=('Arial', 13, 'bold'))
- text.place(x=1060, y=355)
- but_func = Button(window, text='Сгенерировать', command=get_choosen_graphic_n_draw)
- but_func.place(x=1165, y=380)
- but_erase_last_func = Button(window, width=11, text='Стереть', command=erase_graphic)
- but_erase_last_func.place(x=1295, y=40)
- but_erase_all = Button(window, width=11, text='Стереть всё', command=erase_graphics)
- but_erase_all.place(x=1295, y=70)
- but_hide_graph = Button(window, width=11, text='Скрыть', command=hideshow_graphic)
- but_hide_graph.place(x=1295, y=100)
- global func_listbox
- func_listbox = Listbox(window, width=25, height=20, selectmode=EXTENDED)
- func_listbox.place(x=1135, y=20)
- # scroll = Scrollbar(window, command=func_listbox.yview, orient=VERTICAL)
- # scroll.place(x=1329, y=20)
- # func_listbox.config(yscrollcommand=scroll.set)
- global func_choose, ent_first_coef, ent_second_coef
- ent_first_coef = Entry(window, width=3)
- ent_first_coef.place(x=1095, y=357)
- text = Label(window, text='*', font=('Arial', 13, 'bold'))
- text.place(x=1120, y=357)
- functions = [
- 'acos(x)', 'acosh(x)', 'asin(x)', 'asinh(x)', 'atan(x)', 'atan2(x,x)', 'atanh(x)', 'ceil(x)',
- 'cos(x)', 'cosh(x)', 'exp(x)', 'expm1(x)', 'fabs(x)', 'factorial(x)', 'log(x)', 'log10(x)',
- 'log1p(x)', 'log2(x)','sin(x)','sinh(x)', 'sqrt(x)', 'tan(x)', 'tanh(x)', 'tau(x)', 'trunc(x)'
- ]
- func_choose = ttk.Combobox(window, values=functions, width=20)
- func_choose.place(x=1140, y=356)
- text = Label(window, text='+', font=('Arial', 13))
- text.place(x=1290, y=354)
- ent_second_coef = Entry(window, width=3)
- ent_second_coef.place(x=1310, y=356)
- global color
- color = '#000000'
- global but_color
- but_color = Button(window, command=get_color, width=2, height=1, bg=color)
- but_color.place(x=1300, y=380)
- # but_scale1 = Button(window, command=scale_up, text='up')
- # but_scale1.place(x=1300, y=500)
- # but_scale2 = Button(window, command=scale_down, text='down')
- # but_scale2.place(x=1300, y=600)
- global scale, graphic_scale
- graphic_scale = 1
- scale = 1
- #BINDINGS
- window.bind('<Return>', enter_function)
- window.bind('<MouseWheel>', mouse_wheel)
- def mouse_wheel(event):
- global scale, graphic_scale
- # -120 down, 120 up
- if event.delta == 120:
- graphic_scale *= 2
- elif event.delta == -120 and scale >= -1:
- graphic_scale /= 2
- if event.delta == 120:
- canvas.scale('all', 512, 360, 2, 2)
- elif event.delta == -120:
- canvas.scale('all', 512, 360, 0.5, 0.5)
- def create_graphic_grid(delay, size=(1024, 720)):
- width = size[0]
- height = size[1]
- middle_cord_x = (width)//2
- middle_cord_y = (height)//2
- # Созадние осей графика
- canvas.create_line(middle_cord_x, height, middle_cord_x, 0, width=3, arrow=LAST)
- canvas.create_line(0, middle_cord_y, width, middle_cord_y, width=3, arrow=LAST)
- start_x = middle_cord_x % delay
- start_y = middle_cord_y % delay
- # Создание сетки графика, меток и цифр
- for i in range(start_x, width, delay):
- canvas.create_line(i, 0, i, height, fill='grey') # Создание линий сетки по оси OY
- canvas.create_line(i, middle_cord_y-5, i, middle_cord_y+6, width=3) # Создание меток (разделителей на оси OX)
- text = (middle_cord_x - i) // delay
- canvas.create_text(i-5, middle_cord_y+10, text=-text, fill='#f10', font=('Arial', 8, 'bold')) # Создание цифр на разделителях оси OX
- for i in range(start_y, height, delay):
- canvas.create_line(0, i, width, i, fill='grey') # Создание линий сетки по оси OX
- canvas.create_line(middle_cord_x-5, i, middle_cord_x+6, i, width=3) # Создание меток (разделителей на оси OY)
- text = (middle_cord_y - i) // delay
- if text == 0:
- continue
- canvas.create_text(middle_cord_x-10, i, text=text, fill='#f10', font=('Arial', 8, 'bold')) # Создание цифр на разделителях оси OY
- global params
- params = (middle_cord_x, middle_cord_y, delay)
- def get_color():
- global color
- color = colorchooser.askcolor()[1]
- but_color.config(bg=color)
- def enter_function(event):
- get_choosen_graphic_n_draw()
- def get_choosen_graphic_n_draw():
- body_func = func_choose.get()
- first_coef = ent_first_coef.get()
- second_coef = ent_second_coef.get()
- if first_coef != '':
- first_coef += ' * '
- if second_coef != '':
- if '-' not in second_coef:
- second_coef = ' + ' + second_coef
- else:
- second_coef = ' - ' + second_coef[1:]
- func = Graphic(first_coef + body_func + second_coef)
- if func.function in graphs:
- return
- functions.append(func)
- graphs.append(func.function)
- draw_graphic(func) # params = (middle_cord_x, middle_cord_y, delay)
- update_listbox('ADD')
- def hideshow_graphic():
- selection = func_listbox.curselection()
- for index in selection:
- func = functions[index]
- if func.hided == True:
- show_graphic(func, index)
- else:
- hide_graphic(func, index)
- # Скрывает график удаляя линии рисунка
- def hide_graphic(func, index):
- '''
- args - func (Grpahic), index (Int)
- return - None
- '''
- func.hided = True
- delete_graphic_lines(func)
- graphics.pop(func)
- func_listbox.delete(index)
- func_listbox.insert(index,' y = ' + func.function + ' СКРЫТ')
- # Перерисовывает скрытый график
- def show_graphic(func, index):
- '''
- args - func (Grpahic), index (Int)
- return - None
- '''
- func.hided = False
- draw_graphic(func)
- func_listbox.delete(index)
- func_listbox.insert(index,' y = ' + func.function)
- def delete_graphic_lines(func):
- '''
- args - func (Grpahic)
- return - None
- '''
- for line in graphics[func]:
- canvas.delete(line)
- # Стерает выбранный график или последний, если график не выбран
- def erase_graphic():
- global functions
- selection = func_listbox.curselection()
- if len(selection) == 0:
- func = functions[-1]
- functions.pop()
- graphs.pop()
- index_func = len(functions)
- delete_graphic_lines(func)
- if not func.hided:
- graphics.pop(func)
- update_listbox('DELETEFUNC', index_func)
- else:
- deleted_index = []
- for i in sorted(list(selection), reverse=True):
- index_func = i
- func = functions[i]
- if func.hided:
- functions.remove(func)
- graphs.remove(func.function)
- deleted_index.append(index_func)
- else:
- functions.remove(func)
- graphs.remove(func.function)
- delete_graphic_lines(func)
- graphics.pop(func)
- deleted_index.append(index_func)
- deleted_index.sort(reverse=True)
- for i in deleted_index:
- update_listbox('DELETEFUNC', i)
- # Удаляет все графики, а также очищает массивы
- def erase_graphics():
- global graphics, functions, graphs
- for function in graphics:
- delete_graphic_lines(function)
- graphs = []
- functions = []
- graphics = {}
- update_listbox('DELETEALL')
- def draw_graphic(func):
- global color
- '''
- Рисует график по точкам
- args - func (Grpahic)
- return - None
- '''
- graphic = []
- x_dots = func.dots[0]
- y_dots = func.dots[1]
- for i in range(1, len(x_dots)-1):
- try:
- if x_dots[i] != None and x_dots[i-1] != None:
- last_x, last_y = set_coordinat(x_dots[i-1], y_dots[i-1], *params)
- x, y = set_coordinat(x_dots[i], y_dots[i], *params)
- graphic.append(canvas.create_line(last_x, last_y, x, y, fill=color, width=3))
- except Exception:
- continue
- graphics[func] = graphic
- def set_coordinat(x, y, middle_cord_x, middle_cord_y, delay):
- '''
- Устанавливает координаты точки на графике
- args - x (int), y (int), middle_cord_x (int), middle_cord_y (int), delay(int)
- return - x (int), y (int)
- '''
- x = middle_cord_x + x*delay*graphic_scale
- y = middle_cord_y - y*delay*graphic_scale
- return x, y
- def update_listbox(action, index=None):
- '''
- args - action (str), index (int)
- return - None
- '''
- # Удаляет все графики
- if action == 'DELETEALL':
- func_listbox.delete(0, func_listbox.size())
- # Удаляет выбранный график
- elif action == 'DELETEFUNC':
- func_listbox.delete(index)
- # Добавляет графиик
- elif action == 'ADD':
- func_listbox.insert(len(functions)-1, ' y = ' + functions[-1].function)
- def main():
- global graphics, functions, graphs
- graphics = {}
- functions = []
- graphs = []
- size_of_window = (1400, 900)
- root = Tk()
- start = (10, 10)
- initialize_window(root, 'Graphics', size_of_window)
- initialize_widgets(root, start)
- delay = 40
- create_graphic_grid(delay)
- root.mainloop()
- if __name__ == "__main__":
- main()
- # made by Egor Golubev Lyceum №8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement