我有一个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()
答案 0 :(得分:0)
您报告的错误源于此行:
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
会获得对网格调用的结果,该结果始终为None
。因此,lbox
为None
,因此当滚动条尝试与列表框同步时,您会收到错误NoneType object has no attribute yview
。
您应始终将窗口小部件的创建与布局分开。它避免了这个问题,在我看来,它使您的代码更容易编写,理解和维护。