在赋值错误之前引用或tkinter类中没有此类属性错误

2024-04-19 16:14:12 发布

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

我试图使用Tkinter制作一个GUI,将某些文件加载到数据库中,但遇到了一个问题,根据我的代码是如何编写的,我要么在Tkinter GUI中使用的所有方法(因为我注释掉了其他方法以检查是否有单独工作的方法)都没有这样的属性错误,要么a referenced before assignment error。我得到的错误取决于我是否有command=self.acommandcommand=acommand。我可能做了错误的按钮代码,但我遵循的是一本书中给出的一个例子,这是有效的(尽管是一个更简单的程序)。你知道吗

我得到的错误是当我没有self时(这是用pick csv按钮代码注释掉的:

Traceback (most recent call last): File "C:/Python27/databasetest1.py", line 116, in main() File "C:/Python27/databasetest1.py", line 113, in main db_gui=DBGui() File "C:/Python27/databasetest1.py", line 48, in init text='Member Data in Alpabetical Order', command=listalphabetical) UnboundLocalError: local variable 'listalphabetical' referenced before assignment

当我使用这个时:

self.alphabeticallist_button = Tkinter.Button(self.main_window, \
        text='Member Data in Alpabetical Order', command=listalphabetical)

当我使用此代码时:

self.alphabeticallist_button = Tkinter.Button(self.main_window, \
        text='Member Data in Alpabetical Order', command=self.listalphabetical)

我得到这个错误:

Traceback (most recent call last): File "C:/Python27/databasetest1.py", line 116, in main() File "C:/Python27/databasetest1.py", line 113, in main db_gui=DBGui() File "C:/Python27/databasetest1.py", line 48, in init text='Member Data in Alpabetical Order', command=self.listalphabetical) AttributeError: DBGui instance has no attribute 'listalphabetical'

不管怎样,下面是我使用的类定义:

class DBGui:
    def __init__(self):
    #Create main window widget.
        self.main_window = Tkinter.Tk()


        print 'window created'




#Button to open file dialog box.
    self.choosefile_button = Tkinter.Button(self.main_window, \
                    text='Pick csv file', command=self.loadcsv)

#Buttons to use queries                                     
    self.alphabeticallist_button = Tkinter.Button(self.main_window, \
        text='Member Data in Alpabetical Order', command=listalphabetical)

    self.zipcodepaydatefilter_button = Tkinter.Button(self.main_window, \
    text='Members with zipcode 22101 who paid January', command=filterzippaydate)

    self.statejoindatefilter_button = Tkinter.Button(self.main_window, \
        text='members who joined since 1999-07-01 and live in VA', command=filterstatejoindate)

    self.namemembersfavloc_button = Tkinter.Button(self.main_window, \
        text ='Members and their favorite store+location', command=listmembersfavstore)

    self.membersfavtotalwine_button = Tkinter.Button(self.main_window, \
                    text='Members whose favorite wine store is Total Wine', command=listmembersfavtotalwine)

    self.quit_button = Tkinter.Button(self.main_window, \
        text='Quit', command=self.main_window.quit)                                             

    #Pack the buttons
    self.choosefile_button.pack()                                   
    self.alphabeticallist_button.pack()                                 
    self.zipcodepaydatefilter_button.pack()
    self.statejoindatefilter_button.pack()
    self.namemembersfavloc_button.pack()
    self.membersfavtotalwine_button.pack()
    self.quit_button.pack()





    def loadcsv(self):
            filename = tkFileDialog.askopenfilename(**self.file_opt)
            if filename.endswith('Members.csv'):
                    cur.executescript("""
                DROP TABLE IF EXISTS Members;
                CREATE TABLE Members (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12);
                """)
                    with open(filename, 'rb') as csv_file:
                                    data_csv = csv.DictReader(csv_file)
                                    to_db=[(i['col1'], i['col2'], i['col3'], i['col4'], i['col5'], i['col6'], i['col7'], i['col8'], i['col9'], i['col10'], i['col11'], i['col12']) for i in data_csv]
                                    cur.executemany("INSERT INTO Members (col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);", to_db)
                                    con.commit()
        #code for importing Members.csv into a Members table
            elif filename.endswith('Stores.csv'):
                    cur.executescript("""
                DROP TABLE IF EXISTS Stores;
                CREATE TABLE Stores (col1, col2, col3);
                """)
                    with open(filename, 'rb') as csv_file:
                                    data_csv = csv.DictReader(csv_file)
                                    to_db=[(i['col1'], i['col2'], i['col3']) for i in data_csv]
                                    cur.executemany("INSERT INTO Stores(col1, col2, col3) VALUES (?,?);", to_db)
                                    con.commit()
        #code for importing Stores.csv into a Stores table
            else:
                   print('Not a valid file.')



    def listalphabetical(self):
        cur=conn.execute("SELECT * FROM Members ORDER BY 'Last Name', 'First Name' ASC;")


    def filterzippaydate(self):
        cur=conn.execute("SELECT 'Member #', 'Last Name', 'First Name' FROM Members WHERE 'Zip Code' = 22101 AND 'Dues Paid' LIKE '%-01-%';")


    def filterstatejoindate(self):
        cur=conn.execute("SELECT 'Member #', 'Last Name', 'First Name' FROM Members WHERE 'State'= 'VA' AND 'Date Joined' >= '1999-07-01';")


    def listmembersfavstore(self):
        cur=conn.execute("SELECT 'Member #', 'Last Name', 'First Name', 'Favorite Store' FROM Members JOIN Stores ON Members.'Favorite Store'=Stores.'Store id';")


    def listmembersfavtotalwine(self):
        cur=conn.execute("SELECT 'Member #', 'Last Name', 'First Name', FROM Members WHERE 'Members.Favorite Store' = (SELECT 'Store id' FROM Stores WHERE Stores.'Store Name'= 'Total Wine');")

Tags: csvtextnameinselfmaintkinterbutton