Search code examples
pythonwxpython

Issue while launching wx.dialog in wx python


I am new to python . I started using wx python to create a GUI. GUI is having one main frame. Main frame is having button say START. When I click on button another window will pop up and will have some check boxes. But I am getting error. Below is the code

import wx
import re

class MyFrame(wx.Frame):
    i = 1
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(3000, 3000))
        panel = wx.Panel(self)
        #panel.Bind(wx.EVT_MOTION,  self.OnMove)
        wx.StaticText(panel, -1, "What are the values of X", pos=(10, 12))
        startButton=wx.Button(panel,label="Start",pos=(800, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, self.start, startButton)
        txt = open("questions.txt") 
        dict = {}
        alist = []

        for line in txt:    
            if re.search("^\n", line): continue
            searchObj = re.search("([\d]+)\.\s*(.*)", line)
            if searchObj :
                i = searchObj.group(1)
                j = 1
                key = 'Q' + str(i)
                dict[key] = [searchObj.group(2)]
            searchObj2 = re.search("[^0-9]+\.\s*(.*)", line)
            if searchObj2 : 
                dict[key,j] = (searchObj2.group(1))
                j = j + 1
        dict['totalQuestions'] = i
        txt.close()
        print dict
    def newwindow(self, event):
        dia = MyDialog(self, -1, 'buttons', "true")
        dia.ShowModal()
        dia.Destroy()
    def start(self, event):
        dictIndex = self.i
        start = MyQuestionDialog(self, -1, "button", dictIndex, dict)
        start.ShowModal()
        start.Destroy()
        dictIndex = dictIndex + 1





class MyQuestionDialog(wx.Dialog):
    def __init__(self, parent, id, title, dictIndex, dict):
        wx.Dialog.__init__(self, parent, id, title, size=(1000,1000))
        # panel1 = wx.Panel(self, -1)
        key = 'Q' + str(dictIndex)
        # wx.StaticText(panel, -1, dict[key], pos=(10, 12))
        #self.posCtrl = wx.TextCtrl(panel, -1, "", pos=(100, 10))
        wx.CheckBox(self, -1, dict[key,1], (20,100), (160,-1))
        # self.getValue = self.option1.GetValue()
        wx.CheckBox(self, -1, dict[key,2], (20,150), (160,-1))
        wx.CheckBox(self, -1, dict[key,3], (20,200), (160,-1))
        wx.CheckBox(self, -1, dict[key,4], (20,250), (160,-1))
        button=wx.Button(self,label="OK",pos=(800, 400), size = (50,50))
        # self.Bind(wx.EVT_BUTTON, self.newwindow, button)
        nextButton=wx.Button(self,label="Next",pos=(1000, 400), size = (50,50))
        # self.Bind(wx.EVT_BUTTON, self.nextwindow, nextButton)



class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title, answer):
        wx.Dialog.__init__(self, parent, id, title, size=(350,300))
        wx.StaticText(self, -1, answer , pos=(10, 12))


app = wx.App(False)
frame = MyFrame()
frame.Show(True)
app.MainLoop()

Following error I am getting

Traceback (most recent call last):
  File "3_phase.py", line 39, in start
    start = MyQuestionDialog(self, -1, "button", dictIndex, dict)
  File "3_phase.py", line 55, in __init__
    wx.CheckBox(self, -1, dict[key,1], (20,100), (160,-1))
TypeError: 'type' object has no attribute '__getitem__'

Solution

  • You're using dict as your argument/variable name, but dict is a reserved name for the dictionary class in Python. Try naming it something else.