来自元组的python 3x列表框内容不显示在单独的行上但重复了每一行

时间:2014-02-03 13:46:54

标签: python listbox tkinter tuples

我的代码创建了一个列表框并显示元组的内容以便我可以选择一个项目,麻烦的是列表框只显示每行的完整元组,而不是每行的每个元组项目,我确定它只是语法,但看不到它?这是一个创建元组的代码片段,列表框可​​以让任何人看到我出错的地方吗?

    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)
     for item in schema_names:
         lbox.insert(END, schema_names)
    # 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)
     lbox.bind('<Double-1>', connectStatus)
     mainframe.bind('<Return>', connectStatus)


   # Set the starting state of the interface, including selecting the
   # default gift to send, 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)
     conn.close()

    def connectStatus(*args):
        idxs = lbox.curselection()
        if len(idxs)==1:
            idx = int(idxs[0])
        lbox.see(idx)
        name = countrynames[idx]

    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=10)
    g0=Label(mainframe, text="Select Database")
    g1=Label(mainframe, text="Option:")
    g2=Radiobutton(mainframe, text=commands['dbconnect'], variable=gift, value='dbconnect')
    g3=Radiobutton(mainframe, text=commands['dbdelete'], variable=gift, value='dbdelete')
    send=Button(mainframe, text='Apply', command=(connectStatus), default='active')
    sentlbl=Label(mainframe, textvariable=sentmsg, anchor='center')
    status=Label(mainframe, textvariable=statusmsg, anchor=W)


    #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, columnspan=3, 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, pady=20)
    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=6, row=16, sticky=W, padx=20, pady=10);
    g3.grid(column=6, row=17, sticky=W, padx=20);
    send.grid(column=6, row=16, sticky=E)
    sentlbl.grid(column=3, row=22, columnspan=2, sticky=N, pady=5, padx=5);
    status.grid(column=4, row=23, columnspan=2, sticky=(W,E));

1 个答案:

答案 0 :(得分:2)

问题似乎是这段代码:

 for item in schema_names:
     lbox.insert(END, schema_names)

您是否尝试在插入语句中使用item而不是schema_names

 for item in schema_names:
     lbox.insert(END, item)

假设schema_names是一个元组,你也可以用这一行代替循环:

lbox.insert(END, *schema_names)