Hello guys, I'm trying to make a program that will :
at startup it will give you login Dialog which will connect to database. Then it will bring Main window displaying tables. Then I have Another dialog to do some editing of Tables. Here are my two Questions:
Is it possible to connect to MySQL without specifying database parameter. I ask this because I want users to connect and create their own databases and change which database to work with.
When I call the editing GUI, I hide the main window. But I cannot re-Show the hidden GUI
Here is a code :
import wx
import MySQLdb
#to-do add database + adding window for adding, editing & removing data! + disconnect from database option
class AddData(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size=(200, 150))
#Add widgets
prodtext = wx.StaticText(self, -1, "Product")
prod = wx.TextCtrl(self, -1)
amounttext = wx.StaticText(self, -1, "Amount")
amount = wx.SpinCtrl(self, -1)
costtext = wx.StaticText(self, -1, "Cost")
cost = wx.TextCtrl(self, -1)
comit = wx.Button(self, -1, "Commit")
self.cancel = wx.Button(self, -1, "Cancel")
#Attach event
self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.cancel.GetId() )
#Add to sizer
grids = wx.GridBagSizer(5,5)
grids.AddMany([(prodtext, (0, 0), (1,1)),
(prod, (0,1), (1, 3)),
(amounttext, (1,0), (1, 1)),
(amount, (1,1), (1,1)),
(costtext, (2,0), (1, 1)),
(cost, (2,1), (1, 3))
])
hboxer = wx.BoxSizer(wx.HORIZONTAL)
hboxer.Add(comit, 0, wx.LEFT | wx.RIGHT, 8)
hboxer.Add(self.cancel, 0, wx.LEFT | wx.RIGHT, 8)
#Add to main sizer
msizer = wx.BoxSizer(wx.VERTICAL)
msizer.Add(grids, 1, wx.EXPAND | wx.ALL, 8)
msizer.Add(hboxer, 0, wx.TOP | wx.BOTTOM, 8 )
self.SetSizer(msizer)
self.Layout()
#Events belonging to class
def OnCancel(self, event):
self.Show(False)
hide = True
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size = (500, 350))
#Make a panel
MainPanel = wx.Panel(self, -1)
#Add widgets to Panneled Frame
#----------------------------------------
#Select Database
#Refresh
self.refresh = wx.Button(MainPanel, -1, "Refresh")
#Edit
self.edit = wx.Button(MainPanel, -1, "Edit")
#Bind events
self.Bind(wx.EVT_BUTTON, self.OnEdit, id = self.edit.GetId())
#Exit
self.exit = wx.Button(MainPanel, -1, "Exit")
#Caption
self.dbtitle = wx.StaticText(MainPanel, -1, "Select Database")
#Database selector
self.dblist= ["Mtangoo", "Steve", '10'] # Database list
self.dbselect = wx.ComboBox(MainPanel, -1, choices=self.dblist)
#select table
self.tbtitle = wx.StaticText(MainPanel, -1, "Select Table")
self.tblist= [] # Table list
self.tbselect = wx.ComboBox(MainPanel, -1, choices=self.tblist)
#Column titles
self.col1 = wx.StaticText(MainPanel, -1, "Column 1", style=wx.ALIGN_CENTER)
self.col2= wx.StaticText(MainPanel, -1, "Column 2", style=wx.ALIGN_CENTER)
self.col3 = wx.StaticText(MainPanel, -1, "Column 3", style=wx.ALIGN_CENTER)
#Cells, arranged by rows i.e 1st row cel1, cel2, cel3
#Row 1
self.cel1 = wx.TextCtrl(MainPanel, -1)
self.cel2 = wx.TextCtrl(MainPanel, -1)
self.cel3 = wx.TextCtrl(MainPanel, -1)
#Row 2
self.cel4 = wx.TextCtrl(MainPanel, -1)
self.cel5 = wx.TextCtrl(MainPanel, -1)
self.cel6 = wx.TextCtrl(MainPanel, -1)
#Row 3
self.cel7 = wx.TextCtrl(MainPanel, -1)
self.cel8 = wx.TextCtrl(MainPanel, -1)
self.cel9 = wx.TextCtrl(MainPanel, -1)
#Row 4
self.cel10 = wx.TextCtrl(MainPanel, -1)
self.cel11 = wx.TextCtrl(MainPanel, -1)
self.cel12 = wx.TextCtrl(MainPanel, -1)
#-----------------------------------------
#Add to the sizers
vbox = wx.BoxSizer(wx.VERTICAL)
#Database & Table selection section
hboxselect = wx.BoxSizer()
hboxselect.Add(self.dbtitle, 0, wx.LEFT , 10)
hboxselect.Add(self.dbselect, 1, wx.EXPAND |wx.LEFT | wx.RIGHT, 10)
hboxselect.Add(self.tbtitle, 0,wx.LEFT , 10)
hboxselect.Add(self.tbselect, 1, wx.EXPAND |wx.LEFT | wx.RIGHT, 10)
vbox.Add(hboxselect, 0, wx.ALL, 15)
vbox.Add((0,15))
#Table titles, depends on the selected table i.e, name of table chosen is displayed!
self.apptitle = wx.StaticText(MainPanel, -1, "This is table name")
vbox.Add(self.apptitle, 0, wx.CENTER)
vbox.Add((0,15))
#Add to gridsizer (row, col, vgap, hgap)
gsizer = wx.GridSizer(4, 3, 4, 4)
gsizer.AddMany([(self.col1, 0, wx.EXPAND) ,
(self.col2, 0, wx.EXPAND) ,
(self.col3, 0, wx.EXPAND) ,
(self.cel1, 0, wx.EXPAND) ,
(self.cel2, 0, wx.EXPAND),
(self.cel3, 0, wx.EXPAND),
(self.cel4, 0, wx.EXPAND),
(self.cel5, 0, wx.EXPAND),
(self.cel6, 0, wx.EXPAND),
(self.cel7, 0, wx.EXPAND),
(self.cel8, 0, wx.EXPAND),
(self.cel9, 0, wx.EXPAND),
(self.cel10, 0, wx.EXPAND),
(self.cel11, 0, wx.EXPAND),
(self.cel12, 0, wx.EXPAND)
])
vbox.Add(gsizer, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 7)
hbox1 = wx.BoxSizer()
hbox1.Add(self.refresh, 0, wx.LEFT, 10)
hbox1.Add(self.edit, 0, wx.RIGHT | wx.LEFT, 10)
hbox1.Add(self.exit, 0, wx.RIGHT, 10)
vbox.Add(hbox1, 0, wx.CENTER |wx.TOP, 15)
#set up sizer
MainPanel.SetSizer(vbox)
MainPanel.Layout()
#events for Data edit
def OnEdit(self, event):
call = AddData(None, -1, "Please Login for Authentication")
call.Show(True)
self.Show(False)
class Login(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size = (200, 200))
#define widgets
self.nametext = wx.StaticText(self, -1, "User Name")
self.name = wx.TextCtrl(self, -1)
self.password = wx.TextCtrl(self, -1, style = wx.TE_PASSWORD)
self.passwordtext = wx.StaticText(self, -1, "Password")
#host
self.hosttext = wx.StaticText(self, -1, "Database Host")
host = ["localhost"]
self.host = wx.ComboBox(self,-1, choices = host)
#Ok Button
self.ok = wx.Button(self, -1, "Ok")
#Bind to event
self.ok.Bind(wx.EVT_BUTTON, self.OnConnect, id = self.ok.GetId())
#Bind to event
self.close= wx.Button(self, -1, "Exit")
self.close.Bind(wx.EVT_BUTTON, self.OnClose, id = self.close.GetId())
#Add to sizer
vsizer = wx.BoxSizer(wx.VERTICAL)
vsizer.Add(self.hosttext, 0, wx.TOP | wx.LEFT, 10)
vsizer.Add(self.host, 0, wx.ALL, 10)
grid = wx.FlexGridSizer(2, 2, 8, 8)
grid.AddMany([(self.nametext, 0, wx.EXPAND | wx.LEFT, 10),
(self.name, 0, wx.EXPAND),
(self.passwordtext, 0, wx.EXPAND | wx.LEFT, 10),
(self.password , 0,wx.EXPAND)
])
vsizer.Add(grid, 0, wx.EXPAND, wx.ALL, 10)
hbutton = wx.BoxSizer()
hbutton.Add(self.ok, 0, wx.LEFT | wx.TOP, 10)
hbutton.Add(self.close, 0, wx.LEFT | wx.TOP, 10)
vsizer.Add(hbutton, 0, wx.EXPAND)
self.SetSizer(vsizer)
self.Layout()
self.Center()
self.Show(True)
#Start Handlers
def OnConnect(self, event):
host = self.host.GetValue()
user = self.name.GetValue()
passw = self.password.GetValue()
db =MySQLdb.connect(host , user, passw, db = "sql_book" )# create database choice entry
call = MainWindow(None, -1, "Elijah Database Manager")
ret = call.Show()
self.Destroy()#close the small window left big one to display tables
def OnClose(self, event):
self.Close()
#Create our App
app = wx.App(False)
#Instantiate MainWindow class
Login(None, -1, "Please Login for Authentication")
#AddData(None, -1, "Please Add or Edit data")
#Looping our main App
app.MainLoop()