Hello!
I have designed an initial frame for a database app using this code
#! /usr/bin/env python
# OrnithobaseGUI.py
import wx, MySQLdb, wx.lib.intctrl
ID_HELP = 1
ID_ABOUT = 2
ID_LOG=3
class Ornithobase(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(750,225))
#Define menus
menubar = wx.MenuBar()
#Define file menu
file = wx.Menu()
file.Append(ID_LOG, '&Log in')
quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q')
file.AppendItem(quit)
self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
#Define help menu
helpMenu = wx.Menu()
helpMenu.Append(ID_HELP, '&Help')
helpMenu.Append(ID_ABOUT, '&About')
self.Bind(wx.EVT_MENU, self.OnAboutBox, id=ID_ABOUT)
#Append menus
menubar.Append(file, '&File')
menubar.Append(helpMenu, '&Help')
self.SetMenuBar(menubar)
#Define main panel
panel = wx.Panel(self, -1)
vbox1 = wx.BoxSizer(wx.VERTICAL)
#Define sizers
#Vertical sizers
hbox1 = wx.BoxSizer(wx.HORIZONTAL)
#Horizontal sizers
vbox1.Add(hbox1, 0, wx.ALIGN_LEFT | wx.ALL, 5)
panel.SetSizer(vbox1)
self.Centre()
self.Show(True)
def OnQuit(self, event):
self.Close()
def OnAboutBox(self, event):
info = wx.AboutDialogInfo()
info.SetIcon(wx.Icon('icons/exit.png', wx.BITMAP_TYPE_PNG))
info.SetName('Watermark Image Processing')
info.SetVersion('1.0b')
description = open('docs/info.txt').read()
info.SetDescription(description)
info.SetCopyright('(C) 2010 Acrocephalus Soft')
info.SetWebSite('http://www.acrocephalus.net')
license = open('docs/licence.txt').read()
info.SetLicence(license)
info.AddDeveloper('Daniel Valverde')
info.AddDocWriter('Daniel Valverde')
info.AddArtist('Daniel Valverde')
info.AddTranslator('Daniel Valverde')
wx.AboutBox(info)
def OnClose(self, event):
self.Close(True)
app = wx.App()
Ornithobase(None, -1, 'Ornithobase 1.0b')
app.MainLoop()
Then, I have designed a dialog to connect to a MySQL database
#! /usr/bin/env python
# login.py
import wx, MySQLdb
class LoginDlg(wx.Dialog):
def __init__(parent):
wx.Dialog.__init__(parent, None, -1, 'Login to Ornithobase 1.0b', size=(250,150))
# widgets
userLbl = wx.StaticText(parent, -1, 'Username:', size=(75, -1))
parent.userTxt = wx.TextCtrl(parent, -1, '', size=(200, -1))
passwordLbl = wx.StaticText(parent, -1, 'Password:', size=(75, -1))
parent.passwordTxt = wx.TextCtrl(parent, -1, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
loginBtn = wx.Button(parent, -1, 'Login')
clearBtn = wx.Button(parent, wx.ID_CLEAR, 'Clear')
parent.Bind(wx.EVT_BUTTON, parent.OnLogin,loginBtn)
parent.Bind(wx.EVT_BUTTON, parent.OnClear, clearBtn)
# sizer / layout
userSizer = wx.BoxSizer(wx.HORIZONTAL)
passwordSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer = wx.BoxSizer(wx.VERTICAL)
userSizer.Add(userLbl, 0, wx.ALL, 5)
userSizer.Add(parent.userTxt, 0, wx.ALL, 5)
passwordSizer.Add(passwordLbl, 0, wx.LEFT|wx.RIGHT, 5)
passwordSizer.Add(parent.passwordTxt, 0, wx.LEFT, 5)
btnSizer.Add(loginBtn, 0, wx.ALL, 5)
btnSizer.Add(clearBtn, 0, wx.ALL, 5)
mainSizer.Add(userSizer, 0, wx.ALL, 0)
mainSizer.Add(passwordSizer, 0, wx.ALL, 0)
mainSizer.Add(btnSizer, 0, wx.ALL, 5)
# Logged in variable
parent.loggedIn = False
parent.SetSizer(mainSizer)
parent.Fit()
parent.Layout()
def OnLogin(parent, event):
Username = parent.userTxt.GetValue()
pwd = parent.passwordTxt.GetValue()
try:
db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
cursor = db.cursor()
print 'Connected'
Login.Destroy()
except:
print 'Connection failed'
def OnClear(parent, event):
parent.userTxt.Clear()
parent.passwordTxt.Clear()
app = wx.App()
Login = LoginDlg()
result = Login.ShowModal()
What I would like to do next, is to set up a system that when the user clicks on "File > Log in" it prompts the dialog in class
login
. I've been playing around with some options I've found on the web, but I haven't been able to use any of them. How can I do it?
Cheers!
Dani