Python Forum
[Tkinter] How do I open a file and the plot it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How do I open a file and the plot it?
#1
I want to create a GUI that allows me to read a text file into a variable by pressing a button. Then, I want to plot a histogram out of the data from the text file. The text file is just a 1D array of many values. Here is what I have so far:
    from tkinter import *
    from tkinter import ttk
    from tkinter import filedialog
    import matplotlib.pyplot as plt
    
    root = Tk()
    root.geometry('800x800')
    
    def openfile():
        global peaks
        filename = filedialog.askopenfilename()
        peaks = open(filename).read()
    
    def plot():
        fig2,ax2 = plt.subplots()
        ax2.hist(peaks,1000)
    
    button = ttk.Button(root, text="Open", command=openfile)
    button.grid(column=1, row=1)
    
    plot = ttk.Button(root,text='Plot',command = plot)
    plot.grid(column=2,row=1)
    
    root.mainloop()
From what I have at the moment, it seems like I can open a file, but I am not sure if it is being read into the global variable peaks. Then, when I press the Plot button, the code just stalls and then crashes. I need help with figuring out what I am doing wrong. Thank you.

Note: this is my first time dealing with Tkinter, so if there are better way to do this, I am open to suggestions.
Reply
#2
if plotting a histogram, don't you want to accumulate the data being read in by value?
Or is that already done (file in an ordered list of means)?
it would be most helpful to show a few lines of the file data.
Reply
#3
Here is the code I use to generate the data:
np.random.seed(1)  # for repeatability

D0 = stats.norm.rvs(5340,13.4,size=12000)
D1 = stats.norm.rvs(5423,13.4,size=36500)  
D2 = stats.norm.rvs(5685,14.2,size=45000)
D3 = stats.norm.rvs(6051,14.1,size=9000)
D4 = stats.norm.rvs(6090,14.2,size=3500)
D5 = stats.norm.rvs(6288,14.1,size=42000)
D6 = stats.norm.rvs(6778,14.9,size=43000)
D7 = stats.norm.rvs(8785,14.4,size=25000)

allpeaks = np.concatenate((D0,D1,D2,D3,D4,D5,D6,D7))
#fig2,ax2 = plt.subplots()
#ax2.hist(allpeaks,1000)

np.savetxt('228Thorium.txt',allpeaks)
Please uncomment the two lines to see the histogram.
Reply
#4
The problem is solved by doing this:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import matplotlib.pyplot as plt
from numpy import loadtxt

root = Tk()
root.geometry('800x800')

def openfile():
    global filename
    filename = filedialog.askopenfilename()
    print(filename)

def plot():
    data = loadtxt(filename,float)
    fig2,ax2 = plt.subplots()
    ax2.hist(data,1000)

button = ttk.Button(root, text='Open', command=openfile)
button.grid(column=1, row=1)

plot_bttn = ttk.Button(root, text='Plot', command=plot)
plot_bttn.grid(column=2,row=1)

root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] filedialog, open a file error liketocode 4 3,352 Dec-07-2022, 10:51 PM
Last Post: liketocode
  [PyQt] QFSFileEngine::open: No file name specified MegasXLR 1 4,027 May-25-2018, 04:06 PM
Last Post: MegasXLR

Forum Jump:

User Panel Messages

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