using a .dat archive in python tkinter

42 Views Asked by At

So i wanted to make a code that would get a value and turn it into a graph plot, the below code is working and showing the content of the .dat file, but it's not what my teacher wanted lmao

from tkinter import Tk, Label
def read_archive():
    try:
        archive = open("name_of_the_archive.dat", "r")
        value = archive.read()
        archive.close()
        return value
    except FileNotFoundError:
        return "File not found"

window = Tk()
window.title("Value")

label_value = Label(window, text=read_archive(), font=("Arial", 18))
label_value.pack(pady=20)

window.mainloop()

Could somebody give me some info on how to do the graph, like a -pi to pi range or domething like that...thanks and sorry for my rookie programming and poor english :)

1

There are 1 best solutions below

0
Fanfansmilkyway On

Suppose that your .dat file looks like this:

[(-1,-2),(0,0),(1,2),(2,0),(3,3)]

This is a list saves all the coordinates of the points on the gragh. I recommend using matplotlib instead of tkinter because users can easily plot graghs on matplotlib. (Perhaps your teacher asks you to do all of these only with tkinter.)

The full code:

import matplotlib.pyplot as plt
import pickle # Use pickle to process .dat files

data = [(-1,-2),(0,0),(1,2),(2,0),(3,3)]
archive = open('data.dat', 'wb') # 'wb' means binary write
pickle.dump(data, archive)
archive.close()

def read_archive():
    try:
        load_file = open('data.dat', 'rb') # 'rb' means binary read
        data = pickle.load(load_file)
        load_file.close()
        return data
    except FileNotFoundError:
        return "File not found"

values = read_archive()
x = []
y = []
for coordinate in values:
    x.append(coordinate[0])
    y.append(coordinate[1])

plt.figure(num="Gragh")
plt.plot(x,y)
plt.show()