Robię mały projekcik na uczelnie i napotkałem na duży problem
Napisałem sobie 2 klasy i problem pojawił się kiedy chciałem z skorzystać z metod jednej klasy w drugiej
W normalnych językach obiektowych(C# lub java) po prostu stworzył bym metodę statyczną lub nowy obiekt danej klasy i po problemie.
Niestety nie tutaj, kombinuję od kilku godzin i niestety nie w dalszym ciągu nie mogę sobie z tym poradzić
import sys
import sqlite3
import temp
import wx
import wx.xrc
class SqlConnection():
def __init__(self, connection=None, isconnectedd=False):
self.connection = connection
self.isconnectedd = isconnectedd
def getConnection(self, baseName):
try:
self.connection = sqlite3.connect(baseName)
self.isconnectedd = True
except self.connection.DatabaseError:
print "Blad bazy"
self.isconnectedd = False
def closeConnection(self):
try:
self.connection.close()
print "zakonczylem polaczenie"
except self.connection.DatabaseError:
print "Blad bazy"
def getQuery(self, querry):
try:
cursor = self.connection.execute(querry)
return cursor
except self.connection.DatabaseError:
return None
def toDatabase(self, querry):
try:
cursor = self.connection.execute(querry)
except self.connection.DatabaseError:
print 'errror'
class MyFrame1 ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 500,484 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.toggleBtnPolacz = wx.ToggleButton( self, wx.ID_ANY, u"Polacz/Rozlacz", wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.toggleBtnPolacz, 0, wx.ALL, 5 )
radioBoxTabeleChoices = [ u"Ksiazki", u"Czytelnicy", u"Wypozyczenia" ]
self.radioBoxTabele = wx.RadioBox( self, wx.ID_ANY, u"Wybierz tabele", wx.DefaultPosition, wx.DefaultSize, radioBoxTabeleChoices, 3, wx.RA_SPECIFY_COLS )
self.radioBoxTabele.SetSelection( 2 )
self.radioBoxTabele.Enable( False )
bSizer1.Add( self.radioBoxTabele, 0, wx.ALL, 5 )
self.buttonDodaj = wx.Button( self, wx.ID_ANY, u"Dodaj", wx.DefaultPosition, wx.DefaultSize, 0 )
self.buttonDodaj.Enable( False )
bSizer1.Add( self.buttonDodaj, 0, wx.ALL, 5 )
self.buttonUsun = wx.Button( self, wx.ID_ANY, u"Usun", wx.DefaultPosition, wx.DefaultSize, 0 )
self.buttonUsun.Enable( False )
bSizer1.Add( self.buttonUsun, 0, wx.ALL, 5 )
self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, u"Dodaj wartosci/Usun rekord po ID", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
bSizer1.Add( self.m_staticText1, 0, wx.ALL, 5 )
self.zmiennaArea = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
bSizer1.Add( self.zmiennaArea, 0, wx.ALL, 5 )
self.m_staticText2 = wx.StaticText( self, wx.ID_ANY, u"Wynik", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText2.Wrap( -1 )
bSizer1.Add( self.m_staticText2, 0, wx.ALL, 5 )
self.wynik = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,300 ), 0 )
bSizer1.Add( self.wynik, 0, wx.ALL, 5 )
self.wlacznikint = True
self.SetSizer( bSizer1 )
self.Layout()
self.Show(True)
self.Centre( wx.BOTH )
# Connect Events
self.toggleBtnPolacz.Bind( wx.EVT_TOGGLEBUTTON, self.polaczRozlacz )
self.radioBoxTabele.Bind( wx.EVT_RADIOBOX, self.tabela )
self.buttonDodaj.Bind( wx.EVT_BUTTON, self.dodajRek )
self.buttonUsun.Bind( wx.EVT_BUTTON, self.usunRek )
#self.sq = SqlConnection(sqlite3, False)
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def polaczRozlacz( self, event ):
self.buttonDodaj.Enable(self.wlacznikint)
self.radioBoxTabele.Enable(self.wlacznikint)
self.buttonUsun.Enable(self.wlacznikint)
if self.wlacznikint == False:
self.wlacznikint= True
else:
self.wlacznikint = False
def tabela( self, event ):
event.Skip()
def dodajRek( self, event ):
event.Skip()
def usunRek( self, event ):
event.Skip()
if __name__ == '__main__':
sq = SqlConnection(sqlite3, False)
sq.getConnection('wypozycz.db')
app = wx.App()
MyFrame1(None)
print "obieg petli"
app.MainLoop()
if sq.isconnectedd:
print('polaczono')
else:
print('niepolaczono')
ttt = sq.getQuery('select * from ksiazka')
for row in ttt:
print row[0]," ",row[1]," ",row[2]
sq.toDatabase('insert ')