Hi I have the following code which reads a file, makes data class and based
on that shows multiple choice question view. Thus I need to dynamically add
and change radio buttons. I don't know if the way I wrote the code has some
defect or something .. but the radio buttons wont appear vertically
straight, but would appear rather shifted vertically.
My data class is :
class QAPojo:
def __init__(self):
self.question=""
self.options=[]
self.answer=""
def setQuestion(self,question):
self.question=question
def setOptions(self,options):
self.options=options
def setAnswer(self,answer):
self.answer=answer
def getQuestion(self):
return self.question
def getOptions(self):
return self.options
def getAnswer(self):
return self.answer
and my application file is:
import wx
from edu.uno.utils.mathUtils import mathUtils
class QAFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, id=-1, title="parent", pos=(0,
0), size=(500,600))
self.load()
self.initUI()
def load(self):
self.mUtils=mathUtils()
self.mUtils.load()
def initUI(self):
mainpanel=wx.Panel(self, wx.ID_ANY)
self.panel1=wx.Panel(mainpanel, wx.ID_ANY)
self.panel2=wx.Panel(mainpanel, wx.ID_ANY)
btn_OK=wx.Button(self.panel2, label="OK")
btn_OK.Bind(wx.EVT_BUTTON, self.clickOK)
self.QPnl=wx.Panel(self.panel1)
self.question=wx.TextCtrl(self.QPnl,value="",size=wx.DefaultSize,style=wx.ALL|wx.EXPAND|wx.TE_MULTILINE|wx.TE_READONLY)
qsizer=wx.BoxSizer(wx.VERTICAL)
qsizer.Add(self.question)
self.QPnl.SetSizer(qsizer)
self.OptionPnl=wx.Panel(self.panel1, wx.ID_ANY)
self.optionSizer=wx.BoxSizer(wx.VERTICAL)
self.OptionPnl.SetSizer(self.optionSizer)
self.QOSizer=wx.BoxSizer(wx.VERTICAL)
self.QOSizer.Add(self.QPnl,proportion=1,flag=wx.EXPAND|wx.ALL,border=5)
self.QOSizer.Add(self.OptionPnl,proportion=1,flag=wx.EXPAND|wx.ALL,border=5)
self.timer=wx.Timer(self.panel1)
self.panel1.Bind(wx.EVT_TIMER, self.StartQA, self.timer)
self.timer.Start(100,oneShot=True)
self.i=0
self.panel1.SetSizer(self.QOSizer)
vsizer=wx.BoxSizer(wx.VERTICAL)
vsizer.Add(self.panel1,proportion=1,flag=wx.EXPAND|wx.ALL,border=5)
vsizer.Add(self.panel2,proportion=1,flag=wx.EXPAND|wx.ALL,border=5)
mainpanel.SetSizer(vsizer)
def StartQA(self,event):
self.StartQARepeat()
def StartQARepeat(self):
while(self.i<len(self.mUtils.QAODict)):
eachQA=self.mUtils.QAODict[self.i]
self.createQueOptionView(eachQA)
self.i+=1
self.OptionPnl.Layout()
self.OptionPnl.Fit()
self.OptionPnl.Refresh()
break
def createQueOptionView(self,QAPojo):
self.question.Clear()
self.question.AppendText(QAPojo.getQuestion())
radioOptions=QAPojo.getOptions()
# radioOptions=["apple","ball"]
first=True
self.OptionPnl.Destroy()
self.OptionPnl=wx.Panel(self.panel1, wx.ID_ANY)
# self.optionSizer.Clear()
self.optionSizer=wx.BoxSizer(wx.VERTICAL)
for option in radioOptions:
print option
if first:
radio=wx.RadioButton( self.OptionPnl, -1, option,style =
wx.RB_GROUP|wx.ALL)
first=False
else:
radio=wx.RadioButton( self.OptionPnl, -1,
option,style=wx.ALL|wx.EXPAND)
radio.Bind(wx.EVT_RADIOBUTTON, self.onChooseRadioButton)
# self.optionSizer.Add((5,5))
self.optionSizer.Add(radio,wx.UP|wx.BOTTOM|wx.EXPAND, 15)
self.OptionPnl.SetSizer(self.optionSizer)
self.QOSizer.Add(self.OptionPnl,proportion=1,flag=wx.UP|wx.BOTTOM,border=25)
wx.CallLater(int(3000), self.StartQARepeat)
# self.OptionPnl.Fit()
self.OptionPnl.Layout()
self.OptionPnl.Refresh()
self.panel1.Fit()
self.panel1.Layout()
self.panel1.Refresh()
# def addRadioButton(self,radio):
···
#
def onChooseRadioButton(self, event):
"""
This method is fired when its corresponding button is pressed
"""
btn = event.GetEventObject()
self.label = btn.GetLabel()
def clickOK(self,event):
print "click OK"
message = "You just selected %s" % self.label
dlg = wx.MessageDialog(None, message, 'Message',
wx.OK|wx.ICON_EXCLAMATION)
dlg.ShowModal()
dlg.Destroy()
if __name__ == "__main__":
app = wx.App(False)
frame = QAFrame()
frame.Show()
app.MainLoop()
--
View this message in context: http://wxpython-users.1045709.n5.nabble.com/radio-button-does-not-appear-just-vertically-but-appear-shifted-tp5722294.html
Sent from the wxPython-users mailing list archive at Nabble.com.