I am new to GUI programming and I’m trying to create a graph that takes in user input simultaneously while plotting the points given. I am still at the start, I am taking double entries and turning them into a list of tuples, and saving the entries in a text document. I have worked this all out but as I keep giving new input, the text document is erasing the previous input. Can you tell me how to save the previous inputs so I can use them to plot graphs?
here is my code till now, I’m working on y=mx+c graph for now:
JavaScript
x
33
33
1
from tkinter import *
2
import matplotlib
3
import matplotlib.pyplot as plt
4
import matplotlib.animation as animation
5
import math
6
import numpy as np
7
from random import randint
8
from matplotlib.widgets import Slider
9
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
10
11
matplotlib.use('TkAgg')
12
13
x=[]
14
m =[]
15
mywidgets=[]
16
17
def get_entry():
18
with open('data.txt', 'w') as out:
19
for E1, E2 in mywidgets:
20
out.write(E1.get()+" "+E2.get()+'n')
21
22
root = Tk()
23
root.title("Y=mX+c Graph")
24
label1= Label(root,text="m").grid(row=0,column=0)
25
label2 =Label(root,text="x").grid(row=0,column=3)
26
e1 = Entry(root)
27
e2=Entry(root)
28
e1.grid(row=0, column=1, padx=5, pady=10)
29
e2.grid(row=0, column=4, padx=5, pady=10)
30
mywidgets.append((e1,e2))
31
button = Button(root, text="Plot the Graph",command=get_entry).grid(row=0,column=5)
32
root.mainloop()
33
Advertisement
Answer
A better way to implement this program is to ask the user for values m and c, where x can span a range. For this example, make sure that the text file is in the same folder as the program.
Here is my adapted example:
JavaScript
1
106
106
1
from tkinter import *
2
import matplotlib
3
import matplotlib.pyplot as plt
4
import matplotlib.animation as animation
5
import math
6
import numpy as np
7
from random import randint
8
from matplotlib.widgets import Slider
9
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
10
from matplotlib.figure import Figure
11
12
13
14
#Change the path to that of your.txt file
15
f = open("p.txt", "r")
16
17
#Function that quits program when user closes window
18
def quit_me():
19
print('Thank you for using the program')
20
root.quit()
21
root.destroy()
22
23
#Plots chart when button "Plot" is pressed
24
def plotData():
25
26
#Clears previous chart
27
plt.clf()
28
#Size of chart
29
fig = Figure(figsize = (4.5, 2.7),
30
dpi = 100)
31
canvas = FigureCanvasTkAgg(fig,
32
master = root)
33
canvas.draw()
34
35
#Turns user input into integers
36
m = int(e1.get())
37
c = int(e2.get())
38
39
#Here you can set the range of your graph
40
y = [m*x + c for x in range(10)]
41
42
#Adding the subplot
43
plot1 = fig.add_subplot(111)
44
45
#Plotting the graph
46
plot1.plot(y)
47
#Placing the canvas on the Tkinter window
48
canvas.get_tk_widget().place(x=25, y=70)
49
50
#Appends the user inputed m and c values delimited by a comma onto the chosen .txt file
51
with open("p.txt", "a+") as file_object:
52
#Move read cursor to the start of file.
53
file_object.seek(0)
54
#If file is not empty then append 'n'
55
data = file_object.read(100)
56
if len(data) > 0 :
57
file_object.write("n")
58
#Appends text at the end of file on a new line.
59
file_object.write(("%s,%s") % (m,c))
60
61
62
root = Tk()
63
64
root.geometry("500x400")
65
66
#Empty Plot placeholder
67
68
fig = Figure(figsize = (4.5, 2.7),
69
dpi = 100)
70
canvas = FigureCanvasTkAgg(fig,
71
master = root)
72
canvas.draw()
73
y = [0]
74
plot1 = fig.add_subplot(111)
75
plot1.plot(y)
76
canvas.get_tk_widget().place(x=25, y=70)
77
78
79
root.title("Y=mx+c Graph")
80
81
#Label for M value
82
lbl=Label(root, text="Type in M Value:", fg='black', font=("Helvetica", 12))
83
#Position of M value label
84
lbl.place(x=0, y=0)
85
86
#Label for C value
87
lbl2=Label(root, text="Type in C Value:", fg='black', font=("Helvetica", 12))
88
#Position of C label
89
lbl2.place(x=200, y=0)
90
91
#M user input value
92
e1 = Entry(root)
93
e1.place(x=130,y=5,width=60)
94
95
#C user input value
96
e2 = Entry(root)
97
e2.place(x=330,y=5,width=60)
98
99
100
btn = Button(root, text="Plot", width=4, command=lambda:plotData())
101
btn.place(x=415, y=2)
102
103
104
root.protocol("WM_DELETE_WINDOW", quit_me)
105
root.mainloop()
106