我想分别使用 3 个按钮导入 3 个 csv 文件,并将它们合并为一个 csv 文件并使用按钮单击保存。这是我的代码:
from Tkinter import *
from Tkinter import Tk
from tkFileDialog import askopenfilename
import pandas as pd
from tkFileDialog import asksaveasfilename
import time
class Window(Tk):
def __init__(self, parent):
Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.geometry("600x400+30+30")
self.wButton = Button(self, text='Product Price List', command = self.OnButtonClick)
self.wButton.pack()
def OnButtonClick(self):
self.top = Toplevel()
self.top.title("Product Price List")
self.top.geometry("300x150+30+30")
self.top.transient(self)
self.wButton.config(state='disabled')
self.topButton = Button(self.top, text="Import Price list CSV", command = self.OnImport1)
self.topButton.pack()
self.topButton = Button(self.top, text="Import Price Adjustment CSV", command = self.OnImport2)
self.topButton.pack()
self.topButton = Button(self.top, text="Import Price Adjustment CSV", command = self.OnImport3)
self.topButton.pack()
self.topButton = Button(self.top, text="Save As", command = self.OnSaveAs)
self.topButton.pack()
self.topButton = Button(self.top, text="CLOSE", command = self.OnChildClose)
self.topButton.pack()
def OnImport1(self):
a = askopenfilename()
def OnImport2(self):
b = askopenfilename()
c = a.OnImport1.merge(b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
def OnImport3(self):
d = askopenfilename()
d = d.dropna(axis=0)
g = d.groupby('Dynamic_spcMatrix')['Attribute_spcName'].apply(lambda x: ', '.join(x.astype(str))) #join attributes usin commas
c['Attribute_spcName'] = c['Dynamic_spcMatrix'].map(g)
c = c[['Type', 'Name', 'Currency_spcCode', 'Product_spcCfg_spcModel_spcId', 'Product_spcName', 'Attribute_spcName', 'Matrix_spcType', 'Start_spcDate', 'End_spcDate', 'Original_spcList_spcPrice', 'Max_spcSale_spcPrice', 'Min_spcSale_spcPrice', 'String_spcMatrix_spcColumn_spc1', 'String_spcMatrix_spcColumn_spc2', 'String_spcMatrix_spcColumn_spc3', 'String_spcMatrix_spcColumn_spc4','Number_spcMatrix_spcColumn_spc1']]
def OnSaveAs(self):
dlg = asksaveasfilename(confirmoverwrite=False)
fname = dlg
if fname != '':
f = open(fname, "a")
new_text = time.time()
f.write(str(new_text)+'n')
f.close()
c.to_csv(fname, index=False)
def OnChildClose(self):
self.wButton.config(state='normal')
self.top.destroy()
if __name__ == "__main__":
window = Window(None)
window.title("Create Csv")
window.mainloop()
我想分别使用 3 个按钮导入 3 个 csv 文件,并将它们合并为一个 csv 文件并使用按钮单击保存。当我运行此错误时,会发生以下错误。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Userstt20172129AppDataLocalContinuumanaconda2liblib-tkTkinter.py", line 1541, in __call__
return self.func(*args)
File "<ipython-input-15-64436dd12913>", line 51, in OnImport2
c = a.OnImport1.merge(b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
NameError: global name 'a' is not defined
我是python和编码的新手。 希望有人可以帮助我,这样我就可以学习。:)
Shripal Gandhi 在基本方面是正确的,a 在 Import1 的方法之外没有范围。 这就是代码不起作用的原因。
他的解决方案 - 全局 - 有效 - 但很粗糙,并且是为什么使用全局的巨大反模式。
你必须在这里做出架构决定。
你必须存储 a = askopenfilename(( 的结果,而不是放入全局范围,只需将其保留在类范围内即可。 只是有自己。在 A、B 和 C 前面,然后你可以从任何其他类方法中引用它们,包括当你想写出来的时候(下次这个错误会出现(
def OnImport1(self):
self.a = askopenfilename()
def OnImport2(self):
self.b = askopenfilename()
def OnImport3(self):
self.c = self.a.OnImport1.merge(self.b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
这是因为 a 的作用域是:
def OnImport1(self):
a = askopenfilename()
在作用域中,未定义 a。
def OnImport2(self):
b = askopenfilename()
c = a.merge(b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
将代码更新为:
a = #Use askopenfilename() return type object to initialize
def OnImport1(self):
global a
a = askopenfilename()
def OnImport2(self):
global a
b = askopenfilename()
c = a.merge(b, how='left', left_on='Dynamic_spcMatrix', right_on='Dynamic_spcMatrix' )
它将能够在OnImport2的范围内找到一个。