得到错误:'示例'对象没有属性' grid_1'

时间:2017-06-10 15:45:19

标签: python python-2.7 sqlite

我正在尝试将wxpython gui与sqlite数据库链接。我在我的数据库中创建了一个表(RAMAN.db)。我的GUI基本上是一个网格,我想在其中添加按钮,因此GUI应该直接从数据库中获取输入。我想在数据库表中的每个按钮上打印名称。在表格中,我给出了行号和列号,它们对应于网格的行号和列号。我的数据库表是:https://pastebin.com/PXLfCrNW我不明白如何做到这一点我试过这个`

import wx
import sqlite3
import wx.grid
import os

cwd = os.path.abspath(os.curdir)

def connect(): #this is the sqlite3 connection
conn = sqlite3.connect('RAMAN.db')
return conn
conn.close()

def data_rows_count(): #to count the rows in the database
conn = connect()
cur = conn.cursor()
cur.execute("SELECT * FROM ELEMENT")
rows = cur.fetchall()
i = 0
for r in rows:
  i += 1
return i

class Example(wx.Frame):

  def __init__(self, parent, title):
  super(Example, self).__init__(parent, title = title, size = (800, 600))

self.InitUI()
self.Layout()
self.Centre()
self.Show()

def refresh_data(self):
  conn = connect()
cur = conn.cursor()
cur.execute("SELECT * FROM Phone")
rows = cur.fetchall()
for i in range(0, len(rows)):
  for j in range(0, 4):
  cell = rows[i]
self.grid_1.SetCellValue(i, j, str(cell[j]))


def InitUI(self):
  self.grid_1 = wx.grid.Grid(self, -1, size = (1, 1))
p = wx.Panel(self)
r = data_rows_count()
self.grid_1.CreateGrid(r, 1)# this is to create the grid with same rows as database
self.grid_1.SetColLabelValue(0, ("SYMBOL"))
self.grid_1.SetColSize(0, 12)
bs = wx.BoxSizer(wx.VERTICAL)
self.t1 = wx.TextCtrl(p, size = (120, 30), style = wx.TE_MULTILINE | wx.TE_CENTER)
bs.Add(self.t1, 1, wx.EXPAND)
bs.Add(grid_1, 1, wx.EXPAND)
self.refresh_data()

def clk_h(self, event):
  r = data_rows_count
for e in range(0, r):
  for f in range(0, 103):
  self.grid_1.SetCellValue(e, f, "")
conn = connect()
cursor = conn.cursor()
currsor.execute("SELECT SYMBOL FROM ELEMENT")
conn.commit()
rows = cursor.fetchall()
btn = wx.Button(p, -1, h, (10, 20))
btn.myname = h
btn.Bind(wx.EVT_BUTTON, self.OnClick, btn)
grid_1.Add(btn, 0, wx.EXPAND)
for i in range(len(rows)):
  for j in range(0, 4):
  cell = rows[i]
self.grid_1.SetCellValue(i, j, str(cell[j]))
conn.close()
event.Skip()

p.SetSizer(bs)

def OnClick(self, event):
  name = event.GetEventObject().myname
self.t1.AppendText(name)
self.t1.AppendText("\n")


app = wx.App()
Example(None, title = 'Grid demo')

app.MainLoop()

1 个答案:

答案 0 :(得分:0)

您使用grid_1而不是self.grid_1
这里:

bs.Add(grid_1, 1, wx.EXPAND)

在这里:

grid_1.Add(btn, 0, wx.EXPAND)