Hello!
I have been playing around with python and mysql, and I have started thinking about creating a database to record my bird sights. For connection purposes, I have programmed this class

class LoginDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Login to Ornithobase 1.0b', size=(250,150))
		def comboLogin():
				import MySQLdb
				db= MySQLdb.connect(host='localhost', user='root' , passwd='acrsci00', db='Ornithobase')
				cursor = db.cursor()
				sql = '''select Username from Users'''
				cursor.execute(sql)
				result = cursor.fetchall()
				result2 = []
				x = 0
				for record in result:
					for field in record:
						users = str(field)
						result2.append(users)
						x += 1
				return result2
		users = comboLogin()
		# widgets
		userLbl = wx.StaticText(self, -1, 'Username:', size=(75, -1))
		self.userTxt = wx.ComboBox(self, -1, '', size=(200, -1), choices = users)
		passwordLbl = wx.StaticText(self, -1, 'Password:', size=(75, -1))
		self.passwordTxt = wx.TextCtrl(self, -1, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
		loginBtn = wx.Button(self, -1, 'Login')
		clearBtn = wx.Button(self, wx.ID_CLEAR, 'Clear')
		self.Bind(wx.EVT_BUTTON, self.OnLogin,loginBtn)
		self.Bind(wx.EVT_BUTTON, self.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(self.userTxt, 0, wx.ALL, 5)
		passwordSizer.Add(passwordLbl, 0, wx.LEFT|wx.RIGHT, 5)
		passwordSizer.Add(self.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
		self.loggedIn = False

		self.SetSizer(mainSizer)
		self.Fit()
		self.Layout()
	def OnLogin(self, event):
		Username = self.userTxt.GetValue()
		pwd = self.passwordTxt.GetValue()
		try:
			db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
			cursor = db.cursor()
			print 'Connected'
			dlg = wx.MessageDialog(None, 'You are logged in', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		except:
			self.userTxt.Clear()
			self.passwordTxt.Clear()
			Errordlg = wx.MessageDialog(None, 'Connection failed', 'Error', wx.OK | wx.ICON_ERROR)
			print 'Connection failed'
			Errordlg.ShowModal()
	def OnClear(self, event):
		self.userTxt.Clear()
		self.passwordTxt.Clear()

My immediate question is: is this database connection permanent? I mean, there will be many functionalities in the db, such as user management, data entry or queries on the entered data. Is there a way to make the login connection to last until the app is closed or should I connect every time I design a class (for example, a dialog that retrieves the logged in user's data to edit it. I hope I've explained myself well enough ...
Ciao!

Dani

Recommended Answers

All 12 Replies

The connection is available till you do a db.close() I believe, but I never used MySQL.

I was sure that it was as you say. However, when I run any other class, for example to edit the current user data with this command

sql = 'select substring_index(CURRENT_USER(),"@",1)'
cursor.execute(sql)

I get this error
NameError: global name 'cursor' is not defined

So I guess I'm doing something wrong.


Cheers!

I think I know where is the problem, but I don't know how to solve it. In order to connect to connect to the database, I create the cursor object on line 7 of the LoginDlg class. Then, if I want to run an sql statement in some other class I must call the cursor

cursor.execute('sql statement')

, but I get the error

NameError: global name 'cursor' is not defined

. How can I make the cursor object available to other classes, not only in the LoginDlg one?
Cheers!

Dani

You can declare the cursor as:

self.cursor = db.cursor()

and call it as:

login = LoginDlg()

login.cursor.execute('sql statement')

I have tried your suggestion, but I get this error

AttributeError: 'LoginDlg' object has no attribute 'cursor'

. I have seen that I define the cursor in a try/except statment inside a function within the LoginDlg class (line 58). I don't know if it matters ...

Dani

You must use

self.cursor = db.cursor()

Then the LoginDld will have the cursor attribute, replace on all cursor declarations on the logiDlg and try again.

Cheers and Happy coding

I don't know where's the problem, as this is what I am doing. I post the full code at the end, just in case it helps. On line 71 I connect to the database and on line 72 I declare the cursor as suggested

self.cursor = db.cursor()

Then, on line 185 it starts the EditUserDlg class, where I want to use the cursor again. So I call the cursor (line 188) and on line 190 the cursor is supposed to run the sql statement

login.cursor.execute(sql)

. However, I get the error,

AttributeError: 'LoginDlg' object has no attribute 'cursor'

whereas if I run

login.execute(sql)

(which I think it's wrong, but I had to try), I get this error

AttributeError: 'LoginDlg' object has no attribute 'execute'

. So now I don't know how to proceed ...

Cheers!

Dani

#! /usr/bin/env python
# OrnithobaseGUI.py

import wx, MySQLdb, wx.lib.intctrl

ID_HELP = 1
ID_ABOUT = 2
ID_LOG=3
ID_NEWUSER=4
ID_NEWUSER2=5
ID_DELUSER=6
ID_EDITUSER = 7


class LoginDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Login to Ornithobase 1.0b', size=(250,150))
		def comboLogin():
				import MySQLdb
				dbRoot= MySQLdb.connect(host='localhost', user='root' , passwd='acrsci00', db='Ornithobase')
				cursor = dbRoot.cursor()
				sql = '''select Username from Users'''
				cursor.execute(sql)
				result = cursor.fetchall()
				result2 = []
				x = 0
				for record in result:
					for field in record:
						users = str(field)
						result2.append(users)
						x += 1
				return result2
		users = comboLogin()
		# widgets
		userLbl = wx.StaticText(self, -1, 'Username:', size=(75, -1))
		self.userTxt = wx.ComboBox(self, -1, '', size=(200, -1), choices = users)
		passwordLbl = wx.StaticText(self, -1, 'Password:', size=(75, -1))
		self.passwordTxt = wx.TextCtrl(self, -1, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
		loginBtn = wx.Button(self, -1, 'Login')
		clearBtn = wx.Button(self, wx.ID_CLEAR, 'Clear')
		self.Bind(wx.EVT_BUTTON, self.OnLogin,loginBtn)
		self.Bind(wx.EVT_BUTTON, self.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(self.userTxt, 0, wx.ALL, 5)
		passwordSizer.Add(passwordLbl, 0, wx.LEFT|wx.RIGHT, 5)
		passwordSizer.Add(self.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
		self.loggedIn = False

		self.SetSizer(mainSizer)
		self.Fit()
		self.Layout()
	def OnLogin(self, event):
		Username = self.userTxt.GetValue()
		pwd = self.passwordTxt.GetValue()
		try:
			db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
			self.cursor = db.cursor()
			print 'Connected'
			dlg = wx.MessageDialog(None, 'You are logged in', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		except:
			self.userTxt.Clear()
			self.passwordTxt.Clear()
			Errordlg = wx.MessageDialog(None, 'Connection failed', 'Error', wx.OK | wx.ICON_ERROR)
			print 'Connection failed'
			Errordlg.ShowModal()
	def OnClear(self, event):
		self.userTxt.Clear()
		self.passwordTxt.Clear()

class NewUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Create new user', size=(400, 335))

#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		self.Bind(wx.EVT_BUTTON, self.OnOk,okBtn)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

	def OnOk(self, event):
		dbRoot= MySQLdb.connect(host='localhost', user='root' , passwd='acrsci00', db='Ornithobase')
		cursor = dbRoot.cursor()
		Name = self.NameTxt.GetValue()
		Surname = self.FamilyNameTxt.GetValue()
		eMail = self.eMailTxt.GetValue()
		Username = self.UsernameTxt.GetValue()
#Check if username already exists
		sql = '''use mysql'''
		cursor.execute(sql)
		sql = '''select count(*) from user where User=%s'''
		cursor.execute(sql, (Username))
		result = cursor.fetchall()
		result = list(result[0])
		result = str(result)
		result = int(result[1])
		if result == 1:
			wx.MessageBox('This username is already in use. Please choose another one', 'Exclamation', style= wx.OK | wx.ICON_EXCLAMATION)
			self.UsernameTxt.Clear()
		Passwd = self.PasswdTxt.GetValue()
#Check if password already exists
		RepeatPasswd = self.RepeatPasswdTxt.GetValue()
		if Passwd == RepeatPasswd:
			sql = 'create user ''%s''@''localhost'' identified by %s'
			cursor.execute(sql, (Username, Passwd))
			sql = 'grant all privileges on Ornithobase.* to ''%s''@''localhost'''
			cursor.execute(sql, (Username))
			sql = '''use Ornithobase'''
			cursor.execute(sql)
			newUser = '''INSERT INTO Users (Name, FamilyName,eMail,Username) VALUES (%s, %s, %s, %s)'''
			cursor.execute(newUser, (Name, Surname, eMail, Username))
			db.commit()
			dlg = wx.MessageDialog(None, 'New user created', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		else:
			wx.MessageBox('Passwords do not match', 'Error', style=wx.ICON_ERROR)
			self.PasswdTxt.Clear()
			self.RepeatPasswdTxt.Clear()

class EditUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Edit user', size=(400, 335))
		login = LoginDlg()
		sql = 'select substring_index(CURRENT_USER(),"@",1)'
		login.cursor.execute(sql)
		#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

class Ornithobase(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title, size=(650,75))

	#Define menus
		menubar = wx.MenuBar()
	#Define file menu
		fileMenu = wx.Menu()
		log = wx.MenuItem(fileMenu, ID_LOG, '&Login\tCtrl+L')
		fileMenu.AppendItem(log)
		self.Bind(wx.EVT_MENU, self.OnLog, id=ID_LOG)
		NewUser = wx.MenuItem(fileMenu, ID_NEWUSER, '&New User\tCtrl+N')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER)
		fileMenu.AppendItem(NewUser)
		quit = wx.MenuItem(fileMenu, 1, '&Quit\tCtrl+Q')
		fileMenu.AppendItem(quit)
		self.Bind(wx.EVT_MENU, self.OnQuit, id=1)

	#Define edit menu
		editMenu = wx.Menu()
		manageUsers = wx.Menu()
		manageUsers.Append(ID_NEWUSER2, '&New user')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER2)
		manageUsers.Append(ID_EDITUSER, '&Edit user')
		self.Bind(wx.EVT_MENU, self.EditUserDlg, id=ID_EDITUSER)
		manageUsers.Append(ID_DELUSER, '&Delete user')
		self.Bind(wx.EVT_MENU, self.DelUserDlg, id=ID_DELUSER)
		editMenu.AppendMenu(-1, 'Manage users', manageUsers)

	#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(fileMenu, '&File')
		menubar.Append(editMenu, '&Edit')
		menubar.Append(helpMenu, '&Help')
		self.SetMenuBar(menubar)
#Define main panel
		panel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
	#Define sizers
		#Vertical sizers
		hbox = wx.BoxSizer(wx.HORIZONTAL)
		#Horizontal sizers
		self.InitLifeList = wx.Button(panel,-1,'Initialize life list',size=(200,30))
		hbox.Add(self.InitLifeList, 0, wx.ALL , 5)
		self.NewData = wx.Button(panel,-1,'Enter new data',size=(200,30))
		hbox.Add(self.NewData, 0, wx.ALL , 5)
		self.Query = wx.Button(panel,-1,'Query your data',size=(200,30))
		hbox.Add(self.Query, 0, wx.ALL , 5)
		#Merge sizers
		vbox.Add(hbox, 0, wx.ALIGN_CENTER | wx.ALL, 5)

		panel.SetSizer(vbox)
		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('Ornithobase 1.0b')
		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)
	
	def OnLog(self, event):
		dlg = LoginDlg()
		dlg.ShowModal()

	def NewUserDlg(self, event):
		dlg = NewUserDlg()
		dlg.ShowModal()

	def DelUserDlg(self, event):
		dlg = wx.MessageDialog(None, 'Are you sure you want to delete your user?', 'Question', wx.OK | wx.CANCEL | wx.NO_DEFAULT | wx.ICON_QUESTION)
		dlg.ShowModal()

	def EditUserDlg(self, event):
		dlg = EditUserDlg()
		dlg.ShowModal()

app = wx.App()
Ornithobase(None, -1, 'Ornithobase 1.0b')
app.MainLoop()

Change also your line 21 mate.

Cheers and Happy coding.

Great! Now it works. I feel quite naive ... :$

Cheers!

Dani

Happy coding mate.

Case solved. You can close the thread.

Sorry about continuing with this threat, but I have modified the code (see the new code at the end) and I'm having the same previous problem. I have removed the combobox, because to use it I had to connect to the database as root to retrieve the users, so I decided to use a text box where the user has to give his name. However, now I get this error

AttributeError: 'LoginDlg' object has no attribute 'cursor'

which is the same that I got before. Now the cursor is defined in lines 55-56, and I defined it as

self.cursor = db.cursor()

From our previous posts, I assume that defining the cursor like this it will be available only within the OnLogin function, isn't it? Then, I cannot figure out how to make the cursor available for the rest of the code...
Can you help?

Cheers!

#! /usr/bin/env python
# OrnithobaseGUI.py

import wx, MySQLdb, wx.lib.intctrl

ID_HELP = 1
ID_ABOUT = 2
ID_LOG=3
ID_NEWUSER=4
ID_NEWUSER2=5
ID_DELUSER=6
ID_EDITUSER = 7


class LoginDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Login to Ornithobase 1.0b', size=(250,150))
		# widgets
		userLbl = wx.StaticText(self, -1, 'Username:', size=(75, -1))
		self.userTxt = wx.TextCtrl(self, -1, '', size=(200, -1))
		passwordLbl = wx.StaticText(self, -1, 'Password:', size=(75, -1))
		self.passwordTxt = wx.TextCtrl(self, -1, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
		loginBtn = wx.Button(self, -1, 'Login')
		clearBtn = wx.Button(self, wx.ID_CLEAR, 'Clear')
		self.Bind(wx.EVT_BUTTON, self.OnLogin,loginBtn)
		self.Bind(wx.EVT_BUTTON, self.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(self.userTxt, 0, wx.ALL, 5)
		passwordSizer.Add(passwordLbl, 0, wx.LEFT|wx.RIGHT, 5)
		passwordSizer.Add(self.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
		self.loggedIn = False

		self.SetSizer(mainSizer)
		self.Fit()
		self.Layout()
	def OnLogin(self, event):
		Username = self.userTxt.GetValue()
		pwd = self.passwordTxt.GetValue()
		try:
			db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
			self.cursor = db.cursor()
			print 'Connected'
			dlg = wx.MessageDialog(None, 'You are logged in', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		except:
			self.userTxt.Clear()
			self.passwordTxt.Clear()
			Errordlg = wx.MessageDialog(None, 'Connection failed', 'Error', wx.OK | wx.ICON_ERROR)
			print 'Connection failed'
			Errordlg.ShowModal()

	def OnClear(self, event):
		self.userTxt.Clear()
		self.passwordTxt.Clear()

class NewUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Create new user', size=(400, 335))

#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		self.Bind(wx.EVT_BUTTON, self.OnOk,okBtn)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

	def OnOk(self, event):
		db= MySQLdb.connect(host='localhost', user='root' , passwd='acrsci00', db='Ornithobase')
		cursor = db.cursor()
		Name = self.NameTxt.GetValue()
		Surname = self.FamilyNameTxt.GetValue()
		eMail = self.eMailTxt.GetValue()
		Username = self.UsernameTxt.GetValue()
#Check if username already exists
		sql = '''use mysql'''
		cursor.execute(sql)
		sql = '''select count(*) from user where User=%s'''
		cursor.execute(sql, (Username))
		result = cursor.fetchall()
		result = list(result[0])
		result = str(result)
		result = int(result[1])
		if result == 1:
			wx.MessageBox('This username is already in use. Please choose another one', 'Exclamation', style= wx.OK | wx.ICON_EXCLAMATION)
			self.UsernameTxt.Clear()
		Passwd = self.PasswdTxt.GetValue()
#Check if password already exists
		RepeatPasswd = self.RepeatPasswdTxt.GetValue()
		if Passwd == RepeatPasswd:
			sql = 'create user ''%s''@''localhost'' identified by %s'
			cursor.execute(sql, (Username, Passwd))
			sql = 'grant all privileges on Ornithobase.* to ''%s''@''localhost'''
			cursor.execute(sql, (Username))
			sql = '''use Ornithobase'''
			cursor.execute(sql)
			newUser = '''INSERT INTO Users (Name, FamilyName,eMail,Username) VALUES (%s, %s, %s, %s)'''
			cursor.execute(newUser, (Name, Surname, eMail, Username))
			db.commit()
			dlg = wx.MessageDialog(None, 'New user created', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		else:
			wx.MessageBox('Passwords do not match', 'Error', style=wx.ICON_ERROR)
			self.PasswdTxt.Clear()
			self.RepeatPasswdTxt.Clear()

class EditUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Edit user', size=(400, 335))
		def GetCurrentUser():
			login = LoginDlg()
			sql = 'select substring_index(CURRENT_USER(),"@",1)'
			login.cursor.execute(sql)
			result = login.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					CurrentUser = str(field)
					return CurrentUser
		CurrentUser = GetCurrentUser()
		
		#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.StaticText(panel, -1, CurrentUser, size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

class Ornithobase(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title, size=(650,75))

	#Define menus
		menubar = wx.MenuBar()
	#Define file menu
		fileMenu = wx.Menu()
		log = wx.MenuItem(fileMenu, ID_LOG, '&Login\tCtrl+L')
		fileMenu.AppendItem(log)
		self.Bind(wx.EVT_MENU, self.OnLog, id=ID_LOG)
		NewUser = wx.MenuItem(fileMenu, ID_NEWUSER, '&New User\tCtrl+N')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER)
		fileMenu.AppendItem(NewUser)
		quit = wx.MenuItem(fileMenu, 1, '&Quit\tCtrl+Q')
		fileMenu.AppendItem(quit)
		self.Bind(wx.EVT_MENU, self.OnQuit, id=1)

	#Define edit menu
		editMenu = wx.Menu()
		manageUsers = wx.Menu()
		manageUsers.Append(ID_NEWUSER2, '&New user')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER2)
		manageUsers.Append(ID_EDITUSER, '&Edit user')
		self.Bind(wx.EVT_MENU, self.EditUserDlg, id=ID_EDITUSER)
		manageUsers.Append(ID_DELUSER, '&Delete user')
		self.Bind(wx.EVT_MENU, self.DelUserDlg, id=ID_DELUSER)
		editMenu.AppendMenu(-1, 'Manage users', manageUsers)

	#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(fileMenu, '&File')
		menubar.Append(editMenu, '&Edit')
		menubar.Append(helpMenu, '&Help')
		self.SetMenuBar(menubar)
#Define main panel
		panel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
	#Define sizers
		#Vertical sizers
		hbox = wx.BoxSizer(wx.HORIZONTAL)
		#Horizontal sizers
		self.InitLifeList = wx.Button(panel,-1,'Initialize life list',size=(200,30))
		hbox.Add(self.InitLifeList, 0, wx.ALL , 5)
		self.NewData = wx.Button(panel,-1,'Enter new data',size=(200,30))
		hbox.Add(self.NewData, 0, wx.ALL , 5)
		self.Query = wx.Button(panel,-1,'Query your data',size=(200,30))
		hbox.Add(self.Query, 0, wx.ALL , 5)
		#Merge sizers
		vbox.Add(hbox, 0, wx.ALIGN_CENTER | wx.ALL, 5)

		panel.SetSizer(vbox)
		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('Ornithobase 1.0b')
		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)
	
	def OnLog(self, event):
		dlg = LoginDlg()
		dlg.ShowModal()

	def NewUserDlg(self, event):
		dlg = NewUserDlg()
		dlg.ShowModal()

	def DelUserDlg(self, event):
		dlg = wx.MessageDialog(None, 'Are you sure you want to delete your user?', 'Question', wx.OK | wx.CANCEL | wx.NO_DEFAULT | wx.ICON_QUESTION)
		dlg.ShowModal()

	def EditUserDlg(self, event):
		dlg = EditUserDlg()
		dlg.ShowModal()

app = wx.App()
Ornithobase(None, -1, 'Ornithobase 1.0b')
app.MainLoop()

I'm sorry to continue on this, but there's another issue. I have modified the code (see at the end) converting the combobox in the LoginDlg class to a TextCtrl box. I've done this because to use the combobox I need to connect to the database using the root user to retrieve the usernames. Now I define the cursor as

db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
			self.cursor = db.cursor()

on lines 55-56. From our previous conversations I guess that this makes the self.cursor available as attribute within the LogIn function inside the LoginDlg class. However, when I try to use the self.cursor in another class (for example, in the EditUser class, line 176) calling it as

login = LoginDlg()
sql = 'select substring_index(CURRENT_USER(),"@",1)'
login.cursor.execute(sql)

I get this error

AttributeError: 'LoginDlg' object has no attribute 'cursor'

I know it may be a naive question, as the previous issue. Can you help?
Cheers,

Dani

PS. Whenever I come to Den Haag I must pay you some beers!

#! /usr/bin/env python
# OrnithobaseGUI.py

import wx, MySQLdb, wx.lib.intctrl

ID_HELP = 1
ID_ABOUT = 2
ID_LOG=3
ID_NEWUSER=4
ID_NEWUSER2=5
ID_DELUSER=6
ID_EDITUSER = 7


class LoginDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Login to Ornithobase 1.0b', size=(250,150))
		# widgets
		userLbl = wx.StaticText(self, -1, 'Username:', size=(75, -1))
		self.userTxt = wx.TextCtrl(self, -1, '', size=(200, -1))
		passwordLbl = wx.StaticText(self, -1, 'Password:', size=(75, -1))
		self.passwordTxt = wx.TextCtrl(self, -1, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
		loginBtn = wx.Button(self, -1, 'Login')
		clearBtn = wx.Button(self, wx.ID_CLEAR, 'Clear')
		self.Bind(wx.EVT_BUTTON, self.OnLogin,loginBtn)
		self.Bind(wx.EVT_BUTTON, self.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(self.userTxt, 0, wx.ALL, 5)
		passwordSizer.Add(passwordLbl, 0, wx.LEFT|wx.RIGHT, 5)
		passwordSizer.Add(self.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
		self.loggedIn = False

		self.SetSizer(mainSizer)
		self.Fit()
		self.Layout()
	def OnLogin(self, event):
		Username = self.userTxt.GetValue()
		pwd = self.passwordTxt.GetValue()
		try:
			db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
			self.cursor = db.cursor()
			print 'Connected'
			dlg = wx.MessageDialog(None, 'You are logged in', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		except:
			self.userTxt.Clear()
			self.passwordTxt.Clear()
			Errordlg = wx.MessageDialog(None, 'Connection failed', 'Error', wx.OK | wx.ICON_ERROR)
			print 'Connection failed'
			Errordlg.ShowModal()

	def OnClear(self, event):
		self.userTxt.Clear()
		self.passwordTxt.Clear()

class NewUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Create new user', size=(400, 335))

#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		self.Bind(wx.EVT_BUTTON, self.OnOk,okBtn)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

	def OnOk(self, event):
		db= MySQLdb.connect(host='localhost', user='root' , passwd='acrsci00', db='Ornithobase')
		cursor = db.cursor()
		Name = self.NameTxt.GetValue()
		Surname = self.FamilyNameTxt.GetValue()
		eMail = self.eMailTxt.GetValue()
		Username = self.UsernameTxt.GetValue()
#Check if username already exists
		sql = '''use mysql'''
		cursor.execute(sql)
		sql = '''select count(*) from user where User=%s'''
		cursor.execute(sql, (Username))
		result = cursor.fetchall()
		result = list(result[0])
		result = str(result)
		result = int(result[1])
		if result == 1:
			wx.MessageBox('This username is already in use. Please choose another one', 'Exclamation', style= wx.OK | wx.ICON_EXCLAMATION)
			self.UsernameTxt.Clear()
		Passwd = self.PasswdTxt.GetValue()
#Check if password already exists
		RepeatPasswd = self.RepeatPasswdTxt.GetValue()
		if Passwd == RepeatPasswd:
			sql = 'create user ''%s''@''localhost'' identified by %s'
			cursor.execute(sql, (Username, Passwd))
			sql = 'grant all privileges on Ornithobase.* to ''%s''@''localhost'''
			cursor.execute(sql, (Username))
			sql = '''use Ornithobase'''
			cursor.execute(sql)
			newUser = '''INSERT INTO Users (Name, FamilyName,eMail,Username) VALUES (%s, %s, %s, %s)'''
			cursor.execute(newUser, (Name, Surname, eMail, Username))
			db.commit()
			dlg = wx.MessageDialog(None, 'New user created', 'Info', wx.OK)
			dlg.ShowModal()
			self.Destroy()
		else:
			wx.MessageBox('Passwords do not match', 'Error', style=wx.ICON_ERROR)
			self.PasswdTxt.Clear()
			self.RepeatPasswdTxt.Clear()

class EditUserDlg(wx.Dialog):
	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Edit user', size=(400, 335))
		def GetCurrentUser():
			login = LoginDlg()
			sql = 'select substring_index(CURRENT_USER(),"@",1)'
			login.cursor.execute(sql)
			result = login.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					CurrentUser = str(field)
					return CurrentUser
		CurrentUser = GetCurrentUser()
		
		#Define main panel
		panel = wx.Panel(self, -1)

	#Define sizers
		#Vertical sizers
		vbox = wx.BoxSizer(wx.VERTICAL)
		#Horizontal sizers
		NameSizer = wx.BoxSizer(wx.HORIZONTAL)
		FamilyNameSizer = wx.BoxSizer(wx.HORIZONTAL)
		eMailSizer = wx.BoxSizer(wx.HORIZONTAL)
		UsernameSizer = wx.BoxSizer(wx.HORIZONTAL)
		PasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		RepeatPasswdSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		Name = wx.StaticText(panel, -1, 'Name:', size=(100, -1))
		self.NameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		NameSizer.Add(Name, 0, wx.ALL, 5)
		NameSizer.Add(self.NameTxt, 0, wx.ALL, 5)
		vbox.Add(NameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		FamilyName = wx.StaticText(panel, -1, 'Family Name:', size=(100, -1))
		self.FamilyNameTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		FamilyNameSizer.Add(FamilyName, 0, wx.ALL, 5)
		FamilyNameSizer.Add(self.FamilyNameTxt, 0, wx.ALL, 5)
		vbox.Add(FamilyNameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		eMail = wx.StaticText(panel, -1, 'e-Mail:', size=(100, -1))
		self.eMailTxt = wx.TextCtrl(panel, -1, '', size=(250, -1))
		eMailSizer.Add(eMail, 0, wx.ALL, 5)
		eMailSizer.Add(self.eMailTxt, 0, wx.ALL, 5)
		vbox.Add(eMailSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Username = wx.StaticText(panel, -1, 'Username:', size=(100, -1))
		self.UsernameTxt = wx.StaticText(panel, -1, CurrentUser, size=(250, -1))
		UsernameSizer.Add(Username, 0, wx.ALL, 5)
		UsernameSizer.Add(self.UsernameTxt, 0, wx.ALL, 5)
		vbox.Add(UsernameSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		Passwd = wx.StaticText(panel, -1, 'Password:', size=(100, -1))
		self.PasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		PasswdSizer.Add(Passwd, 0, wx.ALL, 5)
		PasswdSizer.Add(self.PasswdTxt, 0, wx.ALL, 5)
		vbox.Add(PasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		RepeatPasswd = wx.StaticText(panel, -1, 'Repeat Password:', size=(100, -1))
		self.RepeatPasswdTxt = wx.TextCtrl(panel, -1, '', size=(250, -1), style = wx.TE_PASSWORD)
		RepeatPasswdSizer.Add(RepeatPasswd, 0, wx.ALL, 5)
		RepeatPasswdSizer.Add(self.RepeatPasswdTxt, 0, wx.ALL, 5)
		vbox.Add(RepeatPasswdSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		okBtn = wx.Button(panel, -1, 'Ok')
		clearBtn = wx.Button(panel, -1, 'Clear')
		BtnSizer.Add(okBtn, 0, wx.ALL, 5)
		BtnSizer.Add(clearBtn, 0, wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

class Ornithobase(wx.Frame):
	def __init__(self, parent, id, title):
		wx.Frame.__init__(self, parent, id, title, size=(650,75))

	#Define menus
		menubar = wx.MenuBar()
	#Define file menu
		fileMenu = wx.Menu()
		log = wx.MenuItem(fileMenu, ID_LOG, '&Login\tCtrl+L')
		fileMenu.AppendItem(log)
		self.Bind(wx.EVT_MENU, self.OnLog, id=ID_LOG)
		NewUser = wx.MenuItem(fileMenu, ID_NEWUSER, '&New User\tCtrl+N')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER)
		fileMenu.AppendItem(NewUser)
		quit = wx.MenuItem(fileMenu, 1, '&Quit\tCtrl+Q')
		fileMenu.AppendItem(quit)
		self.Bind(wx.EVT_MENU, self.OnQuit, id=1)

	#Define edit menu
		editMenu = wx.Menu()
		manageUsers = wx.Menu()
		manageUsers.Append(ID_NEWUSER2, '&New user')
		self.Bind(wx.EVT_MENU, self.NewUserDlg, id=ID_NEWUSER2)
		manageUsers.Append(ID_EDITUSER, '&Edit user')
		self.Bind(wx.EVT_MENU, self.EditUserDlg, id=ID_EDITUSER)
		manageUsers.Append(ID_DELUSER, '&Delete user')
		self.Bind(wx.EVT_MENU, self.DelUserDlg, id=ID_DELUSER)
		editMenu.AppendMenu(-1, 'Manage users', manageUsers)

	#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(fileMenu, '&File')
		menubar.Append(editMenu, '&Edit')
		menubar.Append(helpMenu, '&Help')
		self.SetMenuBar(menubar)
#Define main panel
		panel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
	#Define sizers
		#Vertical sizers
		hbox = wx.BoxSizer(wx.HORIZONTAL)
		#Horizontal sizers
		self.InitLifeList = wx.Button(panel,-1,'Initialize life list',size=(200,30))
		hbox.Add(self.InitLifeList, 0, wx.ALL , 5)
		self.NewData = wx.Button(panel,-1,'Enter new data',size=(200,30))
		hbox.Add(self.NewData, 0, wx.ALL , 5)
		self.Query = wx.Button(panel,-1,'Query your data',size=(200,30))
		hbox.Add(self.Query, 0, wx.ALL , 5)
		#Merge sizers
		vbox.Add(hbox, 0, wx.ALIGN_CENTER | wx.ALL, 5)

		panel.SetSizer(vbox)
		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('Ornithobase 1.0b')
		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)
	
	def OnLog(self, event):
		dlg = LoginDlg()
		dlg.ShowModal()

	def NewUserDlg(self, event):
		dlg = NewUserDlg()
		dlg.ShowModal()

	def DelUserDlg(self, event):
		dlg = wx.MessageDialog(None, 'Are you sure you want to delete your user?', 'Question', wx.OK | wx.CANCEL | wx.NO_DEFAULT | wx.ICON_QUESTION)
		dlg.ShowModal()

	def EditUserDlg(self, event):
		dlg = EditUserDlg()
		dlg.ShowModal()

app = wx.App()
Ornithobase(None, -1, 'Ornithobase 1.0b')
app.MainLoop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.