Python Forum
User Interaction with Graph in GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User Interaction with Graph in GUI
#1
So I am relatively new to gui's in python, but I am trying to make an interactive graph, such that a user enters in 3 values in the boxes given, this is called "sample_point" and an 'X' symbol is then plotted on the graph. The issue I am having is I want the graph to be present when the program is run, not after the button is pressed. I also want the 'X' to change according to the values entered by the user on the graph, 'X' will only change position when the user has pressed the button after changing the input in the boxes. I have been using the point 41,41,1 in the boxes to test. I hope someone can shed some light to my issues!

import tkinter as Tk
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure 
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,  
NavigationToolbar2Tk) 

class App(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.geometry("500x500") 
        self.root.wm_title("DGA Analysis Tool Using Duval Triangle")
        self.label = Tk.Label(self.root, text="C2H2")
        self.label.pack()

        self.C2H2 = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.C2H2).pack()

        self.C2H4 = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.C2H4).pack()

        self.CH4 = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.CH4).pack()

        self.buttontext = Tk.StringVar()
        self.buttontext.set("Calculate")
        
        Tk.Button(self.root,
                  textvariable=self.buttontext,
                  command=self.clicked1).pack()

        self.label = Tk.Label(self.root, text="")
        self.label.pack()

        fig = Figure(figsize = (5, 5), 
                     dpi = 100)
        
        
        self.root.mainloop()
    

    def clicked1(self):
        C2H2_CON = self.C2H2.get()
        C2H4_CON = self.C2H4.get()
        CH4_CON = self.CH4.get()

         
        #
        # Define the transformation matrix
        #
        A = np.array([[5, 2.5, 50], [0, 5 * np.sqrt(3)/2, 50], [0, 0, 1]])

        #
        # Define a set of points for Duval triangle regions
        #
        p = np.array([
            [0, 0, 1],          #0-p1
            [0, 100, 1],        #1-p2
            [100, 0, 1],        #2-p3
            [0, 87, 1],         #3-p4
            [0, 96, 1],         #4-p5
            [0, 98, 1],         #5-p6
            [2, 98, 1],         #6-p7
            [23, 0, 1],         #7-p8
            [23, 64, 1],        #8-p9
            [20, 76, 1],        #9-p10
            [20, 80, 1],        #10-p11
            [40, 31, 1],        #11-p12
            [40, 47, 1],        #12-p13
            [50, 35, 1],        #13-p14
            [50, 46, 1],        #14-p15
            [50, 50, 1],        #15-p16
            [71, 0, 1],         #16-p17
            [85, 0, 1]])        #17-p18

        #
        # Apply the coordinates transformation to all points
        #
        v = p @ np.transpose(A)

        #
        # Set one more sample point
        #

        sample_point = np.array([int(C2H2_CON), int(C2H4_CON), int(CH4_CON)]) @ np.transpose(A)
        
        #
        # Define each of the regions by the coordinates of its angle points
        #
        region_PD = v[[5, 1, 6], :]
        region_T1 = v[[4, 5, 6, 10, 9], :]
        region_T2 = v[[9, 10, 15, 14], :]
        region_T3 = v[[13, 15, 2, 17], :]
        region_D1 = v[[0, 3, 8, 7], :]
        region_D2 = v[[7, 8, 12, 11, 16], :]
        region_DT = v[[3, 4, 14, 13, 17, 16, 11, 12], :]

        #
        # Plot the results
        #
        fig, ax1 = plt.subplots()
        ax1.fill(region_PD[:, 0], region_PD[:, 1], '#2e962d')
        ax1.fill(region_T1[:, 0], region_T1[:, 1], '#bebe12')
        ax1.fill(region_T2[:, 0], region_T2[:, 1], '#ff642b')
        ax1.fill(region_T3[:, 0], region_T3[:, 1], '#b46414')
        ax1.fill(region_D1[:, 0], region_D1[:, 1], '#10b4a7')
        ax1.fill(region_D2[:, 0], region_D2[:, 1], '#121eb4')
        ax1.fill(region_DT[:, 0], region_DT[:, 1], '#f217d0')
        ax1.scatter(sample_point[0], sample_point[1], marker='x', c='r', zorder=2)
        ax1.grid(linestyle='--', alpha=0.4, axis='both')

        #
        # Also place axes captions
        #
        label1 = np.array([45, -5, 1]) @ np.transpose(A)
        ax1.text(label1[0], label1[1], '%C2H2')
        label11 = np.array([95, -5, 1]) @ np.transpose(A)
        ax1.text(label11[0], label11[1], '0')
        label12 = np.array([5, -5, 1]) @ np.transpose(A)
        ax1.text(label12[0], label12[1], '100')
        label2 = np.array([-10, 55, 1]) @ np.transpose(A)
        ax1.text(label2[0], label2[1], '%CH4')
        label21 = np.array([-7, 5, 1]) @ np.transpose(A)
        ax1.text(label21[0], label21[1], '0')
        label22 = np.array([-7, 95, 1]) @ np.transpose(A)
        ax1.text(label22[0], label22[1], '100')
        label3 = np.array([45, 55, 1]) @ np.transpose(A)
        ax1.text(label3[0], label3[1], '%C2H4')
        label31 = np.array([5, 95, 1]) @ np.transpose(A)
        ax1.text(label31[0], label31[1], '0')
        label22 = np.array([95, 5, 1]) @ np.transpose(A)
        ax1.text(label22[0], label22[1], '100')

        #
        # Show the final plot
        #
        ax1.set_xlim(0, 600)
        ax1.set_ylim(0, 550)
        
        canvas = FigureCanvasTkAgg(fig, self.root)   
        canvas.draw()
        
        # placing the canvas on the Tkinter window 
        canvas.get_tk_widget().pack() 
      
        # creating the Matplotlib toolbar 
        toolbar = NavigationToolbar2Tk(canvas, 
                                       self.root) 
        toolbar.update() 
      
        # placing the toolbar on the Tkinter window 
        canvas.get_tk_widget().pack()
        
      
        



App()
Reply


Messages In This Thread
User Interaction with Graph in GUI - by Marty23 - Mar-18-2024, 08:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with tkinter Button and Label interaction issues ... sealyons 0 4,760 Jun-01-2017, 06:58 PM
Last Post: sealyons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020