0
我試圖重新進入Python和我再次堅持了這個問題,我讓彼此訪問的對象以前吃過。在這個簡單的例子,我顯示與一個按鈕和一個文本框的面板。點擊文本框調用其查詢數據庫,並返回檢索到的數據光標的功能。我需要讓這個無論是LookupSQL功能或ShowClientData功能可以寫這個輸出,在一個循環,到文本框。當前任何其他函數都不知道TextBox(outputBox)。我如何使其他功能知道它是什麼?的wxPython:編寫SQL命令結果outputbox
import wx
import pypyodbc
conn = pypyodbc.connect(driver='{SQL Server}', server='.', database='TheDB', uid='sa', pwd='Pass')
class Audit(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
hbox = wx.BoxSizer()
sizer = wx.GridSizer(6,1,2,2)
btn1 = wx.Button(panel, label='Clients')
outputBox = wx.TextCtrl(panel, -1, style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
sizer.AddMany([btn1, btn2, btn3, btn4, btn5, btn6])
hbox.Add(sizer, 0, wx.ALL, 15)
hbox.Add(outputBox, 1, wx.EXPAND)
panel.SetSizer(hbox)
btn1.Bind(wx.EVT_BUTTON, self.ShowClientData)
self.SetSize((800, 600))
self.SetTitle('Audit View')
self.Centre()
self.Show(True)
def ShowClientData(self, event):
SQL = 'select * from V_UpdatedClient'
recursor = lookupSQL(SQL)
for row in recursor:
rChange = row[0]
rItemType = row[1]
rPK = row[2]
rItemCode = row[3]
rFieldName = row[4]
rOldValue = row[5]
rNewValue = row[6]
rUpdateDate = row[7]
rUserName = row[8]
print('%s %s %s %s %s %s %s %s %s' % (rChange, rItemType, rPK, rItemCode, rFieldName, rOldValue, rNewValue, rUpdateDate, rUserName))
def lookupSQL(SQLString):
cursor = conn.cursor()
cursor.execute(SQLString)
return cursor
cursor.close()
def main():
ex = wx.App()
Audit(None)
ex.MainLoop()
if __name__ == '__main__':
main()
ShowClientData還是當它的定義,並要求自不承認它。 NameError:名字「outputBox」沒有定義 我試着聲明outputBox全球,而工作,但我不知道這是很好的做法 – pickarooney
見我的編輯,你應該通過自助訪問'outputBox',所以作出這樣的改變它在哪裏使用。 –
仍然收到錯誤 self.outputBox.WriteText( '%s%S%S%S%S%S%S%S%S \ n' %(rChange,rItemType,RPK,rItemCode,rFieldName,rOldValue,rNewValue ,rUpdateDate,rUserName)) NameError:名字「自我」是沒有定義 – pickarooney