Tkinter:在新帧中加载/处理输入文件



我正在尝试使用Tkinter制作一个小程序,该程序将打开数据文件,对数据执行一些操作,并将其保存为新文件。

我想将输入文件的选择与其处理分开,因此我使用了两个不同的帧。但是,我在正确初始化ProcessPage框架时遇到了一些问题,因为在用户指定实际文件名之前,它总是读取我的(未定义的)文件名,试图加载数据。

最有可能的是,我要么错误地测试了全局变量,要么缺少了与ProcessPage的init__相比至关重要的东西。

如何确保在用户声明正确的文件之前,ProcessPage框架不会在init__期间尝试读取数据?

我对特金特还没什么经验,所以我可能会错过一些显而易见的东西。

谢谢!

编辑(2017年1月10日):向ProcessPage类添加了一个额外的方法(load_file);从get_filename调用了此方法;并将时间和数据声明为全局参数。然而,我不确定后者是否是一种好的做法。

import Tkinter as tk
from tkFileDialog import askopenfilename
import numpy as np
global time, data
time, data = np.array([]), np.array([])
class MainApp(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 = {}
for F in (LoadPage, ProcessPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(LoadPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class LoadPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = tk.Button(self, text='Browse', command = lambda: self.get_filename(controller))
button1.pack()
self.filename = tk.StringVar()
self.filename.set('No File Selected')
labelFile = tk.Label(self, textvariable=self.filename)
labelFile.pack()
button2 = tk.Button(self, text='Proceed and process my data', command = lambda: controller.show_frame(ProcessPage))
button2.pack()

def get_filename(self, controller):
global time, data
fname = askopenfilename()
if fname:
self.filename.set(fname)
# Extra line below  (10/01/2017)
controller.frames[ProcessPage].load_file(fname)
class ProcessPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# Extra method, as suggested   (10/01/2017)
def load_file(self, filename):
global time, data
time, data = np.loadtxt(filename, usecols=(0,1), unpack=True)

app = MainApp()
app.mainloop()

最终实现,其中调用方法ProcessPage.load_file()读取数据。数据被读入并存储为ProcessPage中的变量(self.time和self.data)。

数据是从LoadPage.start_processing()读取的,该页面现在链接到按钮2,因此数据的浏览和读取是分开的。

import Tkinter as tk
from tkFileDialog import askopenfilename
import numpy as np
class MainApp(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 = {}
for F in (LoadPage, ProcessPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(LoadPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class LoadPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button1 = tk.Button(self, text='Browse', command = self.get_filename)
button1.pack()
self.filename = tk.StringVar()
self.filename.set('No File Selected')
labelFile = tk.Label(self, textvariable=self.filename)
labelFile.pack()
button2 = tk.Button(self, text='Proceed and process my data', command = lambda: self.start_processing(controller))
button2.pack()

def get_filename(self):
fname = askopenfilename()
if fname:
self.filename.set(fname)
return
# Extra method below  (10/01/2017)
def start_processing(self, controller):
controller.frames[ProcessPage].load_file(self.filename.get())
controller.show_frame(ProcessPage)
class ProcessPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# Initialised the variables (10/01/2017)
self.time = np.array([])
self.data = np.array([])
# Extra method, as suggested   (10/01/2017)
def load_file(self, filename):
self.time, self.data = np.loadtxt(filename, usecols=(0,1), unpack=True)   
app = MainApp()
app.mainloop()

最新更新