468,556 Members | 2,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

wx.Dialog subclass instantiates my dbServer class

bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. #Boa:Dialog:DBConnectDialog
  2.  
  3. import wx
  4.  
  5. import wxdbtools as db
  6.  
  7. def create(parent):
  8.     return DBConnectDialog(parent)
  9.  
  10. [wxID_DBCONNECTDIALOG, wxID_DBCONNECTDIALOGCANCELBTN,
  11.  wxID_DBCONNECTDIALOGCONNECTBTN, wxID_DBCONNECTDIALOGDATABASETEXTCTRL,
  12.  wxID_DBCONNECTDIALOGPWDTEXTCTRL, wxID_DBCONNECTDIALOGSERVERNAMETEXTCTRL,
  13.  wxID_DBCONNECTDIALOGSTATICTEXT1, wxID_DBCONNECTDIALOGSTATICTEXT2,
  14.  wxID_DBCONNECTDIALOGSTATICTEXT3, wxID_DBCONNECTDIALOGSTATICTEXT4,
  15.  wxID_DBCONNECTDIALOGSTATICTEXT5, wxID_DBCONNECTDIALOGSTATUSTEXTCTRL,
  16.  wxID_DBCONNECTDIALOGUSERNAMETEXTCTRL,
  17. ] = [wx.NewId() for _init_ctrls in range(13)]
  18.  
  19. class DBConnectDialog(wx.Dialog):
  20.     def _init_ctrls(self, prnt):
  21.         # generated method, don't edit
  22.         wx.Dialog.__init__(self, id=wxID_DBCONNECTDIALOG,
  23.               name='DBConnectDialog', parent=prnt, pos=wx.Point(503, 380),
  24.               size=wx.Size(385, 344),
  25.               style=wx.STAY_ON_TOP | wx.DIALOG_MODAL | wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER | wx.DEFAULT_DIALOG_STYLE,
  26.               title='Database Connection Dialog')
  27.         self.SetClientSize(wx.Size(377, 317))
  28.         self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
  29.               'MS Shell Dlg 2'))
  30.         self.SetThemeEnabled(True)
  31.  
  32.         self.staticText1 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT1,
  33.               label='User Name', name='staticText1', parent=self,
  34.               pos=wx.Point(16, 8), size=wx.Size(63, 16), style=0)
  35.         self.staticText1.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL,
  36.               False, 'MS Shell Dlg 2'))
  37.  
  38.         self.staticText2 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT2,
  39.               label='Password', name='staticText2', parent=self,
  40.               pos=wx.Point(200, 8), size=wx.Size(55, 16), style=0)
  41.  
  42.         self.usernameTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGUSERNAMETEXTCTRL,
  43.               name='usernameTextCtrl', parent=self, pos=wx.Point(16, 24),
  44.               size=wx.Size(160, 24), style=0, value='barton')
  45.  
  46.         self.servernameTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGSERVERNAMETEXTCTRL,
  47.               name='servernameTextCtrl', parent=self, pos=wx.Point(16, 80),
  48.               size=wx.Size(160, 24), style=0, value='genesis')
  49.  
  50.         self.staticText3 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT3,
  51.               label='Server Name or IP Address', name='staticText3',
  52.               parent=self, pos=wx.Point(16, 64), size=wx.Size(156, 16),
  53.               style=0)
  54.  
  55.         self.staticText5 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT5,
  56.               label='Default Database', name='staticText5', parent=self,
  57.               pos=wx.Point(200, 64), size=wx.Size(97, 16), style=0)
  58.  
  59.         self.pwdTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGPWDTEXTCTRL,
  60.               name='pwdTextCtrl', parent=self, pos=wx.Point(200, 24),
  61.               size=wx.Size(160, 24), style=wx.TE_PASSWORD, value='1coolSQL')
  62.  
  63.         self.databaseTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGDATABASETEXTCTRL,
  64.               name='databaseTextCtrl', parent=self, pos=wx.Point(200, 80),
  65.               size=wx.Size(160, 24), style=0, value='Trails')
  66.  
  67.         self.staticText4 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT4,
  68.               label='Connection Status Log', name='staticText4', parent=self,
  69.               pos=wx.Point(16, 120), size=wx.Size(127, 16), style=0)
  70.  
  71.         self.ConnectBtn = wx.Button(id=wxID_DBCONNECTDIALOGCONNECTBTN,
  72.               label='Connect', name='ConnectBtn', parent=self, pos=wx.Point(104,
  73.               280), size=wx.Size(75, 26), style=0)
  74.         self.ConnectBtn.Bind(wx.EVT_BUTTON, self.OnConnectButton,
  75.               id=wxID_DBCONNECTDIALOGCONNECTBTN)
  76.  
  77.         self.cancelBtn = wx.Button(id=wxID_DBCONNECTDIALOGCANCELBTN,
  78.               label='Cancel', name='cancelBtn', parent=self, pos=wx.Point(200,
  79.               280), size=wx.Size(75, 26), style=0)
  80.         self.cancelBtn.Bind(wx.EVT_BUTTON, self.OnCancelButton,
  81.               id=wxID_DBCONNECTDIALOGCANCELBTN)
  82.  
  83.         self.statusTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGSTATUSTEXTCTRL,
  84.               name='statusTextCtrl', parent=self, pos=wx.Point(16, 136),
  85.               size=wx.Size(344, 136), style=wx.TE_READONLY | wx.TE_MULTILINE,
  86.               value='')
  87.         self.statusTextCtrl.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL,
  88.               False, 'MS Shell Dlg 2'))
  89.  
  90.     def __init__(self, parent):
  91.         self._init_ctrls(parent)
  92.  
  93.         self.dbServer = db.DBServer(self)
  94.         self.dbConnect = self.DBConnect()   # Don't call this guy directly (don't keep?)
  95.         if not self.dbConnect:
  96.             self.Show()
  97.  
  98.     def DBConnect(self):
  99.         user = self.usernameTextCtrl.GetValue()
  100.         password = self.pwdTextCtrl.GetValue()
  101.         host = self.servernameTextCtrl.GetValue()
  102.         dbConnect = self.dbServer.Login(host, user, password)
  103.         database = self.databaseTextCtrl.GetValue()
  104.         if database:
  105.             self.dbServer.Execute("CREATE DATABASE IF NOT EXISTS %s" % database)
  106.         self.dbServer.Execute("USE %s" % database)
  107.         return dbConnect
  108.  
  109.     def GetDBServer(self):
  110.         return self.dbServer
  111.  
  112.     def GetDefaultDatabase(self):
  113.         return self.databaseTextCtrl.GetValue()
  114.  
  115.     def write(self, message):
  116.         self.statusTextCtrl.AppendText("%s\n" %message)
  117.  
  118.     def OnConnectButton(self, event):
  119.         if self.dbConnect:
  120.             self.dbConnect.close()
  121.         self.dbConnect = self.DBConnect()
  122.         if self.dbConnect:
  123.             self.write("Ready")
  124.  
  125.  
  126.     def OnCancelButton(self, event):
  127.         self.Hide()
Dec 3 '06 #1
0 5315

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Frank Millman | last post: by
2 posts views Thread by William Gill | last post: by
11 posts views Thread by Zytan | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.