How to dynamically create and populate widgets in wxglade created code.
Dennis DeBerry via wxGlade-general <wxglade-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org>
| Newsgroups | gmane.comp.python.wxglade |
|---|---|
| Message-ID | <[email protected]> |
I have created and interface to a sqlite table with wxglade. I created that interface using wxgalde, then I added the python code t access the on hard drive sqlite database with python.. PLEASE do not get hung up on the sqlite access parts of this program ( I know somone is going to focus in on that and miss the actual point of the question).
Anyway. I have a dialog box where the user has to select the table in the database what he wants to add records to. Those table names are queried and returned from the database as a list of tuple, where each tuple element is a string of the table name.
What I want to do is to create a series of radiobuttons whose lables are the table names. Then after accessing that table create a wx.grid where the row names are replaced by the field names and each cell next to those row names are where the user enters the data that will be written to that table
import wximport wx.gridimport os,sysimport sqlite3 as sq#import wx.SplashScreenfrom sqlite3 import Error
filetypewildcard = "Sqlite Database (*.db)|*.db|""sqz files(*.sqz)|*.sqz|""sqlfiles(*.sql)|*.sql"currentDirectory = os.getcwd()
def create_connection(db_file): """ create a database connection to a SQLite database """# First string in a sqlite database file. This identies the file# as a sqlite database file "SQLite format 3" choice = 0 tablenames = [] databasefileheader = "SQLite format 3" foreignkeyexist = "PRAGMA foreign_key_list" foreignkeyon = "PRAGMA foreign_key = 'ON'" try: conn = sq.connect(db_file) c = conn.cursor() conn.text_factory = bytes print(sq.version)# get the tables in the database# `SELECT name FROM sqlite_master WHERE type = "table"` row = c.execute("SELECT name FROM sqlite_master WHERE type = 'table'") #print(type(row)) row not neccessary in the windowed version of this program # print(type(row)) for line in row: #How to get the table name into the radiobox and dynamically adjust #the number of items in the radio box. #TableChoice.radio_box_1 choice +=1 print(str(choice) + ")" + " " + line[0]) tablenames += [line[0]] print(tablenames) tablechoice = raw_input("Enter the number of the table that you want to edit ") tableindex = int(tablechoice) chosentable = tablenames[tableindex-1] print chosentable
# Determine if table has foreign key or BLOB column and excluse them from listing# of columns to be displayed tablestmt = "PRAGMA table_info(" + chosentable + ")" tablecolumns = c.execute(tablestmt ) for index in tablecolumns: if not index[5]: if "BLOB" not in index[2]: print index except Error as e: print(e) finally: conn.close()
# begin wxGlade: dependencies# end wxGlade
# begin wxGlade: extracode# end wxGlade
class FileChoice(wx.Dialog): def __init__(self, *args, **kwds): # Content of this block not found. Did you rename this class? pass
def __set_properties(self): # Content of this block not found. Did you rename this class? pass
def __do_layout(self): # Content of this block not found. Did you rename this class? pass
# end of class FileChoice
class TableChoice(wx.Dialog): def __init__(self, *args, **kwds): # begin wxGlade: TableChoice.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX | wx.RESIZE_BORDER wx.Dialog.__init__(self, *args, **kwds) self.SetSize((300, 100)) def addradiobutton(tableslist): pass self.radio_btn_1 = wx.RadioButton(self, wx.ID_ANY, "")
self.__set_properties() self.__do_layout() # end wxGlade
def __set_properties(self): # begin wxGlade: TableChoice.__set_properties self.SetTitle("TableChoice") self.SetSize((300, 100)) # end wxGlade
def __do_layout(self): # begin wxGlade: TableChoice.__do_layout grid_sizer_1 = wx.BoxSizer(wx.VERTICAL) grid_sizer_1.Add(self.radio_btn_1, 0, 0, 0) self.SetSizer(grid_sizer_1) self.Layout() self.Centre() # end wxGlade
# end of class TableChoiceclass MyDialog(wx.Dialog): def __init__(self, *args, **kwds): # begin wxGlade: MyDialog.__init__ kwds["style"] = kwds.get("style", 0) | wx.CAPTION | wx.CLOSE_BOX wx.Dialog.__init__(self, *args, **kwds)
self.__set_properties() self.__do_layout() # end wxGlade
def __set_properties(self): # begin wxGlade: MyDialog.__set_properties self.SetTitle("Database File Error") # end wxGlade
def __do_layout(self): # begin wxGlade: MyDialog.__do_layout self.Layout() self.Centre() # end wxGlade
# end of class MyDialogclass ActorList(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: ActorList.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.SetSize((800, 600)) # Menu Bar self.ActiorListMenu = wx.MenuBar() wxglade_tmp_menu = wx.Menu() item = wxglade_tmp_menu.Append(wx.ID_ANY, "Open Database File", "") self.Bind(wx.EVT_MENU, self.OpenFile, id=item.GetId()) wxglade_tmp_menu.Append(wx.ID_ANY, "Save Database", "") wxglade_tmp_menu.Append(wx.ID_ANY, "Close Database", "") item = wxglade_tmp_menu.Append(wx.ID_ANY, "Exit", "") self.Bind(wx.EVT_MENU, self.Quit, id=item.GetId()) self.ActiorListMenu.Append(wxglade_tmp_menu, "File") wxglade_tmp_menu = wx.Menu() item = wxglade_tmp_menu.Append(wx.ID_ANY, "Help", "") self.Bind(wx.EVT_MENU, self.DisplayHelpFrame, id=item.GetId()) item = wxglade_tmp_menu.Append(wx.ID_ANY, "About", "") self.Bind(wx.EVT_MENU, self.DisplauSplashScreen, id=item.GetId()) self.ActiorListMenu.Append(wxglade_tmp_menu, "Help") self.SetMenuBar(self.ActiorListMenu) # Menu Bar end self.frame_statusbar = self.CreateStatusBar(2) self.panel_1 = wx.Panel(self, wx.ID_ANY) self.panel_2 = wx.ScrolledWindow(self.panel_1, wx.ID_ANY, style=wx.BORDER_RAISED) self.displaygrid = wx.grid.Grid(self.panel_2, wx.ID_ANY, size=(1, 1)) self.button_3 = wx.Button(self.panel_1, wx.ID_ANY, "CLEAR") self.button_1 = wx.Button(self.panel_1, wx.ID_ANY, "ADD") self.button_2 = wx.Button(self.panel_1, wx.ID_ANY, "OPEN")
self.__set_properties() self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.ClearForm, self.button_3) self.Bind(wx.EVT_BUTTON, self.AddData, self.button_1) self.Bind(wx.EVT_BUTTON, self.OpenFile, self.button_2) # end wxGlade
def __set_properties(self): # begin wxGlade: ActorList.__set_properties self.SetTitle("frame") self.frame_statusbar.SetStatusWidths([-1, 0]) # statusbar fields frame_statusbar_fields = ["frame_statusbar", ""] for i in range(len(frame_statusbar_fields)): self.frame_statusbar.SetStatusText(frame_statusbar_fields[i], i) self.displaygrid.CreateGrid(2, 1) self.displaygrid.SetFocus() self.panel_2.SetScrollRate(10, 10) self.button_1.SetToolTipString("Add data to table") self.button_2.SetToolTipString("Open a database table") # end wxGlade
def __do_layout(self): # begin wxGlade: ActorList.__do_layout sizer_1 = wx.BoxSizer(wx.VERTICAL) sizer_2 = wx.BoxSizer(wx.VERTICAL) sizer_3 = wx.BoxSizer(wx.HORIZONTAL) sizer_4 = wx.BoxSizer(wx.VERTICAL) sizer_4.Add(self.displaygrid, 1, wx.ALIGN_CENTER | wx.EXPAND, 0) self.panel_2.SetSizer(sizer_4) sizer_2.Add(self.panel_2, 1, wx.EXPAND, 0) sizer_3.Add(self.button_3, 1, wx.EXPAND, 0) sizer_3.Add(self.button_1, 1, wx.EXPAND, 0) sizer_3.Add(self.button_2, 1, wx.EXPAND, 0) sizer_2.Add(sizer_3, 1, wx.ALIGN_BOTTOM | wx.ALL | wx.EXPAND | wx.FIXED_MINSIZE | wx.SHAPED, 10) self.panel_1.SetSizer(sizer_2) sizer_1.Add(self.panel_1, 3, wx.ALL | wx.EXPAND, 0) self.SetSizer(sizer_1) self.Layout() # end wxGlade
def OpenFile(self, event): # wxGlade: ActorList.<event_handler> dlg = wx.FileDialog(self, message="Choose a file", defaultDir=currentDirectory, defaultFile="", wildcard=filetypewildcard, style=wx.OPEN | wx.FD_CHANGE_DIR) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath() self.frame_statusbar.SetStatusText(filename) create_connection(filename) # for path in db_file: # print path dlg.Destroy() else: # print("Event handler 'OpenFile' not implemented!") event.Skip()
def Quit(self, event): # wxGlade: ActorList.<event_handler> print("Event handler 'Quit' not implemented!") event.Skip()
def ClearForm(self, event): # wxGlade: ActorList.<event_handler> print("Event handler 'ClearForm' not implemented!") event.Skip()
def AddData(self, event): # wxGlade: ActorList.<event_handler> print("Event handler 'AddData' not implemented!") event.Skip()
def DisplayHelpFrame(self, event): # wxGlade: ActorList.<event_handler> print("Event handler 'DisplayHelpFrame' not implemented!") event.Skip() def DisplauSplashScreen(self, event): # wxGlade: ActorList.<event_handler> print("Event handler 'DisplauSplashScreen' not implemented!") event.Skip()# end of class ActorList
class OpenDialog(wx.Dialog): def __init__(self, *args, **kwds): # begin wxGlade: OpenDialog.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX | wx.RESIZE_BORDER wx.Dialog.__init__(self, *args, **kwds)
self.__set_properties() self.__do_layout() # end wxGlade
def __set_properties(self): # begin wxGlade: OpenDialog.__set_properties self.SetTitle("Open") # end wxGlade
def __do_layout(self): # begin wxGlade: OpenDialog.__do_layout self.Layout() # end wxGlade
# end of class OpenDialog
class SaveDialog(wx.Dialog): def __init__(self, *args, **kwds): # begin wxGlade: SaveDialog.__init__ kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX | wx.RESIZE_BORDER wx.Dialog.__init__(self, *args, **kwds)
self.__set_properties() self.__do_layout() # end wxGlade
def __set_properties(self): # begin wxGlade: SaveDialog.__set_properties self.SetTitle("Save") # end wxGlade
def __do_layout(self): # begin wxGlade: SaveDialog.__do_layout self.Layout() # end wxGlade
# end of class SaveDialog
class MyApp(wx.App): def OnInit(self): self.frame = ActorList(None, wx.ID_ANY, "") self.SetTopWindow(self.frame) self.frame.Show() return True
# end of class MyApp
if __name__ == "__main__": app = MyApp(0) app.MainLoop()
As you can see in the class TableChoice class I have added ONE radio button with wxglade. BUT when the databse it chosen and queried from the table names the list that is returned can vary. it may be 12 tables or 4 tables or whatever. How to I dynamically created the number of radio buttons needed without destroying the TableChoice code that wxglade has already written correctly. The same thing will apply to the grid in ActorList class. self.diaplaygrid.
Thanks in advance
_______________________________________________
wxGlade-general mailing list
wxGlade-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/wxglade-general