在wx python中启动wx.dialog时出现问题

时间:2016-03-30 17:18:01

标签: python wxpython

我是python的新手。我开始使用wx python来创建GUI。 GUI有一个主框架。主框架有按钮说START。当我点击按钮时,会弹出另一个窗口并显示一些复选框。但我收到了错误。以下是代码

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()

以下错误我正在

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__'

1 个答案:

答案 0 :(得分:1)

您使用dict作为参数/变量名,但dict是Python中字典类的保留名称。尝试将其命名为其他内容。