"""IMPORTANT. RENAME THIS FILE TO graph_app.py INSTEAD OF graph_app.txt""" """"application that converts mathmatical formulas in graphs""" __autheur__ = "prime-suspect" import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, Gdk, GdkPixbuf import matplotlib.pyplot as plt import numpy as np from matplotlib.figure import Figure from matplotlib.backends.backend_gtk3cairo import FigureCanvasGTK3Cairo as FigureCanvas from playsound import playsound from threading import Thread import random import re class GraphApp: def __init__(self): self.builder = Gtk.Builder() self.builder.add_from_file("graph_app.glade") # get the main window self.main_window = self.builder.get_object("main_window") self.formula_entry = self.builder.get_object("formula_entry") self.x_range_entry = self.builder.get_object("x_range_entry") self.y_range_entry = self.builder.get_object("y_range_entry") self.rounding_entry = self.builder.get_object("rounding_entry") self.plot_button = self.builder.get_object("plot_button") self.status_bar = self.builder.get_object("status_bar") # make css attributes self.status_bar.get_style_context().add_class("status_bar") self.formula_entry.get_style_context().add_class("entry") self.x_range_entry.get_style_context().add_class("entry") self.y_range_entry.get_style_context().add_class("entry") self.rounding_entry.get_style_context().add_class("entry") self.graph_window = self.builder.get_object("graph_window") self.plot_button.connect("clicked", self.on_plot) self.formula_entry.connect("activate", self.on_entry_activate) self.x_range_entry.connect("activate", self.on_entry_activate) self.y_range_entry.connect("activate", self.on_entry_activate) self.rounding_entry.connect("activate", self.on_entry_activate) self.canvas = None self.builder.connect_signals(self) self.load_css() # show the main window self.main_window.show_all() # startup music playsound('data/media/netMetro.wav') def load_css(self): css_provider = Gtk.CssProvider() css_provider.load_from_path("style.css") screen = Gdk.Screen.get_default() Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) def show_status_message(self, message): context_id = self.status_bar.get_context_id("status") self.status_bar.push(context_id, message) def on_plot(self, widget): self.plot_formula() def on_entry_activate(self, widget): self.plot_formula() def play_sound(self): # play sounds at random items = [1, 2, 3, 4] random_number = random.choice(items) if random_number == 1: playsound('data/media/btn_1.wav') elif random_number == 2: playsound('data/media/btn_2.wav') elif random_number == 3: playsound('data/media/btn_3.wav') elif random_number == 4: playsound('data/media/btn_4.wav') def plot_formula(self): threadsound = Thread(target=self.play_sound) threadsound.start() try: # get input values formula = self.formula_entry.get_text().strip() rounding_string = self.rounding_entry.get_text().strip() formula = formula.replace('π', str(np.pi)) formula = formula.replace('pi', str(np.pi)) formula = formula.replace('