mysql - Python 列表框在请求之前显示但在请求之后未填充

标签 mysql python-3.x listbox tkinter

我有一个 tkinter 列表框,应该列出 mysql information_schema 的内容,这样我就可以选择我想要连接的数据库,该列表也将转换为元组并显示在列表框中,列表框显示为空,因为我尚未连接,但在连接后仍然未填充,滚动条也没有连接到列表框,而是出现在窗口的右下角:

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)
    conn.close()

    # Figure out which country is selected, which gift is selected with the
    # radiobuttons, "send the gift", and provide feedback that it was sent.
    def sendGift(*args):
       idxs = lbox.curselection()
       if len(idxs)==1:
          idx = int(idxs[0])
    lbox.see(idx)
    name = countrynames[idx]
    # Gift sending left as an exercise to the reader
    sentmsg.set("%s %s" % (commands[gift.get()], name))

    # Note we're using the StringVar() 'cnames', constructed from 'countrynames'

    usertxt=Label(mainframe, text="User =")
    userlabel=Label(mainframe,text=usr)
    pathlabel=Label(mainframe, text="Project directory path is P:\\Projects_2013\\")
    projectdetaillabel=Label(mainframe, text="Project detail:")
    enternewname=Label(mainframe, text="Enter NEW project name:")
    ttk.Entry(mainframe, textvariable= assetVar).grid(column=4, row=2,columnspan=2, sticky=(W,E))
    createbutton=Button(mainframe, width=18,text="Create Folders", command=genAsset)
    selectlabel=Label (mainframe, text='Select an existing project:')
    browsebutton=Button(mainframe, width= 18,text="Browse", command=sel_Browse)
    prjtnm = Entry(mainframe, width=50)
    optionlabel=Label (mainframe, text="Project options:")
    completedbutton=Button(mainframe, text="Mark as completed", command=fldrRename)
    openbutton=Button(mainframe, width=18,text="Open Directory", command=Open_Win_Explorer_and_Select_Dir)
    shortcutlabel=Label(mainframe, text="Program Shortcuts:")
    xenonbutton=Button(mainframe, image=xenonimage, command=opn_xenon)
    wdebutton=Button(mainframe, image=webdateimage, command=opn_data_extr)
    wcebutton=Button(mainframe, image=webcontimage, command=opn_content_extr)
    htbutton=Button(mainframe, image=htimage, command=opn_ht)
    mysqllabel=Label(mainframe, text="Mysql:")
    mysqlbutton=Button(mainframe, width=18,text="MySql connect:",command=mysqlConnect)
    mysqlusr=Label(mainframe, text="Mysql Username:")
    mysqlusrentry=Entry(mainframe, width=18,textvariable= MysqlUsr)
    mysqlpasswordlabel=Label(mainframe, text="Mysql Password:")
    mysqlpwdentry=Entry(mainframe, width=18,textvariable= Mysqlpwd)
    lbox = Listbox(mainframe, listvariable=cnames, height=6)
    g0 = ttk.Label(mainframe, text="Select Database")
    g1 = ttk.Label(mainframe, text="Option:")
    g2 = ttk.Radiobutton(mainframe, text=commands['dbconnect'], variable=gift, value='dbconnect')
    g3 = ttk.Radiobutton(mainframe, text=commands['dbdelete'], variable=gift, value='dbdelete')
    send = ttk.Button(mainframe, text='Apply', command=(sendGift), default='active')
    sentlbl = ttk.Label(mainframe, textvariable=sentmsg, anchor='center')
    status = ttk.Label(mainframe, textvariable=statusmsg, anchor=W)

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


    #grid all the above
    usertxt.grid(column=1, row=1, sticky=E)
    userlabel.grid(column=2, row=1, sticky=W)
    pathlabel.grid(column=4, columnspan=2, row=1, sticky=W)
    projectdetaillabel.grid(column=1, row=2, sticky=E)
    enternewname.grid(column=2, row=2, columnspan=2, sticky=E, pady=15)
    lbox.grid(column=3, row=15, rowspan=6, sticky=(N,S,E,W))
    prjtnm.grid(row=3,column=4, columnspan=2)
    createbutton.grid(column=6, row=2, sticky=E, padx=10)
    selectlabel.grid(column=2,row=3,columnspan=2, sticky=E, pady=10)
    browsebutton.grid(row=3,column=6, sticky=W, padx=10)
    optionlabel.grid(row=4, column=2, columnspan=2, sticky=E)
    completedbutton.grid(row=4,column=4, sticky=W, padx=2)
    openbutton.grid(row=4,column=5, sticky=E, padx=2)
    shortcutlabel.grid(column=1, row=6,sticky=E, pady=60)
    xenonbutton.grid(row=6, column=3,sticky=E,)
    wdebutton.grid(row=6, column=4)
    wcebutton.grid(row=6, column=5)
    htbutton.grid(row=6,column=6,sticky=W,)
    mysqllabel.grid(column=1, row=8,sticky=E)
    mysqlbutton.grid(row=8, column=6, sticky=E, padx=10)
    mysqlusr.grid(row=8, column=2, sticky=E)
    mysqlusrentry.grid(row=8, column=3, sticky=W)
    mysqlpasswordlabel.grid(row=8, column=4, sticky=E)
    mysqlpwdentry.grid(row=8, column=5, sticky=W)
    g0.grid(column=3, row=14, sticky=W)
    g1.grid(column=4, row=14, sticky=W)
    g2.grid(column=4, row=16, sticky=W, padx=20, pady=10);
    g3.grid(column=4, row=17, sticky=W, padx=20);
    send.grid(column=5, row=16, sticky=E)
    sentlbl.grid(column=3, row=17, columnspan=2, sticky=N, pady=5, padx=5);
    status.grid(column=4, row=18, columnspan=2, sticky=(W,E));
    lbox.bind('<Double-1>', sendGift)
    root.bind('<Return>', sendGift)

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

    # Set the starting state of the interface, including selecting the
    # default command to connect, and clearing the messages.  Select the first
    # country in the list; because the <<ListboxSelect>> event is only
    # generated when the user makes a change, we explicitly call showPopulation.
    gift.set('dbconnect')
    sentmsg.set('')
    statusmsg.set('')
    lbox.selection_set(0)


    #add mysql pre-programmes queries here:
    ttk.Button(mainframe, width=18, text="Mysql query 1").grid(row=120, column=2, sticky=E)

    root.mainloop()

最佳答案

您报告的错误源于此行:

lbox = Listbox(mainframe, listvariable=cnames, height=6).grid(column=3, row=13, rowspan=6, sticky=(N,S,E,W))

在Python中,如果执行foo=bar().baz()foo将获取链中最后一个函数的结果。因此,当您执行 lbox = Listbox(...).grid(...) 时,lbox 会获取调用 grid 的结果,该结果始终为 。因此,lboxNone,因此当滚动条尝试与列表框同步时,您会收到错误NoneType object has no attribute yview

您应该始终将小部件的创建与布局分开。它避免了这个问题,并且在我看来,它使您的代码更易于编写、理解和维护。

关于mysql - Python 列表框在请求之前显示但在请求之后未填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21458546/

相关文章:

php - 从循环创建多个 MySQL 表?

java - OpenJPA 合并/持久化非常慢

vba - 当用 VBA 分配表达式中使用的控件时,列表框控件源错误

c# - 列表框在 SelectedIndexChanged 事件上返回空字符串

python - 如何使用 smtplib 查看电子邮件中的文本文件和图像文件

silverlight - 根据 Silverlight 中的绑定(bind)属性隐藏列表框项目中的内容

php - Wordpress 从帖子表中获取类别

mysql如何查找同一个表中两行之间的差异并列出不匹配的记录? mysql在表中查找不匹配的行

python - 使用 beautifulsoup 进行 HTML 解析适用于除我想要的 URL 之外的大多数 URL

python - 使用 beautiful soup 从 <td> 标签中提取正确格式的文本(中间有空格)