python'function'没有属性

时间:2014-02-06 11:54:06

标签: python function python-3.x attributes

我一直在推动这个错误,整天试图解决它并决定我需要帮助,我猜它告诉我'countrynames'要么没有定义,要么没有填充或有任何值,但是它被使用了以前在类'mysqlbusiness'确定,我试图显示一个简单的文本行,显示从下拉列表中选择了什么项目,列表由元组填充,并且通过无线电选择要应用的命令按钮,消息是一个礼貌的笔记!

TKinter错误是这样的:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
  File "S:\python\jon\wrt_toolkit_v5\toolbox_v7.py", line 141, in sendGift
    name = mysqlbusiness.mysqlConnect.countrynames[idx]
AttributeError: 'function' object has no attribute 'countrynames'

代码段就是这个,

class mysqlbusiness:

    def mysqlConnect():
        import pymysql
        sqlUsr = MysqlUsr.get()
        sqlpwd = Mysqlpwd.get()
        conn = pymysql.connect(host='192.168.0.27', user= sqlUsr, passwd=sqlpwd, db='information_schema')
        cursor = conn.cursor()
        cursor.execute("SELECT SCHEMA_NAME FROM SCHEMATA")
        schema_names = cursor.fetchall()
        schema_tuple = tuple(schema_names)
        countrynames = (schema_tuple)
        cnames = (countrynames)
        lbox.insert(END, *schema_names)
        # Colorize alternating lines of the listbox
        for i in range(0,len(cnames),2):
            lbox.itemconfigure(i, background='#CEECF5')
        # create a vertical scrollbar to the right of the listbox
        yscroll = tk.Scrollbar(command=lbox.yview, orient=tk.VERTICAL)
        yscroll.grid(row=15, column=6, sticky='ns')
        lbox.configure(yscrollcommand=yscroll.set)
        lbox.selection_set(0)
        conn.close()

    # Called when the user double clicks an item in the listbox, presses
    # the "apply" button, or presses the Return key.  In case the selected
    # item is scrolled out of view, make sure it is visible.
    #
    # Figure out which schema is selected, which command (connect or delete) is selected with the radiobuttons, and provide feedback.
    def listConnection(*args):
        idxs = lbox.curselection()
        if len(idxs)==1:
            idx = int(idxs[0])
        lbox.see(idx)
        name = mysqlbusiness.mysqlConnect.countrynames[idx]
        # Gift sending left as an exercise to the reader
        sentmsg.set("%s %s" % (gifts[gift.get()], name))
        lbox.bind('<Double-1>', listConnection)
        mainframe.bind('<Return>', listConnection)

1 个答案:

答案 0 :(得分:0)

你犯了几个错误:

  • 您正在使用类但仅定义了函数。不要使用类来创建名称空间,Python不是Java,也不是要求类。

  • 您的mysqlConnect()函数不会返回任何内容。您无法在函数中引用本地名称。

  • 您实际上并未调用mysqlConnect()函数。

  • 您正在混合数据检索和更新UI;最好将其拆分为单独的功能。

  • 您没有从行中提取列;数据库中的每一行结果都是一个带有列的元组(在本例中为一个)。

  • 您正在为同一结果集使用多个变量名称;例如,没有必要真正使结果成为元组。

  • 每次调用listConnection时都不需要重新绑定事件处理程序。

更正后的代码:

import pymysql

def countrynames():
    sqlUsr = MysqlUsr.get()
    sqlpwd = Mysqlpwd.get()
    conn = pymysql.connect(host='192.168.0.27', user=sqlUsr, 
                           passwd=sqlpwd, db='information_schema')
    cursor = conn.cursor()
    cursor.execute("SELECT SCHEMA_NAME FROM SCHEMATA")
    countrynames = [row[0] for row in cursor]
    conn.close()
    return countrynames

def set_countrynames_lbox(countrynames)
    lbox.insert(END, *countrynames)

    # Colorize alternating lines of the listbox
    for i in range(0, len(countrynames), 2):
        lbox.itemconfigure(i, background='#CEECF5')

    # create a vertical scrollbar to the right of the listbox
    yscroll = tk.Scrollbar(command=lbox.yview, orient=tk.VERTICAL)
    yscroll.grid(row=15, column=6, sticky='ns')
    lbox.configure(yscrollcommand=yscroll.set)
    lbox.selection_set(0)


# Called when the user double clicks an item in the listbox, presses
# the "apply" button, or presses the Return key.  In case the selected
# item is scrolled out of view, make sure it is visible.
#
# Figure out which schema is selected, which command (connect or delete) is selected with the radiobuttons, and provide feedback.
def listConnection(*args):
    idxs = lbox.curselection()
    if len(idxs)==1:
        idx = int(idxs[0])
    lbox.see(idx)

    cnames = countrynames()
    # optional, set the lbox again with
    # set_countrynames_lbox(cnames)

    name = cnames[idx]

    # Gift sending left as an exercise to the reader
    sentmsg.set("%s %s" % (gifts[gift.get()], name))

# moved out of the functions
lbox.bind('<Double-1>', listConnection)
mainframe.bind('<Return>', listConnection)