Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter 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)):
- 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=1130, y=347)
- global ent_func
- ent_func = Entry(window, width='20')
- ent_func.place(x=1168, y=350)
- but_func = Button(window, text='Сгенерировать', command=get_and_draw_graphic)
- but_func.place(x=1175, y=374)
- but_erase_last_func = Button(window, text='Стереть', command=erase_graphic)
- but_erase_last_func.place(x=1175, y=400)
- but_erase_all = Button(window, text='Стереть всё', command=erase_graphics)
- but_erase_all.place(x=1175, y=426)
- but_hide_graph = Button(window, text='Скрыть', command=hide_graphic)
- but_hide_graph.place(x=1175, y=452)
- global func_listbox
- func_listbox = Listbox(window, width=30, height=20, selectmode=EXTENDED)
- func_listbox.place(x=1145, y=20)
- scroll = Scrollbar(window, command=func_listbox.yview, orient=VERTICAL)
- scroll.place(x=1329, y=20)
- func_listbox.config(yscrollcommand=scroll.set)
- window.bind('<Return>', enter_function)
- 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, y0, i, height, fill='grey')
- # for i in range(start_y, height, delay):
- # canvas.create_line(x0, i, width, i, fill='grey')
- # Создание сетки графика, меток и цифр
- 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 enter_function(event):
- get_and_draw_graphic()
- # Скрывает график удаляя линии рисунка
- def hide_graphic():
- selection = func_listbox.curselection()
- for i in range(len(selection)):
- index_func = selection[i]
- func = func_listbox.get(selection[i])[5:]
- if ' СКРЫТ' in func:
- func = func[:-6]
- show_graphic(func, index_func)
- else:
- hided_graphics.append(func)
- # Стерание графика выбранного графика
- for line in graphics[func]:
- canvas.delete(line)
- graphics.pop(func)
- func_listbox.delete(index_func)
- func_listbox.insert(index_func,' y = ' + func + ' СКРЫТ')
- # Перерисовывает скрытый график
- def show_graphic(func, index_func):
- hided_graphics.remove(func)
- create_graphic(func, *params)
- func_listbox.delete(index_func)
- func_listbox.insert(index_func,' y = ' + func)
- def get_and_draw_graphic():
- func = ent_func.get()
- if func in functions:
- return
- functions.append(func)
- create_graphic(func, *params) # params = (middle_cord_x, middle_cord_y, delay)
- update_listbox('ADD')
- # Стерает выбранный график или последний, если график не выбран
- def erase_graphic():
- global functions, hided_graphics
- selection = func_listbox.curselection()
- if len(selection) == 0:
- func = functions[-1]
- if func in hided_graphics:
- return
- functions.pop()
- index_func = len(functions)
- for line in graphics[func]:
- canvas.delete(line)
- graphics.pop(func)
- update_listbox('DELETEFUNC', index_func)
- else:
- deleted_index = []
- for i in selection:
- index_func = selection[i]
- func = func_listbox.get(selection[i])[5:]
- if ' СКРЫТ' in func:
- func = func[:-6]
- functions.remove(func)
- hided_graphics.remove(func)
- deleted_index.append(index_func)
- else:
- functions.remove(func)
- for line in graphics[func]:
- canvas.delete(line)
- graphics.pop(func)
- deleted_index.append(index_func)
- deleted_index.sort(reverse=True)
- for i in deleted_index:
- update_listbox('DELETEFUNC', i)
- # def erase_last_graphic():
- # last_func = functions[len(functions)-1]
- # functions.pop()
- # for line in graphics[last_func]:
- # canvas.delete(line)
- # graphics.pop(last_func)
- # update_listbox('DELETELAST')
- # Удаляет все графики, а также очищает массивы
- def erase_graphics():
- global graphics, functions
- for function in graphics:
- for line in graphics[function]:
- canvas.delete(line)
- hided_graphics = []
- functions = []
- graphics = {}
- update_listbox('DELETEALL')
- def create_graphic(func, middle_cord_x, middle_cord_y, delay):
- graphic = []
- for x in range(-10000, 10000):
- try:
- last_x = (x - 1)/100
- last_y = set_y_func_value(last_x, func)
- x /= 100 # Уменьшение x для получения более точного рисунка графика
- y = set_y_func_value(x, func)
- last_x, last_y = set_coordinat(last_x, last_y, middle_cord_x, middle_cord_y, delay)
- # x = middle_cord_x + x*delay
- # y = middle_cord_y - (y*delay)
- x, y = set_coordinat(x, y, middle_cord_x, middle_cord_y, delay)
- graphic.append(canvas.create_line(last_x, last_y, x, y, width=3))
- except:
- continue
- graphics[func] = graphic
- def set_y_func_value(x, func):
- # Преобразование текстовой функции в исполняемый код
- y = eval(func)
- return y
- def set_coordinat(x, y, middle_cord_x, middle_cord_y, delay):
- x = middle_cord_x + x*delay
- y = middle_cord_y - (y*delay)
- return x, y
- def update_listbox(action, index=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 = ' + str(functions[-1]) )
- def main():
- global size
- global graphics, functions, hided_graphics
- graphics = {}
- functions = []
- hided_graphics =[]
- size_of_window = (1400, 1000)
- 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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement