I create a panel in wxpython, plus i have a database(MySQLdb), then i select some data from my database and i want to insert them in a wx.Combobox(dropdown), after that if the choice is A choice or B choice i want to insert some other data from database in a listbox.The code is below:
import MySQLdb
import sys
import wx
APP_SIZE_X = 661
APP_SIZE_Y = 319
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,
size=(APP_SIZE_X, APP_SIZE_Y))
panel = wx.Panel(self, -1,style=wx.SUNKEN_BORDER)
sel="Make your choice"
wx.StaticText(panel, -1,sel,(15,10))
db=MySQLdb.connect(host="localhost",use_unicode="True",
charset="utf-8",
user="youruser",passwd="somepwd",db="choicedb")
cursor=db.cursor()
sql="""SELECT name from choicetb"""
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
print row[1]
sampleList = ["A choice", "B choice", "C choice"]#The data from db
wx.ComboBox(panel, -1, "A choice", (15, 30),
wx.DefaultSize,sampleList, wx.CB_DROPDOWN)
math="Selected items"
wx.StaticText(panel, -1,math,(10,100))
listBox = wx.ListBox(panel, -1, (10, 130), (230, 120),
' ', wx.LB_SINGLE)
exitbutton =wx.Button(panel,-1, label="Quit", pos=(300, 230))
exitbutton.Bind(wx.EVT_BUTTON, self.OnQuit)
self.Centre()
def OnQuit(self, e):
self.Close()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'form1.py')
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(0)
app.MainLoop()
How i insert what choice i take from row in combobox, i try something but it gives me the last choice of course. I know is something inside for loop but what? Thanks for your answers and for your help.
Put the new data into a Python list and then use the ListBox’s / ComboBox’s AppendItems(your_list) method. That’s probably the easiest way. If you already have a list for the combobox, you could do something like this:
Something like that should work.