Python 2.7.8 TypeError:';unicode';对象不可调用
我正在运行python版本2.7.8。我将项目标题存储在一个sqlite数据库中,我正试图将其导入一个组合框中。当我尝试从sqlite数据库导入项目标题并将其导入combobox时,会出现以下错误:TypeError:“unicode”对象不可调用 这是我的密码: main.pyPython 2.7.8 TypeError:';unicode';对象不可调用,python,sqlite,python-2.7,unicode,wxpython,Python,Sqlite,Python 2.7,Unicode,Wxpython,我正在运行python版本2.7.8。我将项目标题存储在一个sqlite数据库中,我正试图将其导入一个组合框中。当我尝试从sqlite数据库导入项目标题并将其导入combobox时,会出现以下错误:TypeError:“unicode”对象不可调用 这是我的密码: main.py from EvDB import EvDB import wx from ProjectsPanel import ProjectsPanel class MyFrame(wx.Frame): """ We
from EvDB import EvDB
import wx
from ProjectsPanel import ProjectsPanel
class MyFrame(wx.Frame):
""" We simply derive a new class of Frame. """
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title=title, size=(650,725))
# Create Database Tables
self.db = EvDB(self)
self.db.createTbls()
main = wx.Panel(self)
self.projectsPg = ProjectsPanel(main, -1)
self.mainSizer = wx.BoxSizer(wx.VERTICAL)
self.mainSizer.Add(self.projectsPg, 1, wx.ALL|wx.EXPAND)
main.SetAutoLayout(True)
main.SetSizer(self.mainSizer)
self.mainSizer.Fit(main)
self.Layout()
self.Show()
self.UserID = 1
rows = self.db.getProjects(self.UserID)
print(rows)
print(str(rows))
self.projectsPg.cb.Value(rows)
app = wx.App(False)
ProjectsPanel.py
import wx
from EvDB import EvDB
import sqlite3 as lite
class ProjectsPanel(wx.Panel):
def __init__(self, parent, ID):
wx.Panel.__init__(self, parent, ID)
self.db = lite.connect('evDB.db')
self.cur = self.db.cursor()
self.db2 = EvDB(self)
sizer = wx.BoxSizer(wx.VERTICAL)
# the combobox Control
self.userProjects = ['Before1','Before2', 'Before3', 'Before4']
self.cb = wx.ComboBox(self, value='New', choices=self.userProjects, style=wx.CB_DROPDOWN)
sizer.Add(self.cb, 0, wx.EXPAND)
# Setting Layouts
self.SetAutoLayout(True)
self.SetSizer(sizer)
sizer.Fit(self)
EvDB.py
import sqlite3 as lite
class EvDB():
def __init__(self, parent):
self.db = lite.connect('evDB.db')
self.cur = self.db.cursor()
def createTbls(self):
self.db.execute('''CREATE TABLE IF NOT EXISTS Projects
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
Title varchar(50) DEFAULT NULL,
DateCreated DATE);''')
self.db.execute('''CREATE TABLE IF NOT EXISTS User_x_Project
(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
UserID INT DEFAULT NULL,
ProjectID INT DEFAULT NULL);''')
def getProjects(self,userID):
self.cur.execute("SELECT Title FROM User_x_Project, Projects WHERE User_x_Project.UserID = "+str(userID)+" AND User_x_Project.ProjectID = Projects.ID;")
rows = self.cur.fetchall()
return rows
打印结果:[(u'Tests',),(u'Test1',),(u'Test2',),(u'Test3',)]
如何将存储在sqlite数据库中的标题添加到组合框中
谢谢你的帮助
编辑:这是我的全部回溯
C:\Python27\python.exe C:/Users/xxx/xxx/xxx/main.py
[(u'Tests',), (u'Test1',), (u'Test2',), (u'Test3',)]
[(u'Tests',), (u'Test1',), (u'Test2',), (u'Test3',)]
Traceback (most recent call last):
File "C:/Users/xxx/xxx/xxx/main.py", line 43, in <module>
frame = MyFrame(None, -1, 'Small editor')
File "C:/Users/xxx/xxx/xxx/main.py", line 37, in __init__
self.projectsPg.cb.Value(rows)
TypeError: 'unicode' object is not callable
Process finished with exit code 1
C:\Python27\python.exe C:/Users/xxx/xxx/xxx/main.py
[(u'Tests',),(u'Test1',),(u'Test2',),(u'Test3',)]
[(u'Tests',),(u'Test1',),(u'Test2',),(u'Test3',)]
回溯(最近一次呼叫最后一次):
文件“C:/Users/xxx/xxx/xxx/main.py”,第43行,在
frame=MyFrame(无,-1,“小编辑器”)
文件“C:/Users/xxx/xxx/xxx/main.py”,第37行,在初始化中__
self.projectsPg.cb.Value(行)
TypeError:“unicode”对象不可调用
进程已完成,退出代码为1
self.projectsPg.cb
是一个组合框对象,Value
是一个属性。如果您在没有赋值的情况下访问该属性,它将返回一个字符串(或unicode)。你不能这么说
如果要为combox设置值,请使用属性赋值(),或:
除此之外,返回的行是一个元组列表。你得先去拿。(在下面的示例中,为了简洁起见,我省略了返回行计数检查)
更新您的问题并添加整个回溯。这修复了“unicode”错误,因此您可以得到答案。。。但是self.projectsPg.cb.Value=行[0][0]和self.projectsPg.cb.SetValue(行[0][0])只在组合框的“空白”中显示标题,它不会重新填充组合框的选项。仅供参考,我找到了重新填充组合框选项的答案
self.projectsPg.cb.Value = rows[0][0]
# OR
self.projectsPg.cb.SetValue(rows[0][0])