我创建了一个程序,该文件从包含x,y,z(z始终为0)的文本文件创建热图,以及201 x 201像素区域中每个像素的强度值。我现在正尝试使用Tkinter创建一个GUI,允许我浏览文本文件,然后显示我选择的文件的热图,但我遇到了一些问题。这是我到目前为止的代码:
import numpy as np
import matplotlib
import matplotlib.cm as cm
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import Tkinter as tk
import ttk as ttk
from tkFileDialog import askopenfilename
f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
a.clear()
def Separate(image):
'''Separate columns of text file into arrays'''
img = np.loadtxt(image)
intensity = np.array(image[:,0])
x = np.array(image[:,1])
y = np.array(image[:,2])
z = np.array(image[:,3])
return intensity, x, y, z
def constructImage(intensity, x, y):
'''Create a heatmap of the data'''
lenx = int(np.amax(x) - np.amin(x) + 1)
leny = int(np.amax(y) - np.amin(y) + 1)
intensity2 = intensity.reshape(lenx, leny)
a.clear()
a.imshow(intensity2, extent = (np.amin(x), np.amax(x), np.amin(y), np.amax(y)))
def SeparateandConstruct(image):
img = np.loadtxt(image)
intensity, x, y, z = Separate(img)
constructImage(intensity, x, y)
LARGE_FONT= ("Verdana", 12)
def callback():
'''Opens text file and runs Separate function'''
name = askopenfilename()
return name
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
frame = ImageConstructor(container, self)
self.frames[ImageConstructor] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(ImageConstructor)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
#Image Constructor Frame
class ImageConstructor(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
#Title Label
label = ttk.Label(self, text="Image Constructor", font=LARGE_FONT)
label.pack(pady=10,padx=10)
#Button to browse for a text file
browseButton = ttk.Button(self, text="Browse", command=callback)
browseButton.pack()
#Label displaying the path to the selected text file
filePath = ttk.Label(self, text="No File Selected", font=LARGE_FONT)
filePath.pack()
#Button that creates an image of the selected text file
constructButton = ttk.Button(self, text="Construct Image")
constructButton.pack()
#Canvas for the heatmap
canvas = FigureCanvasTkAgg(f, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
#Button to close the program
quitButton = ttk.Button(self, text="Quit", command=controller.destroy)
quitButton.pack()
def main():
app = Application()
app.mainloop()
if __name__ == '__main__':
main()
我遇到的问题是,在使用浏览按钮后,我不确定如何保存回调函数的输出。我想让filePath标签更新以显示所选文本文件的文件路径,我还想让constructButton将SeparateandConstruct函数应用于我选择的文本文件,但我不知道如何保存输出回调函数。如果我使用tkinter按钮运行它,有没有办法将函数的输出保存到变量?如果我知道如何做到这一点,我觉得这很容易上班,但我还没能弄明白。
答案 0 :(得分:1)
以下是如何使回调修改类实例属性的简短演示。关键的想法是使回调成为类的方法,因此它可以访问类的属性。
我在这里使用它来更新附加到文件名Label的tk.StringVar
。
import Tkinter as tk
from tkFileDialog import askopenfilename
class Demo(object):
def __init__(self):
master = tk.Tk()
frame = tk.Frame(master)
frame.pack()
b = tk.Button(frame, text="Browse", command=self.getfilename_cb)
b.pack()
self.filename = tk.StringVar()
self.filename.set("No File Selected")
l = tk.Label(frame, textvariable=self.filename)
l.pack()
master.mainloop()
def getfilename_cb(self):
fname = askopenfilename()
if fname:
self.filename.set(fname)
Demo()