在Python中创建按钮函数之间的关系

2024-06-27 02:19:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想分别使用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:\Users\tt20172129\AppData\Local\Continuum\anaconda2\lib\lib-tk\Tkinter.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和代码都是新手。希望有人能帮助我,让我学习。:)


Tags: csvtextimportselftopdefbuttondynamic
2条回答

这是因为a的范围是:

def OnImport1(self):
    a = askopenfilename()

在范围中,没有定义:

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的范围内找到一个。你知道吗

Shripal Gandhi基本上是正确的,在Import1方法之外没有范围。这就是代码不起作用的原因。你知道吗

他的解决方案-一个全球-工作-但是粗糙的和一个伟大的反模式,为什么要使用一个全球。你知道吗

你必须在这里做一个架构上的决定。你知道吗

您必须存储a=askopenfilename()的结果,而不是将其放入全局范围,只需将其保留在类范围内即可。只是有自我。在a、b和c前面,然后您可以从任何其他类方法中引用它们,包括您想写出它的时候(下次出现这个bug时)

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' )

相关问题 更多 >