如何在WX python中关闭已经打开的窗口

时间:2016-03-31 09:44:25

标签: python wxpython

我正在编写一个程序,主屏幕上有Start之类的按钮。

当我点击Start按钮时,它会打开一个新窗口。

新窗口将显示按钮Next

当我们点击Next按钮时,它会再次打开一个窗口。

我已完成所有编码,并且按预期工作。

问题:

当我开始新窗口时,我无法关闭旧窗口。

新窗口打开但旧窗口仍然存在。

我是WX的新手。

我没有得到如何做到

以下是代码:

import wx
import re

class MyFrame(wx.Frame):
    i = 1
    dict1 = {}
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "My Frame", size=(2000, 800))
        panel = wx.Panel(self)
        #panel.Bind(wx.EVT_MOTION,  self.OnMove)
        text = wx.StaticText(panel, -1, "Lets Play", pos=(80, 400))
        font = wx.Font(32, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        text.SetFont(font)
        startButton=wx.Button(panel,label="Start",pos=(800, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, self.start, startButton)
        txt = open("questions.txt") 

        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)
                self.dict1[key] = searchObj.group(2)
            searchObj2 = re.search("^[a-z]\.\s*(.*)", line)
            if searchObj2 : 
                self.dict1[key,j] = (searchObj2.group(1))
                j = j + 1
            searchObj3 = re.search("Answer:\s*(\d)", line)
            if searchObj3 :
                self.dict1[key,"answer"] = searchObj3.group(1)
            searchObj4 = re.search("pic(\d)\.\s*(.*)", line)
            if searchObj4 :
                i = searchObj4.group(1)
                j = 1
                key = 'Q' + str(i)
                self.dict1[key] = searchObj4.group(2)
                self.dict1[key,'pic'] = "yes"
            searchObj5 = re.search("path:\s*(.*)", line)
            if searchObj5 : 
                self.dict1[key,'picPath'] = (searchObj5.group(1))


        self.dict1['totalQuestions'] = i
        txt.close()
        print self.dict1
    def start(self, event):
        # 
        dict1Index = self.i
        dict1 = self.dict1
        self.i = self.i + 1
        start = MyQuestionDialog(self, -1, "button", dict1Index, dict1)
        start.ShowModal()
        start.Destroy()
        # self.Close()

        # dict1Index = dict1Index + 1





class MyQuestionDialog(wx.Dialog):
    def __init__(self, parent, id, title, dict1Index, dict1):
        self.dict1 = dict1
        self.dict1Index = dict1Index
        wx.Dialog.__init__(self, parent, id, title, size=(1000,1000))
        # panel1 = wx.Panel(self, -1)
        key = 'Q' + str(dict1Index)

        text = wx.StaticText(self, -1, dict1[key], pos=(10, 12))
        font = wx.Font(18, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        text.SetFont(font)
        # text.SetForegroundColour(wx.RED)
        #self.posCtrl = wx.TextCtrl(panel, -1, "", pos=(100, 10))
        dictPicKey = (key, "pic")
        # print "****dickey*************************",dictPicKey
        # print "^^^^^^^^^^^^^^^^^^^^^^^",dict1
        if dictPicKey in dict1 :
            print "^^^^^^^^^######################################################"
            picPath = dict1[key, 'picPath']
            print "^^^^^^^^^",picPath
            start_image = wx.Image(picPath) 
            start_image.Rescale(500, 500) 
            image = wx.BitmapFromImage(start_image) 
            # mypic = wx.StaticBitmap(self, -1, image, wx.DefaultPosition, style=wx.BITMAP_TYPE_PNG) 
            mypic = wx.StaticBitmap(self, -1, image, (10,100)) 
            self.option1 = wx.CheckBox(self, -1, dict1[key,1], (600,100), (160,-1)) 
            self.option2 = wx.CheckBox(self, -1, dict1[key,2], (600,150), (160,-1))
            self.option3 = wx.CheckBox(self, -1, dict1[key,3], (600,200), (160,-1))         
            self.option4 = wx.CheckBox(self, -1, dict1[key,4], (600,250), (160,-1))         
        else :
            self.option1 = wx.CheckBox(self, -1, dict1[key,1], (20,100), (160,-1))
            self.option2 = wx.CheckBox(self, -1, dict1[key,2], (20,150), (160,-1))
            self.option3 = wx.CheckBox(self, -1, dict1[key,3], (20,200), (160,-1))
            self.option4 = wx.CheckBox(self, -1, dict1[key,4], (20,250), (160,-1))

        button=wx.Button(self,label="NEXT",pos=(800, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, frame.start, button)
        okButton=wx.Button(self,label="OK",pos=(600, 400), size = (50,50))
        self.Bind(wx.EVT_BUTTON, self.checkAnswer, okButton)


        # nextButton=wx.Button(self,label="Next",pos=(1000, 400), size = (50,50))
        # self.Bind(wx.EVT_BUTTON, self.nextwindow, nextButton)
    def checkAnswer(self, event):
        self.getValue1 = self.option1.GetValue()
        self.getValue2 = self.option2.GetValue()
        self.getValue3 = self.option3.GetValue()
        self.getValue4 = self.option4.GetValue()
        key = 'Q' + str(self.dict1Index)
        qAnswer =  self.dict1[key,'answer']
        print "qAnswer is ########", qAnswer        
        ansKey = "self" + "." + "getValue" + qAnswer
        print "########anskey", eval(ansKey)
        ansKey = eval(ansKey)
        if ansKey == True :
            result = "correct"
            correctAnswer = ""
        else :
            result = "Incorrect"
            correctAnswer = self.dict1[key,int(qAnswer)]
        dia = MyDialog(self, -1, 'buttons', result, correctAnswer)
        dia.ShowModal()
        dia.Destroy()


class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title, answer, correctAnswer):
        wx.Dialog.__init__(self, parent, id, title, size=(350,300))
        text = wx.StaticText(self, -1, answer , pos=(100, 12))
        font = wx.Font(18, wx.DECORATIVE, wx.ITALIC, wx.NORMAL)
        text.SetFont(font)
        if answer == "correct" :
            text.SetForegroundColour(wx.GREEN)
        else :
            text.SetForegroundColour(wx.RED)
        str = "Correct Answer is : \n" + correctAnswer
        text1 = wx.StaticText(self, -1, str , pos=(100, 120))


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

0 个答案:

没有答案