I have written this dialog to connect to a MySQL database. It asks for the Username and Password, and it should use this data to connect to the database.

#! /usr/bin/env python
# pwd.py
import wx, MySQLdb

class LoginDlg(wx.Dialog):

	def __init__(self):
		wx.Dialog.__init__(self, None, -1, 'Login', size=(250,150))

		# widgets
		userLbl = wx.StaticText(self, wx.ID_ANY, 'Username:', size=(75, -1))
		self.userTxt = wx.TextCtrl(self, wx.ID_ANY, '', size=(200, -1))
		passwordLbl = wx.StaticText(self, wx.ID_ANY, 'Password:', size=(75, -1))
		self.passwordTxt = wx.TextCtrl(self, wx.ID_ANY, '',style=wx.TE_PROCESS_ENTER|wx.TE_PASSWORD, size=(200, -1))
		loginBtn = wx.Button(self, wx.ID_YES, 'Login')
		cancelBtn = wx.Button(self, wx.ID_ANY, 'Cancel')

		# 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(cancelBtn, 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

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

app = wx.App()
Login = LoginDlg()
result = Login.ShowModal()

However, when I run the code I get this traceback

Traceback (most recent call last):
  File "pwd.py", line 45, in <module>
    Login = LoginDlg()
  File "pwd.py", line 42, in __init__
    db= MySQLdb.connect(host='localhost', user=Username , passwd=pwd, db='Ornithobase')
  File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'acrocephalus'@'localhost' (using password: NO)")

Can anyone help?


8 Years
Discussion Span
Last Post by griswolf

Thanks griswolf, but I cannot solve the problem. I can connect by doing
from MySQLdb import *

# replace servername, username and password with your account info
dbconnect = connect(host=servername, user=username, passwd=password)
dbcursor = dbconnect.cursor()

However, when I want to retrieve the value from the controls (for example, Username = self.userTxt.GetValue()) to use it to connect to the databse, it does not work. So I guess I am doing something wrong. How can I make it to work? I would like to retrieve the values from the control (Username = self.userTxt.GetValue() and pwd = self.passwordTxt.GetValue()) and use them to connect to the database.



By your error message it appears that your values are being correctly extracted from the controls,.

The problem relys elsewhere I believe.


after lines 40 and 41 you can add instrumentation to print the actual values of name and pwd. I'm thinking there may be white space of some kind. So, for instance print("::%s::"%name) to show you the added space or newline if any. If that is the problem, then line 41, for instance becomes pwd = self.passwordTxt.GetValue().strip()

For your information: You can make nice inline code such as you see above using your code goes here tags (lower case is ok for tags if you prefer). Look closely at the gray text in the message box before you start typing your message. The last line is an example of inline code.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.