我正在尝试使用Tkinter创建一个小程序,它将打开您的数据文件,对数据执行某些操作,并将其保存为新文件。
我想将输入文件的选择与其处理分开,因此我使用了两个不同的帧。但是,我在正确初始化ProcessPage框架时遇到了一些问题,因为在用户指定实际文件名之前,它总是以我的(未定义的)文件名读取,尝试加载数据。
最有可能的是,我错误地测试了全局变量,或者缺少与ProcessPage的init__相比至关重要的东西。
如何在用户声明正确文件之前确保ProcessPage框架在init__期间不尝试读入数据?
我对Tkinter相当缺乏经验,所以我可能会遗漏一些明显的东西。
谢谢!
编辑(10/01/2017):在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()
答案 0 :(得分:1)
最终实现,其中调用方法ProcessPage.load_file()来读取数据。读入数据并将其存储为ProcessPage中的变量(self.time和self.data)。
从LoadPage.start_processing()读取数据,该页面现在链接到button2,因此数据和读入的浏览是分开的。
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()